How to use docker compose pull command to update service images

DockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker compose pull command to update service images defined in a compose.yaml file. We will start by preparing a compose.yaml file with services that utilize Docker images.

Following the setup, you will explore various ways to use the docker compose pull command, including pulling a specific service image, pulling all service images, pulling images while ignoring buildable services, and pulling images quietly without displaying progress information. This hands-on experience will equip you with the skills to efficiently manage and update your Docker Compose service images.

Prepare a compose.yaml file with services using images

In this step, we will prepare a compose.yaml file that defines services using Docker images. Docker Compose is a tool that allows you to define and manage multi-container Docker applications. While Docker is already installed on the LabEx VM, Docker Compose is not. We will install Docker Compose first.

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 that Docker Compose is installed, let's verify the installation by checking the version.

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-compose-app
cd ~/project/my-compose-app

Now, we will create a compose.yaml file in this directory. This file will define two services: a web service using the nginx image and a database service using the redis image.

nano compose.yaml

Paste the following content into the compose.yaml file:

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
  db:
    image: redis:latest

Save the file and exit the nano editor (Press Ctrl + X, then Y, then Enter).

This compose.yaml file defines two services:

  • web: Uses the nginx:latest Docker image and maps port 80 on the host to port 80 in the container.
  • db: Uses the redis:latest Docker image.

In the following steps, we will use this compose.yaml file to pull the defined service images.

Pull a specific service image using docker compose pull

In this step, we will learn how to pull a specific service image defined in our compose.yaml file using the docker compose pull command. This is useful when you only need to update or download the image for a single service without affecting others.

First, make sure you are in the ~/project/my-compose-app directory where you created the compose.yaml file in the previous step.

cd ~/project/my-compose-app

Now, let's pull the image for the web service. The command to do this is docker compose pull <service_name>. In our case, the service name is web.

docker compose pull web

You will see output indicating that Docker Compose is pulling the nginx:latest image. This process downloads the image layers from the Docker registry.

After the command completes, you can verify that the nginx image has been pulled by listing the available Docker images.

docker images

You should see nginx listed in the output, along with its tag (latest), image ID, creation date, and size.

This demonstrates how to selectively pull an image for a specific service defined in your compose.yaml file. In the next step, we will pull images for all services.

Pull all service images defined in the compose.yaml file

In this step, we will learn how to pull images for all services defined in our compose.yaml file using the docker compose pull command without specifying any service names. This is the default behavior of docker compose pull when no service names are provided.

First, ensure you are in the ~/project/my-compose-app directory.

cd ~/project/my-compose-app

Now, execute the docker compose pull command without any arguments.

docker compose pull

This command will read the compose.yaml file and pull the images for all services defined within it. Since we already pulled the nginx image in the previous step, Docker Compose will check if the image is up to date. It will then proceed to pull the redis:latest image, as it hasn't been pulled yet.

You will see output indicating the pulling process for the redis image.

After the command completes, you can verify that both the nginx and redis images have been pulled by listing the available Docker images.

docker images

You should now see both nginx and redis listed in the output.

This demonstrates how to easily pull all necessary images for your multi-container application with a single command.

Pull service images while ignoring buildable services

In this step, we will explore how to use the --ignore-buildable flag with docker compose pull. This flag is useful when your compose.yaml file includes services that are built from a Dockerfile (buildable services) in addition to services that use pre-built images. The --ignore-buildable flag tells Docker Compose to only pull images for services that specify an image key, and to skip services that specify a build key.

First, let's modify our compose.yaml file to include a buildable service. We will add a simple service that builds a basic image.

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

cd ~/project/my-compose-app

Now, create a simple Dockerfile for our buildable service.

nano Dockerfile

Paste the following content into the Dockerfile:

FROM alpine:latest
CMD ["echo", "Hello from buildable service!"]

Save the file and exit the nano editor.

Next, modify the compose.yaml file to include a new service that uses this Dockerfile.

nano compose.yaml

Add the following service definition to your compose.yaml file, below the db service:

builder:
  build: .

Your complete compose.yaml file should now look like this:

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
  db:
    image: redis:latest
  builder:
    build: .

Save the file and exit the nano editor.

Now, let's try to pull images using docker compose pull --ignore-buildable.

docker compose pull --ignore-buildable

You will observe that Docker Compose checks the web and db services and pulls their images if necessary (they should already be pulled from the previous steps). However, it will ignore the builder service because it uses the build key instead of the image key. You will not see any output related to building or pulling an image for the builder service.

To confirm that no image was built or pulled for the builder service, you can list your Docker images.

docker images

You should still only see the nginx and redis images listed. There will not be a new image created from the Dockerfile by this command.

This demonstrates how the --ignore-buildable flag allows you to selectively pull only pre-built images, which can be useful in various development and deployment scenarios.

Pull service images quietly without progress information

In this step, we will learn how to pull service images without displaying the detailed progress information using the --quiet or -q flag with docker compose pull. This is useful when you want to reduce the amount of output in your terminal, for example, in scripts or automated workflows.

First, ensure you are in the ~/project/my-compose-app directory.

cd ~/project/my-compose-app

Now, let's use the docker compose pull --quiet command to pull the images. Since the images for web and db are likely already present from previous steps, Docker Compose will check if they are up to date. If they are, the command will complete quickly with minimal output. If an image needed to be pulled, you would not see the usual progress bars and layer information.

docker compose pull --quiet

You will notice that the output is significantly less verbose compared to running docker compose pull without the --quiet flag. You might see messages indicating that images are already up to date, but you won't see the detailed download progress for each layer.

To demonstrate the effect more clearly, let's first remove one of the images and then pull it quietly. We will remove the redis image.

docker rmi redis:latest

Now, pull the images again using the --quiet flag.

docker compose pull --quiet

This time, the redis image will be pulled, but you will not see the detailed progress. The output will be minimal, confirming that the pull operation is happening without the usual visual feedback.

You can verify that the redis image has been pulled again by listing the Docker images.

docker images

You should see both nginx and redis listed.

The --quiet flag is a simple yet effective way to control the verbosity of the docker compose pull command, making it suitable for environments where detailed output is not desired.

Summary

In this lab, we learned how to use the docker compose pull command to update service images. We began by preparing a compose.yaml file defining services with specific Docker images, including installing Docker Compose itself.

Subsequently, we explored pulling images for individual services, pulling all services defined in the file, and utilizing options to ignore buildable services and suppress progress output for a quieter pull process.