How to use docker compose rm command to remove stopped containers

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker compose rm command to remove stopped containers. We will begin by preparing a simple Docker Compose project, which involves installing Docker Compose and creating a docker-compose.yml file to define a basic web service.

Following the project setup, you will practice stopping the service containers. The core of the lab focuses on using docker compose rm to remove these stopped containers, covering scenarios without volumes, with anonymous volumes, and finally, demonstrating how to force the removal of containers.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker(("Docker")) -.-> docker/VolumeOperationsGroup(["Volume Operations"]) 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/VolumeOperationsGroup -.-> docker/volume("Manage Volumes") subgraph Lab Skills docker/ps -.-> lab-555090{{"How to use docker compose rm command to remove stopped containers"}} docker/stop -.-> lab-555090{{"How to use docker compose rm command to remove stopped containers"}} docker/rm -.-> lab-555090{{"How to use docker compose rm command to remove stopped containers"}} docker/pull -.-> lab-555090{{"How to use docker compose rm command to remove stopped containers"}} docker/volume -.-> lab-555090{{"How to use docker compose rm command to remove stopped containers"}} end

Prepare a simple Docker Compose project

In this step, we will prepare a simple Docker Compose project. Since Docker Compose is not pre-installed in the LabEx environment, we will first install it. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.

First, let's install Docker Compose. We will download the latest stable release and make it executable.

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

After the installation, verify the installation by checking the version.

docker-compose --version

You should see the version number printed in the output, confirming that Docker Compose is installed correctly.

Now, let's create a simple Docker Compose project. We will create a directory for our project and then create a docker-compose.yml file inside it. This file will define a simple web service using the nginx image.

Navigate to the project directory.

cd ~/project

Create a new directory for our project, for example, my-web-app.

mkdir my-web-app
cd my-web-app

Now, create the docker-compose.yml file using the nano editor.

nano docker-compose.yml

Add the following content to the docker-compose.yml file:

version: "3.8"
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"

This docker-compose.yml file defines one service named web. This service uses the nginx:latest Docker image and maps port 80 on the host to port 80 in the container.

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

Now, we can start the services defined in the docker-compose.yml file using the docker-compose up command. The -d flag runs the containers in detached mode, meaning they will run in the background.

docker-compose up -d

This command will pull the nginx:latest image (if not already present) and start a container for the web service.

You can check the status of the running containers using the docker ps command.

docker ps

You should see a container named my-web-app_web_1 (or similar, depending on the directory name) running and forwarding port 80.

To verify that the web server is running, you can use curl to access it.

curl http://localhost

You should see the default Nginx welcome page HTML in the output. This confirms that our simple Docker Compose project is set up and running correctly.

Stop the service containers

In the previous step, we started our simple web service using docker-compose up -d. The container is now running in the background. In this step, we will learn how to stop the running service containers using Docker Compose.

To stop the running containers defined in the docker-compose.yml file, we use the docker-compose stop command. This command will send a SIGTERM signal to the containers, allowing them to shut down gracefully.

Make sure you are in the ~/project/my-web-app directory where your docker-compose.yml file is located.

cd ~/project/my-web-app

Now, execute the docker-compose stop command.

docker-compose stop

You will see output indicating that the container is being stopped.

After running the stop command, the containers will be stopped but not removed. You can verify this by listing all containers, including the stopped ones, using the docker ps -a command.

docker ps -a

You should see the my-web-app_web_1 container listed with a status of Exited. This confirms that the container has been stopped successfully.

If you try to access the web server using curl again, it should fail because the container is stopped.

curl http://localhost

You should see a connection refused error, indicating that the web service is no longer running.

Stopping containers is useful when you need to temporarily halt your services without removing their configuration or data. In the next steps, we will explore how to remove these stopped containers.

Remove stopped service containers without volumes

In the previous step, we stopped the running container using docker-compose stop. The container is now in an Exited state. In this step, we will learn how to remove these stopped containers using Docker Compose.

To remove the stopped containers defined in the docker-compose.yml file, we use the docker-compose rm command. By default, this command only removes stopped service containers. It does not remove volumes or networks.

