5.4: Life cycle: up, down, stop, start

WithDockerCompose, managing a multi-container stack becomes much simpler than using repetitive commandsdocker run. The goal is to be able to start, stop, restart, or delete your entire stack with just a few commands, without having to interact with each container individually.

A stack’s lifecycle is primarily managed using four commands: up, down, stop, and start, plusrestart for quick restarts.

Start the stack: docker compose up

To launch all the services defined in your docker-compose.yml, use:

docker compose up -d

The `–background` option-dstarts the stack in the background, leaving your terminal free. Compose then creates the containers if they don’t exist yet, mounts the volumes, connects the networks, and launches all the services. If you omit -d`–background`, you’ll see the logs directly in the terminal, which is handy for verifying the startup.

Stop and delete the stack: docker compose down

When you want to stop all containers and clean up your stack, use the “ commanddown:

docker compose down

By default,docker compose down does not delete the volumes. This allows you to keep the persistent data we configured in/data or via bind mounts.

⚠️ Be careful with the `–delete` option-v:

docker compose down -v

my_data:/var/lib/mysqlDockerThis deletes all volumes associated with the stack, resulting in the loss of data stored in those volumes. If you have configured your databases or persistent files via volumes managed byDocker`(type)`, use this option with caution. For volumes mounted on local folders (./data:/var/lib/mysql“), your data remains secure even if you delete the containers.

Pause and restart the stack

To temporarily suspend your stack without deleting the containers, usestop:

docker compose stop

You can restart stopped containers with:

docker compose start

To quickly restart a service (useful after a configuration change or to flush logs):

docker compose restart php-web

Add / update images: docker compose pull

Before launching a stack for the first time, or when you want to fetch the latest versions of the images, you can use the command:

docker compose pull

This command downloads all the images defined in yourdocker-compose.yml without starting the containers. This allows you to prepare your environment or check if an update is available.

Once the images are downloaded, you can start the stack normally:

docker compose up -d

If an image has been updated,Docker will automatically recreate the container with the new version.

In practice, a typical update of a Compose stack often looks like this:

docker compose pull
docker compose up -d

The first command fetches the new images, and the second restarts the services if necessary.

Tip: You can also combine the update and startup directly with:

docker compose up -d --pull always

Docker It will then systematically check if a new image is available before starting the containers.

Monitoring the stack’s status

To check which containers are running and their status:

docker compose ps

To monitor logs in real time:

docker compose logs -f

You can also view the volumes and networks in use with:

docker volume ls
docker network ls

Best practice: Cleanly updating a Compose stack

In production or on a server, updating a Compose stackDocker is typically done in two steps. First, fetch the new images, then restart the stack.

docker compose pull
docker compose up -d --remove-orphans

The first command downloads the latest versions of the images defined in the docker-compose.yml.

docker-compose.ymlThe second command restarts the stack in the background. If an image has changed,Dockerit will automatically recreate the relevant container.
The option--remove-orphansallows you to remove old containers that are no longer defined in the file. This prevents obsolete services from continuing to run.

This method is particularly useful when your project evolves and certain services are added, renamed, or removed.

In a context of regular server updates, this sequence is often automated via scripts or orchestration tools.

Common Errors withDockerCompose

When starting out withDockerCompose, certain misunderstandings come up very often. Here are the most common ones.

Thinking thatdocker compose pull updates the containers

The following command only downloads the new images:

docker compose pull

But it does not restart the containers. To apply the update, you must restart the stack:

docker compose up -d

Modifying thedocker-compose.yml file without restarting the stack

If you modify a port, a volume, or an environment variable in your Compose file, existing containers will not be automatically updated.

You must apply the changes with:

docker compose up -d

Docker will only recreate the containers affected by the changes.

Usingdown when a simplestop would suffice

Some users stop their stack with:

docker compose down

When they actually just wanted to temporarily stop the services.
In this case, the following command is often more appropriate:

docker compose stop

This prevents the containers from being recreated on the next startup.

Accidentally deleting volumes with down -v

As seen previously, the following command also deletes volumes:

docker compose down -v

DockerIf your databases or persistent files are stored in volumes, the data will be permanently lost.

Conclusion

Mastering these commands allows you to manage the entire lifecycle of a Compose stack.up -d starts the stack,down deletes it,stop/start handles temporary interruptions, andrestart quickly restarts a service.

⚠️ Important reminder about volumes:

if you use volumesDockerto store your data, do not accidentally delete the volumes with down -v, otherwise your databases or persistent files will be lost. Use local bind mounts (./data) instead to maintain full control over your data.


📌 Key takeaways

Most actions inDockerCompose follow a simple logic:

  • pull → download images
  • up -d → apply changes and start the stack
  • stop/start → pause temporarily
  • down → delete the stack

WithDockerCompose, managing a stack relies on a few simple commands that cover the entire container lifecycle.

The general logic is as follows:

docker compose pull
        │
        ▼
docker compose up -d
        │
        ▼
     RUNNING
        │
   ┌────┴────┐
   ▼         ▼
stop       restart
   │
   ▼
start
   │
   ▼
docker compose down