I have a docker-compose.yml
file that contains 4 containers: redis, postgres, api, worker
During the development of worker, I often need to restart it in order to apply changes. Is there any good way to restart a container (e.g. worker
) without restarting the other containers?
Simple 'docker' command knows nothing about 'worker' container. Use command like this
docker-compose -f docker-compose.yml restart worker
Following command
will just STOP and START the container. i.e without loading any changes from the docker-compose.xml
STOP is similar to hibernating in PC . Hence stop/start will not look for any changes made in configuration file . To reload from the recipe of container (docker-compose.xml) we need to remove and create the container (Similar analogy to rebooting the PC )
So commands will be as following
The other answers to restarting a single node are on target,
docker-compose restart worker
. That will bounce that container, but not include any changes, even if you rebuilt it separately. You can manuallystop
,rm
,create
, andstart
, but there are much easier methods.If you've updated your code, you can do the build and reload in a single step with:
That will first rebuild your images from any changed code, which is fast if there are no changes since the cache is reused. And then it only replaces the changed containers. If your downloaded images are stale, you can precede the above command with:
To download any changed images first (the containers won't be restarted until you run a command like the
up
above). Doing an initial stop is unnecessary.And to only do this for a single service, follow the up or pull command with the services you want to specify, e.g.:
Here's a quick example of the first option, the Dockerfile is structured to keep the frequently changing parts of the code near the end. In fact the requirements are pulled in separately for the
pip install
since that file rarely changes. And since the nginx and redis containers were up-to-date, they weren't restarted. Total time for the entire process was under 6 seconds:Restart Service with docker-compose file
If the file name is
docker-compose.yml
and service is workerIf the file name
sample.yml
and service is workerThis is it
It is very simple: Use the command:
You can set the time to wait for stop before killing the container (in seconds)
Thats all !
To restart a service with changes here are the steps that I performed: