Dovid Gefen
Dovid Gefen

Reputation: 403

Does running docker-compose restart re-run init script

I have a Docker container with a init script CMD ["init_server.sh"] which is orchestrated by docker-compose.

Does running docker-compose restart re-run the init script, or will only running docker-compose down followed by docker-compose up trigger the script to be run again?

I imagine whatever the answer to this will apply to docker restart as well. Am I correct?

Upvotes: 1

Views: 1524

Answers (1)

David Maze
David Maze

Reputation: 159373

A Docker container only runs one process, defined by the "entrypoint" and "command" settings (typically from a Dockerfile, you can override them in a docker-compose.yml). Whatever that process does, it will do every time the container starts.

In terms of Docker commands, the Compose commands you show aren't different from their underlying plain-Docker variants. restart is just stop followed by start, so it will re-run the main container process in its existing container with the existing (possibly modified) container filesystem. If you do a docker rm in between these (or docker-compose down) the process starts in a clean container based on the image.

It's typical for an initialization script to check if the initialization it requires has already been done. For things like the standard Docker Hub database images, this works by checking if the data directory is totally empty; initialization only happens on the very first startup. An init script that runs something like database migrations will generally keep track of which migrations have already been done and won't repeat work.

Upvotes: 4

Related Questions