Make sure you are in the ~/project/my-web-app directory where your docker-compose.yml file is located.

cd ~/project/my-web-app

Now, execute the docker-compose rm command. You will be prompted to confirm the removal. Type y and press Enter.

docker-compose rm

You will see output indicating that the container is being removed.

After running the rm command, the container should be removed. You can verify this by listing all containers, including the stopped ones, using the docker ps -a command.

docker ps -a

You should no longer see the my-web-app_web_1 container in the list. This confirms that the container has been removed successfully.

Removing stopped containers helps to clean up your system and free up resources. In the next steps, we will explore how to handle volumes when removing containers.

Remove stopped service containers with anonymous volumes

In the previous step, we removed the stopped container. Now, let's explore how to remove containers along with their associated anonymous volumes. Anonymous volumes are volumes that are not explicitly named in the docker-compose.yml file. Docker automatically assigns a random name to them.

First, let's modify our docker-compose.yml file to include an anonymous volume. We will add a volume to the web service.

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

cd ~/project/my-web-app

Edit the docker-compose.yml file.

nano docker-compose.yml

Add the volumes section to the web service definition. We will mount an anonymous volume to /usr/share/nginx/html inside the container. This is where Nginx serves its default content.

version: "3.8"
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - /usr/share/nginx/html

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

Now, let's bring up the service again. This will create a new container and an anonymous volume.

docker-compose up -d

You can list the volumes created by Docker using the docker volume ls command. You should see a volume with a long, randomly generated name associated with your project.

docker volume ls

Now, stop the service containers again.

docker-compose stop

Verify that the container is stopped using docker ps -a.

docker ps -a

You should see the my-web-app_web_1 container in the Exited state.

To remove the stopped containers and their associated anonymous volumes, we use the docker-compose rm command with the -v flag.

docker-compose rm -v

You will be prompted to confirm the removal. Type y and press Enter.

After running the command, verify that the container is removed using docker ps -a.

docker ps -a

The my-web-app_web_1 container should no longer be listed.

Now, check the volumes again using docker volume ls.

docker volume ls

The anonymous volume that was created for the web service should also be removed. This demonstrates how the -v flag removes anonymous volumes along with the containers.

Force remove stopped service containers

In the previous steps, we stopped and removed containers. Sometimes, you might need to remove containers even if they are still running. This is where the force removal option comes in handy.

First, let's bring up our service again.

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

cd ~/project/my-web-app

Start the service in detached mode.

docker-compose up -d

Verify that the container is running using docker ps.

docker ps

You should see the my-web-app_web_1 container listed with a status of Up.

Now, let's try to remove the running container without stopping it first. If you try to use docker-compose rm on a running container, it will fail.

docker-compose rm

You will see an error message indicating that the container is running and cannot be removed.

To force the removal of running containers, we use the docker-compose rm command with the -f flag. This flag forces the removal without requiring confirmation and without stopping the containers gracefully.

docker-compose rm -f

You will see output indicating that the container is being removed. Notice that you were not prompted for confirmation this time.

After running the command, verify that the container is removed using docker ps -a.

docker ps -a

The my-web-app_web_1 container should no longer be listed. This confirms that the container was force-removed even though it was running.

Using the force removal option should be done with caution, as it can lead to data loss if the container was writing data to a volume that is not explicitly managed.

This concludes our exploration of removing Docker Compose service containers. You have learned how to stop containers, remove stopped containers without volumes, remove stopped containers with anonymous volumes, and force remove running containers.

Summary

In this lab, we learned how to use the docker compose rm command to remove stopped containers. We began by preparing a simple Docker Compose project, which involved installing Docker Compose and creating a docker-compose.yml file to define a basic web service using the Nginx image.

We then practiced stopping the service containers and subsequently removing them using docker compose rm. We explored different scenarios, including removing stopped containers without volumes, removing stopped containers with anonymous volumes, and using the force option to remove containers. This hands-on experience demonstrated the practical application of the docker compose rm command for managing the lifecycle of containers within a Docker Compose project.