Container startup scripts #129
Replies: 1 comment
-
Dockerfile ENTRYPOINT is your key. It's part of a section on advanced images that I never finished (but still want to add to Docker Mastery). That's how the official images deal with database startup (creating db's, users, setting passwords, etc). ENTRYPOINT and CMD, if both are set, are combined into one line at container startup. Then you would put a script as the ENTRYPOINT and the last line of that script would "pass execution" to the rest of the command. This trick is how you can use those two Dockerfile entries to provide the startup magic. For postgres, see it setting up ENTRYPOINT here. Then see all the stuff it's doing in the script at startup, and notice the last line in the shell script. The script starts psql, does all the things, then stops it right before the script stops, then it passes execution to the CMD where the final daemon is started. This combo of ENTRYPOINT and CMD is also how you can create easy CLI tools with Docker. This is what I do in the Swarm Mastery course with httping, where I run the command in ENTRYPOINT, and set the CMD to --help. Why would I do that? Well remember when you do a docker run you can easily override the CMD at the end of the line, so in that way, I can run the httping image and just throw my httping command options at the end of a docker run. See my httping Dockerfile, and the Readme. Also, there's more info on all this in Docker's Dockerfile best practices. |
Beta Was this translation helpful? Give feedback.
-
Hi Bret!
I have a technical issue that I've encountered several times already. For example, I needed to create a postgres database container and run an initialization sql script in order to populate it with a database. I know I can use volumes, but if a new developer sets up his development environment then he won't be able to run things with simple
docker-compose up
command, he will have to run something extra in order to initialize the database. The main problem is that postgres must be already running for the initialization scripts to work. The official postgres image provides a solution for this particular case by providing a special folder/docker-entrypoint-initdb.d/
where it looks for any.sh
or.sql
initialization files and executes them in alphabetical order after postgres service has started.I found the same kind of problem when I wanted my kafka container to have a specific topic preinitialized with custom configuration before I am able to use the container. I cannot place topic creation commands in the
Dockerfile
because in order for those commands to work, kafka service must be started and running. So the command must be somehow executed after the defaultCMD
instruction is run and service is ready, healthy and running.Now I am thinking about how can I create an Elasticsearch container with a bunch of precreated indexes that I can create only when elasticsearch is already up and running.
Could you give me a hint, where should I be looking for a solution for this kind of problem?
Beta Was this translation helpful? Give feedback.
All reactions