How to use docker compose images command to list images

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker compose images command to list the Docker images used by services defined in a docker-compose.yml file. We will start by preparing a simple docker-compose.yml file and then use the docker compose up command to start the services.

Following that, we will explore different ways to list the images. You will learn how to list all images used by the created containers, how to list only the image IDs using the quiet option, and how to list the images in JSON format for easier parsing. This hands-on experience will provide you with practical skills for managing images within your Docker Compose projects.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/start("Start Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") docker/ImageOperationsGroup -.-> docker/images("List Images") subgraph Lab Skills docker/ps -.-> lab-555080{{"How to use docker compose images command to list images"}} docker/start -.-> lab-555080{{"How to use docker compose images command to list images"}} docker/pull -.-> lab-555080{{"How to use docker compose images command to list images"}} docker/images -.-> lab-555080{{"How to use docker compose images command to list images"}} end

Prepare a simple docker-compose.yml file

In this step, we will prepare a simple docker-compose.yml file. Before we start, we need to install Docker Compose. 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 Docker Compose binary 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

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

docker-compose --version

You should see output similar to Docker Compose version v2.20.2.

Next, we will create a directory for our project and navigate into it.

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

Now, we will create a docker-compose.yml file using the nano editor. This file will define a simple web service using the nginx image.

nano docker-compose.yml

In the nano editor, paste the following content:

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

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

  • version: '3.8' specifies the Compose file format version.
  • services: defines the services for our application.
  • web: is the name of our service.
  • image: nginx:latest specifies the Docker image to use for this service. In this case, we are using the latest version of the nginx image.
  • ports: maps ports between the host and the container. "80:80" maps port 80 on the host to port 80 on the container.

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

Before starting the service, we need to pull the nginx:latest image.

docker pull nginx:latest

This command downloads the nginx:latest image from Docker Hub.

Start services defined in the docker-compose.yml file

In this step, we will start the services defined in our docker-compose.yml file. We will use the docker-compose up command to build, create, and start the services.

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

cd ~/project/my-docker-app

Now, run the following command to start the services in detached mode (in the background).

docker-compose up -d

The -d flag runs the containers in detached mode, meaning they will run in the background and not tie up your terminal.

You should see output indicating that the web service is being created and started.

To verify that the container is running, you can use the docker ps command.

docker ps

You should see a container named my-docker-app-web-1 (or similar, depending on the project directory name) with the nginx image, and the status should be Up.

We can also check if the web server is accessible by using curl to access localhost on port 80.

curl http://localhost:80

You should see the default Nginx welcome page HTML output in your terminal, confirming that the web server is running and accessible.

List images used by the created containers

In this step, we will list the Docker images that are currently on our system, specifically focusing on the image used by the container we just created. The docker images command is used to list images.

Run the following command to list all images.

docker images

You should see a table with information about the images, including REPOSITORY, TAG, IMAGE ID, CREATED, and SIZE. You should see the nginx image listed here.

The output shows the images that have been pulled or built on your system. The nginx image was pulled in the previous step and is now used by the running container.

List only image IDs using the quiet option

In this step, we will learn how to list only the image IDs using the docker images command with the quiet option. This is useful when you need to get a list of image IDs for scripting or other purposes.

The quiet option is specified with the -q flag.

Run the following command to list only the image IDs.

docker images -q

You should see a list of image IDs, one per line. This output is much more concise than the full output from docker images.

This option is particularly helpful when you want to process the list of images programmatically, for example, to remove old images.

List images in JSON format

In this step, we will learn how to list Docker images in JSON format. This is useful when you want to parse the output programmatically using tools like jq.

We can use the --format option with the docker images command to specify the output format. To get the output in JSON format, we use the json keyword.

Run the following command to list images in JSON format.

docker images --format json

You should see the output in JSON format, which is a structured way to represent the image information. Each image will be represented as a JSON object within a JSON array.

This format is ideal for integrating Docker image information into scripts or other applications that can process JSON data.

After completing this step, you can stop the running container and remove the project directory if you wish.

To stop the container, navigate back to the ~/project/my-docker-app directory and run:

cd ~/project/my-docker-app
docker-compose down

This command stops and removes the containers, networks, and volumes created by docker-compose up.

To remove the project directory, you can use the rm command:

cd ~/project
rm -rf my-docker-app

Summary

In this lab, we learned how to use the docker compose images command to list images used by services defined in a docker-compose.yml file. We began by preparing a simple docker-compose.yml file defining a web service using the nginx image, including installing Docker Compose and creating the necessary file structure.

We then started the services defined in the docker-compose.yml file. Finally, we explored different ways to list the images used by the created containers, including listing all image details, listing only image IDs using the quiet option, and listing images in JSON format.