How to restart single docker container within multiple docker container?


I am a real big fan of working with docker because it provides you a certain level of isolation and the ability to move your dockerize application anywhere from development to the production environment.

But often when I work with the multiple numbers of running docker containers then it is often needed to restart a single container running among the multiple containers and the reason is I need to apply some changes to my application which is running inside my docker container.

So is there a way using docker-compose.yml I can restart my single container?

Well yes, you can do that with docker-compose.yml along with parameters restart. So in this blog post, we are going to see a different way to restart your container.

Here are the different ways you can restart your single docker container using docker-compose.yml

  1. Using docker-compose restart <your_container_name>
  2. Restart docker container with --detach --build e.g. docker-compose up --detach --build followed by docker restart <your_container_name>
  3. Restart docker container by first stop followed by container removal(rm) and container image(rmi)


1. Using docker-compose restart <your_container_name>

If you only want to restart your docker container then you can simply run the following command by putting the container name -

1docker-compose restart my_container_name 

You can also put the timeout in second along with your restart container command so that the container will restart after that defined time interval.

1docker-compose restart -t 40 my_container_name  

The above mentioned docker-compose command will restart the container after 40 seconds

Note:- One thing to notice over here is the restart command will only restart the container but it will not pull the changes



2. Restart docker container with --detach --build

In the previous step 1 we have seen how you can just restart your single docker container without pulling new changes.

But if you want to restart your docker container with the new or latest changes then your supply --detach --build parameters which will only pull the changes which you made else it will reuse the docker cache.

Here is the command sequence to restart your docker container -

1docker-compose up --detach --build 

And then restart the container

1docker-compose restart worker 

But in case if you do not care more about pulling only the required changes you can simply run the docker-compose pull it will simply discard cache and remove your old docker image with the new one.



3. Restart docker container by first stop followed by container removal(rm) and container image(rmi)

If you really want to follow a very clean approach to restart your docker container where you do not want any of your previously built images and container lying around then I would recommend using the following sequence of commands -

(To find the container-id use the command docker ps -a)

  1. Stop docker container - docker stop container-id
  2. Remove docker container - docker rm container-id
  3. Remove docker image - docker rmi image-id
  4. Build fresh image - docker-compose up container-name

The above docker command sequence is a hard way to restart your docker container and it is often recommended when you do not want to inherit from the previous stale cache of docker. Use the above approach when you want to start clean.

In case you are overwhelmed by docker then I would recommend reading this blog post on Deploying spring boot Microservice with Docker

For more References -

  1. Stackoverflow - How to restart a single container with docker-compose


Posts in this Series