How to use docker compose config command to validate and view compose files

DockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker-compose config command to validate and view your Docker Compose files. We will start by installing Docker Compose and then create a basic compose file.

You will then use the docker-compose config command to validate the syntax of the compose file, view the resolved configuration in both YAML and JSON formats, and extract specific information like service names and image names. This lab will equip you with essential skills for working with Docker Compose files effectively.

Validate a basic compose file

In this step, you will learn how to validate a basic Docker Compose file. Before we start, we need to install Docker Compose as it is not pre-installed in this environment.

First, let's download the Docker Compose binary. We will download version 1.29.2, which is a stable and widely used 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 repository 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 give the downloaded binary executable permissions.

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

This command makes the docker-compose command executable.

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

docker-compose --version

You should see output similar to docker-compose version 1.29.2, build 5becea4c. This confirms that Docker Compose is installed correctly.

Now, let's create a simple Docker Compose file. We will create a file named docker-compose.yml in your ~/project directory.

nano ~/project/docker-compose.yml

Add the following content to the file:

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

This is a very basic Docker Compose file. It defines a single service named web that uses the nginx:latest image and maps port 80 on the host to port 80 in the container.

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

Before running the compose file, it's a good practice to validate its syntax. Docker Compose provides a command for this: config.

docker-compose config

If the syntax of your docker-compose.yml file is correct, this command will output the resolved configuration in YAML format. If there are any syntax errors, it will print an error message indicating the problem. This helps you catch errors early before attempting to build or run your services.

View the resolved configuration in YAML format

In the previous step, we validated the syntax of our docker-compose.yml file using the docker-compose config command. By default, this command outputs the resolved configuration in YAML format. This is useful for seeing the final configuration that Docker Compose will use, including any default values or merges from multiple compose files.

Let's run the command again to view the output. Make sure you are in the ~/project directory where you created the docker-compose.yml file.

cd ~/project
docker-compose config

You should see output similar to this:

services:
  web:
    build:
      context: /home/labex/project
    image: nginx:latest
    ports:
      - published: 80
        target: 80
version: "3.8"

Notice that the output includes the build section with the context set to /home/labex/project. Even though we didn't explicitly define a build section in our docker-compose.yml, Docker Compose adds a default build context which is the directory containing the compose file. This is an example of how docker-compose config shows the resolved configuration, including defaults.

Viewing the resolved configuration in YAML format is helpful for debugging and understanding exactly how Docker Compose interprets your file.

View the resolved configuration in JSON format

In the previous step, we saw how docker-compose config outputs the resolved configuration in YAML format by default. Sometimes, it can be useful to view the configuration in JSON format, especially if you are working with tools or scripts that process JSON.

The docker-compose config command has a --format flag that allows you to specify the output format. To get the output in JSON format, we use --format json.

Make sure you are in the ~/project directory.

cd ~/project
docker-compose config --format json

You should see the resolved configuration printed to the console in JSON format. The output will look something like this (the exact formatting might vary slightly):

{
  "services": {
    "web": {
      "build": {
        "context": "/home/labex/project"
      },
      "image": "nginx:latest",
      "ports": [
        {
          "published": 80,
          "target": 80
        }
      ]
    }
  },
  "version": "3.8"
}

Comparing this output to the YAML output from the previous step, you can see that it represents the same configuration but in a different data format. Both formats are useful depending on your needs.

Viewing the configuration in JSON format can be particularly helpful when integrating Docker Compose with other tools or for programmatic processing of the configuration.

In this step, we will learn how to use the docker-compose config command to print only the names of the services defined in our compose file. This can be useful when you need a quick list of the services without the full configuration details.

The docker-compose config command has a --services flag that does exactly this. It will parse the compose file and output only the names of the services, one per line.

Make sure you are in the ~/project directory.

cd ~/project
docker-compose config --services

Since our docker-compose.yml file currently only has one service named web, the output of this command will be:

web

If you had multiple services defined in your compose file, each service name would be printed on a new line. For example, if you had services named web, db, and app, the output would be:

web
db
app

This flag is a convenient way to quickly list the services defined in your Docker Compose project.

In this final step, we will learn how to use the docker-compose config command to print only the names of the images used by the services defined in our compose file. This is useful for quickly seeing which images your project depends on.

The docker-compose config command has a --images flag that does this. It will parse the compose file and output only the names of the images, one per line.

Make sure you are in the ~/project directory.

cd ~/project
docker-compose config --images

Since our docker-compose.yml file uses the nginx:latest image for the web service, the output of this command will be:

nginx:latest

If you had multiple services using different images, each image name would be printed on a new line. For example, if you had services using nginx:latest, mysql:5.7, and python:3.9, the output would be:

nginx:latest
mysql:5.7
python:3.9

This flag provides a quick overview of the images required by your Docker Compose project.

Summary

In this lab, you learned how to install Docker Compose by downloading the binary and granting executable permissions, and verified the installation by checking the version. You then created a basic docker-compose.yml file defining a simple web service using the Nginx image. Finally, you used the docker-compose config command to validate the syntax of the compose file, ensuring it is correctly formatted before deployment.