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
- Using docker-compose restart <your_container_name>
- Restart docker container with --detach --build e.g. docker-compose up --detach --build followed by docker restart <your_container_name>
- 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
)
- Stop docker container -
docker stop container-id
- Remove docker container -
docker rm container-id
- Remove docker image -
docker rmi image-id
- 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 -
- Stackoverflow - How to restart a single container with docker-compose
Posts in this Series
- (docker run -d) Why Does a Docker Container Stop Automatically?
- Attach and detach from Docker's process?
- How I Change Name of My Docker Repository and Rename Images?
- How to set-up Cron Jobs in Docker Containers?
- 4 Ways to copy file from localhost to docker container
- Multiple commands execution in Docker Compose?
- How to push Docker Images to AWS ECR(Elastic Container registry)?
- How to Copy Docker images from one host to another host?
- What is persistent storage in docker and how to manage it
- Docker Installation on MacOS, Linux and Windows
- Docker - ADD, Update, Export Environment variable
- How to fix-Docker docker failed to compute cache key not found
- How to fix docker driver failed programming external connectivity on endpoint webserver?
- How to fix docker error executable file not found in $PATH?
- How to expose port on live containers?
- How to expose multiple ports with Docker?
- How to restart single docker container within multiple docker container?
- How to edit file within Docker container or edit a file after I shell into a Docker container?
- How to fix Error starting docker service Unit not found?
- How to remove old, unused images of Docker?
- How to fix docker error invalid reference format error?
- How to fix requested access to the resource is denied?
- How to fix Docker error cannot delete docker container conflict unable to remove repository reference?
- How to fix docker error no space left on device?
- How to connect localhost from docker container?
- Docker COPY vs Docker ADD?
- 6 Ways to fix - Got permission denied while trying to connect to the Docker daemon socket?
- 6 Ways to fix – Docker COPY failed: stat no source files were specified