Assume, you run a multi-container Docker application which consists of a database-container and an application-container. What is the difference between the following two methods to shut it down:
First method
1. Open a terminal and start the application with docker-compose up
.
2. Shut the application down with Ctrl + C
.
Second method
1. Open a terminal and start the application with docker-compose up
.
2. Open a second terminal and shut the application down with docker-compose down
.
In my understanding, both methods should do the exact same. However, when using the second method, I usually see some exceptions in the logs of the application that it can't connect to the database when it has already been shut down, which I otherwise don't see.
What is the recommended method?
docker-compose down - command will stop running containers, but it also removes the stopped containers as well as any networks that were created. You can take down one step further and add the -v flag to remove all volumes too.
The key difference between docker run versus docker-compose is that docker run is entirely command line based, while docker-compose reads configuration data from a YAML file. The second major difference is that docker run can only start one container at a time, while docker-compose will configure and run multiple.
docker-compose up : Builds, (re)creates, and starts containers. It also attaches to containers for a service. docker-compose start : Start the stopped containers, can't create new ones.
The docker-compose down command helps to Stop and remove containers, networks, images, and volumes.
Running Ctrl+C will only stop the containers (it's actually equivalent to docker-compose stop
) while docker-compose down
will also remove the stopped containers and delete any networks created by the compose file.
Ctrl+C will only work however if all services are not started as detached (docker-compose up
does not have the -d
flag set)
As for the recommended method, it really depends on your particular use case. If you only need to stop the services, but not remove the containers, docker-compose stop
will suffice. If you want to maybe rebuild the images or use different images, you might need to run docker-compose down
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With