Difference Between Stop, Down, Up, and Start in Docker Compose
-
Difference Between
docker compose stop
anddocker compose down
-
Difference Between
docker compose start
anddocker compose up
Docker Compose has multiple similar commands but functions in a purely different way. A few examples are the commands docker compose stop
and docker compose down
and the commands docker compose start
and docker compose up
.
This article will discuss their differences accordingly.
Difference Between docker compose stop
and docker compose down
Use of the docker compose stop
The primary function of running the docker compose stop
command is to stop the services of a running container. The command will stop our containers but will not necessarily delete them.
For example, the command below will stop all containers if we do not specify a container name or ID:
docker compose stop
Output:
Going to stop file, web, test
Stopping file ... done
Stopping web ... done
Stopping test ... done
We can specify which container to stop by specifying a container name or ID:
docker compose stop file
Output:
Going to stop file
Stopping file ... done
Also, we can specify a shutdown timeout when running docker compose stop
. Docker will usually wait for 10 seconds before it stops our container.
For instance, if we know that our container might need a little more time when stopping, we may want to increase this timeout value, as shown below.
docker compose stop file -t 60
Use of the docker compose down
On the other hand, the docker compose down
command gives us an extra step in the process. The said command will stop and delete the container and the services running inside; the deletion includes both the container and the networks.
The docker compose down
acts similarly with both the docker stop <container name or id>
and docker <prune or rm>
when executed sequentially. We can say that the docker compose down
command is a little bit of a shortcut of two combined commands.
So instead of running the two commands below:
docker compose stop file && docker compose rm -f
We can use docker compose down
instead.
docker compose down file
We can take it up a notch by appending a -v
or --volumes
flags to our docker compose down
command. The command will stop and remove containers, their networks, and the volumes attached when executed.
With the said flag addition, we have combined three commands into just one command.
docker compose down file -v
We can use the docker compose down
command not only for containers but also for images. For example, we can run the docker compose down --rmi <all or local>
command to remove images.
The command is similar to the docker rmi
command when used in native Docker.
Difference Between docker compose start
and docker compose up
Use of the docker compose start
The same with our previous section, the commands docker compose start
and docker compose up
sounds similar but act differently in function. For example, the docker compose start
command restarts a particular container that is stopped previously.
docker compose start file
Furthermore, the said command works only for containers that are already created.
So, how can we directly start an uncreated container? We can use the following command below.
Use of the docker compose up
The docker compose up
command starts a new container based on the docker-compose.yml
file. It is similar to running docker create
and docker start
when run sequentially with each other.
Since we are using a YAML file, we can create and start multiple containers with just one command.
docker compose up
Output:
Creating file
Creating web
Creating test
When we use docker compose up
, if there are any changes in the source YAML file, the containers based on that YAML file will be stopped and recreated.
To avoid this, we can use the –-no-recreate
option as shown below during docker compose up
. To summarize, if the container already exists, the command will not recreate it.
docker-compose up -d --no-recreate
Additionally, just like docker compose stop
, we can specify a timeout value.
docker-compose up -d -t 30
We can also say that this is the main counterpart of the command docker run
when running and starting a new container in a vanilla Docker environment.
Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.
LinkedIn