How to use docker compose down command to stop and remove resources

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively use the docker compose down command to stop and remove resources defined in a Docker Compose file. We will begin by preparing a simple Compose file with two services and a network.

Following the setup, you will practice starting these services using docker compose up. The core of the lab will then focus on using docker compose down to stop and remove the default resources, and subsequently explore the options -v to remove volumes and --rmi all to remove images, demonstrating different levels of resource cleanup.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/VolumeOperationsGroup(["Volume Operations"]) docker(("Docker")) -.-> docker/NetworkOperationsGroup(["Network Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/stop("Stop Container") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") docker/ImageOperationsGroup -.-> docker/rmi("Remove Image") docker/ImageOperationsGroup -.-> docker/images("List Images") docker/VolumeOperationsGroup -.-> docker/volume("Manage Volumes") docker/NetworkOperationsGroup -.-> docker/network("Manage Networks") subgraph Lab Skills docker/run -.-> lab-555077{{"How to use docker compose down command to stop and remove resources"}} docker/ps -.-> lab-555077{{"How to use docker compose down command to stop and remove resources"}} docker/stop -.-> lab-555077{{"How to use docker compose down command to stop and remove resources"}} docker/rm -.-> lab-555077{{"How to use docker compose down command to stop and remove resources"}} docker/pull -.-> lab-555077{{"How to use docker compose down command to stop and remove resources"}} docker/rmi -.-> lab-555077{{"How to use docker compose down command to stop and remove resources"}} docker/images -.-> lab-555077{{"How to use docker compose down command to stop and remove resources"}} docker/volume -.-> lab-555077{{"How to use docker compose down command to stop and remove resources"}} docker/network -.-> lab-555077{{"How to use docker compose down command to stop and remove resources"}} end

Prepare a simple Compose file with services and a network

In this step, we will prepare a simple Compose file to define two services and a network. Before we start, we need to install Docker Compose as it is not pre-installed in the environment.

First, let's download the Docker Compose binary. We will download version 1.29.2, which is compatible with the pre-installed Docker version.

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

This command downloads the Docker Compose binary from the official GitHub releases page and saves it to /usr/local/bin/docker-compose. The $(uname -s) and $(uname -m) parts automatically detect your operating system and architecture to download the correct binary.

Next, we need to apply executable permissions to the downloaded binary so that we can run it as a command.

sudo chmod +x /usr/local/bin/docker-compose

Now, let's verify the installation by checking the Docker Compose version.

docker-compose --version

You should see the version information printed to the console, confirming that Docker Compose is installed correctly.

Now that Docker Compose is installed, let's create a directory for our project and navigate into it. We will create a directory named my-compose-app in the ~/project directory.

mkdir ~/project/my-compose-app
cd ~/project/my-compose-app

Inside this directory, we will create a file named docker-compose.yml. This file will define our services and network. We will use the nano editor to create and edit this file.

nano docker-compose.yml

Now, paste the following content into the docker-compose.yml file. This file defines two services: web and redis, and a network named app-network.

version: "3.8"
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    networks:
      - app-network
  redis:
    image: redis:latest
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

Let's break down this docker-compose.yml file:

  • version: '3.8' specifies the Docker Compose file format version.
  • services: defines the different services that make up your application.
    • web: defines a service named web.
      • image: nginx:latest specifies that this service will use the latest nginx image. We will pull this image later.
      • ports: - "80:80" maps port 80 on the host machine to port 80 in the web container.
      • networks: - app-network connects the web service to the app-network.
    • redis: defines a service named redis.
      • image: redis:latest specifies that this service will use the latest redis image. We will pull this image later.
      • networks: - app-network connects the redis service to the app-network.
  • networks: defines the networks used by the services.
    • app-network: defines a network named app-network.
      • driver: bridge specifies that this network will use the default bridge driver.

Save the file by pressing Ctrl + X, then Y, and then Enter.

Before starting the services, we need to pull the necessary Docker images. We will pull the nginx:latest and redis:latest images.

docker pull nginx:latest
docker pull redis:latest

These commands download the specified images from Docker Hub to your local machine.

Start the services using docker compose up

In this step, we will start the services defined in our docker-compose.yml file using the docker compose up command. This command builds, (re)creates, starts, and attaches to containers for a service.

Make sure you are in the ~/project/my-compose-app directory where you created the docker-compose.yml file.

cd ~/project/my-compose-app

Now, run the following command to start the services. We will use the -d flag to run the containers in detached mode, meaning they will run in the background and not block your terminal.

docker-compose up -d

When you run this command for the first time, Docker Compose will:

  1. Create the app-network as defined in the docker-compose.yml file.
  2. Create a container for the web service using the nginx:latest image.
  3. Create a container for the redis service using the redis:latest image.
  4. Connect both containers to the app-network.
  5. Start both containers.

You should see output indicating that the network and containers are being created and started.

To verify that the containers are running, you can use the docker ps command.

docker ps

This command lists all running containers. You should see two containers listed, one for the web service (based on the nginx image) and one for the redis service (based on the redis image). The container names will typically be prefixed with the directory name (my-compose-app in this case) and the service name (e.g., my-compose-app_web_1, my-compose-app_redis_1).

You can also check the networks created by Docker Compose using the docker network ls command.

docker network ls

You should see a network named my-compose-app_app-network listed, which was created based on the app-network definition in your docker-compose.yml file.

Finally, since we mapped port 80 of the web service to port 80 on the host, you can access the default Nginx welcome page by opening a web browser and navigating to the IP address of your LabEx VM. Alternatively, you can use curl from the terminal.

curl localhost

You should see the HTML content of the default Nginx welcome page in the output. This confirms that the web service is running and accessible.

Stop and remove default resources using docker compose down

In this step, we will stop and remove the containers and networks created by docker compose up using the docker compose down command. By default, docker compose down removes the containers and the default network created by up.

Make sure you are in the ~/project/my-compose-app directory.

cd ~/project/my-compose-app

Now, run the following command to stop and remove the services.

docker-compose down

When you run this command, Docker Compose will:

  1. Stop the running containers (web and redis).
  2. Remove the stopped containers.
  3. Remove the network (my-compose-app_app-network) that was created by docker compose up.

You should see output indicating that the containers and network are being stopped and removed.

To verify that the containers have been removed, you can use the docker ps -a command. The -a flag shows all containers, including stopped ones.

docker ps -a

You should no longer see the my-compose-app_web_1 and my-compose-app_redis_1 containers in the output.

You can also verify that the network has been removed using the docker network ls command.

docker network ls

The my-compose-app_app-network should no longer be listed in the output.

Note that docker compose down by default does not remove volumes or images. We will explore how to remove those in the next steps.

Stop and remove volumes using docker compose down -v

In the previous step, we saw that docker compose down removes containers and networks but not volumes. In this step, we will learn how to remove volumes along with other resources using the -v flag with docker compose down.

First, let's bring up the services again so that we have resources to remove. Make sure you are in the ~/project/my-compose-app directory.

cd ~/project/my-compose-app

Run the docker compose up -d command to start the services in detached mode.

docker-compose up -d

This will recreate the containers and the network.

Now, let's check if any volumes were created. In our docker-compose.yml, we didn't explicitly define any volumes. However, some images, like redis, might create anonymous volumes by default to persist data. We can check for volumes using the docker volume ls command.

docker volume ls

You might see a volume listed with a name like my-compose-app_redisdata (the exact name might vary slightly depending on the Docker Compose version and configuration, but it will likely be prefixed with the project name and service name). This is an anonymous volume created by the redis container.

Now, let's stop and remove the services, network, and also the volumes using docker compose down -v.

docker-compose down -v

The -v flag tells Docker Compose to remove all volumes declared in the volumes section of the docker-compose.yml file and any anonymous volumes attached to containers.

You should see output indicating that the containers, network, and the volume are being stopped and removed.

To verify that the containers have been removed, use docker ps -a.

docker ps -a

The web and redis containers should not be listed.

To verify that the network has been removed, use docker network ls.

docker network ls

The my-compose-app_app-network should not be listed.

Finally, to verify that the volume has been removed, use docker volume ls.

docker volume ls

The volume that was previously listed (e.g., my-compose-app_redisdata) should no longer be in the output.

Stop and remove images using docker compose down --rmi all

In the previous steps, we learned how to stop and remove containers, networks, and volumes using docker compose down. By default, docker compose down does not remove the Docker images that were used to create the containers. In this step, we will learn how to remove images using the --rmi all flag with docker compose down.

First, let's bring up the services again. Make sure you are in the ~/project/my-compose-app directory.

cd ~/project/my-compose-app

Run the docker compose up -d command to start the services in detached mode.

docker-compose up -d

This will recreate the containers and the network.

Now, let's check the Docker images on your system using the docker images command.

docker images

You should see the nginx and redis images listed, along with any other images you might have on your system.

Now, let's stop and remove the services, network, volumes, and also the images using docker compose down --rmi all.

docker-compose down --rmi all -v

We are including the -v flag again to ensure volumes are also removed. The --rmi all flag tells Docker Compose to remove all images used by any service in the docker-compose.yml file, even if they are not tagged.

You should see output indicating that the containers, network, volumes, and images are being stopped and removed.

To verify that the containers have been removed, use docker ps -a.

docker ps -a

The web and redis containers should not be listed.

To verify that the network has been removed, use docker network ls.

docker network ls

The my-compose-app_app-network should not be listed.

To verify that the volumes have been removed, use docker volume ls.

docker volume ls

Any volumes related to this project should not be listed.

Finally, to verify that the images have been removed, use docker images.

docker images

The nginx and redis images that were used by the services should no longer be listed in the output, unless they are being used by other containers or have other tags. docker compose down --rmi all removes images that are not used by any other container.

Summary

In this lab, we learned how to use the docker compose down command to stop and remove resources defined in a Docker Compose file. We began by preparing a simple Compose file with two services and a network, which involved installing Docker Compose and creating the docker-compose.yml file.

After starting the services using docker compose up, we explored different ways to use docker compose down. We first used the basic command to stop and remove the default resources (containers and networks). Subsequently, we learned how to remove volumes using the -v flag and how to remove images using the --rmi all flag, demonstrating the flexibility of the docker compose down command in managing the lifecycle of our Docker Compose applications.