How to use docker compose alpha viz command to visualize compose files

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively visualize your Docker Compose files using the docker-compose alpha viz command. This hands-on experience will guide you through generating basic graphs to represent your services, and then progressively enhance these visualizations by including service images, networks, and ports. You will also discover how to customize the output graph's indentation for better readability.

By the end of this lab, you will be able to create clear and informative visual representations of your Docker Compose configurations, making it easier to understand the relationships and dependencies within your multi-container applications.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/NetworkOperationsGroup(["Network Operations"]) docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker(("Docker")) -.-> docker/VolumeOperationsGroup(["Volume Operations"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/port("List Container Ports") docker/ImageOperationsGroup -.-> docker/images("List Images") docker/VolumeOperationsGroup -.-> docker/volume("Manage Volumes") docker/NetworkOperationsGroup -.-> docker/network("Manage Networks") subgraph Lab Skills docker/ls -.-> lab-555072{{"How to use docker compose alpha viz command to visualize compose files"}} docker/ps -.-> lab-555072{{"How to use docker compose alpha viz command to visualize compose files"}} docker/port -.-> lab-555072{{"How to use docker compose alpha viz command to visualize compose files"}} docker/images -.-> lab-555072{{"How to use docker compose alpha viz command to visualize compose files"}} docker/volume -.-> lab-555072{{"How to use docker compose alpha viz command to visualize compose files"}} docker/network -.-> lab-555072{{"How to use docker compose alpha viz command to visualize compose files"}} end

Generate a basic graph from a compose file

In this step, you will learn how to generate a basic graph representation of your Docker Compose file. This is useful for visualizing the relationships between your services, networks, and volumes.

First, you need to install Docker Compose. Since it's not pre-installed in the LabEx environment, you'll install it using pip.

sudo apt update
sudo apt install -y python3-pip
pip install docker-compose

After the installation is complete, you can verify the installation by checking the version of Docker Compose.

docker-compose --version

You should see output similar to docker-compose version 1.29.2, build 5becea4c.

Now, let's create a simple Docker Compose file. Navigate to your project directory and create a file named docker-compose.yml.

cd ~/project
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"
  app:
    image: ubuntu:latest
    command: sleep infinity

This docker-compose.yml file defines two services: web using the nginx:latest image and app using the ubuntu:latest image. The web service maps port 80 on the host to port 80 in the container. The app service simply runs the sleep infinity command to keep the container running.

To generate a basic graph from this compose file, you will use the docker-compose config command with the --graph flag. This command parses the compose file and outputs a graph representation in DOT format.

docker-compose config --graph

The output will be in DOT language, which is a graph description language. It will show the services and their basic connections. For example, you might see something like:

digraph {
  compound=true
  "web" [label="web"]
  "app" [label="app"]
  "web" -> "app" [label="depends_on"]
}

This DOT output represents a directed graph where web and app are nodes, and there's a directed edge from web to app labeled "depends_on". While this simple example doesn't explicitly define dependencies, docker-compose config --graph can infer some relationships.

Include service images in the graph

In this step, you will learn how to include the service images in the generated graph. By default, the basic graph only shows the service names. Including the image names provides more context about what each service represents.

You will continue using the docker-compose.yml file you created in the previous step.

version: "3.8"
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
  app:
    image: ubuntu:latest
    command: sleep infinity

To include the service images in the graph output, you need to use the --images flag with the docker-compose config --graph command.

docker-compose config --graph --images

Executing this command will generate the DOT graph output, but this time, the nodes representing the services will also include the image names. The output will be similar to the previous step, but with additional information within the node definitions.

For example, the output might now look like this:

digraph {
  compound=true
  "web" [label="web\nnginx:latest"]
  "app" [label="app\nubuntu:latest"]
  "web" -> "app" [label="depends_on"]
}

Notice that the label attribute for each node now includes both the service name and the image name, separated by a newline character (\n). This makes the graph more informative by showing which Docker image is used for each service.

This is a simple way to add more detail to your Docker Compose graph visualization, helping you understand the components of your application at a glance.

Include service networks in the graph

In this step, you will learn how to include the networks that your services are connected to in the generated graph. Understanding the network topology is crucial for debugging and visualizing the communication flow between your containers.

You will modify the docker-compose.yml file to explicitly define a network and connect the services to it.

Open the docker-compose.yml file in your project directory:

nano ~/project/docker-compose.yml

Modify the content to include a network definition and connect the services to this network:

version: "3.8"
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    networks:
      - my_network
  app:
    image: ubuntu:latest
    command: sleep infinity
    networks:
      - my_network

networks:
  my_network:
    driver: bridge

In this updated docker-compose.yml, we've added a networks section at the top level to define a network named my_network using the bridge driver. We've also added a networks key under each service (web and app) to specify that they should be connected to my_network.

Now, to include the network information in the graph output, you will use the --networks flag along with the --graph flag.

docker-compose config --graph --networks

Executing this command will generate the DOT graph output, which will now include nodes representing the network and edges showing which services are connected to it. The output will be more complex than before, illustrating the network connections.

You will see nodes for the services (web and app) and a node for the network (my_network). There will be edges connecting the services to the network, indicating their membership in that network. This visualization helps you see how your services are isolated or connected at the network level.

Include service ports in the graph

In this step, you will learn how to include the exposed and published ports of your services in the generated graph. Visualizing the ports helps understand how your services are accessible from outside the Docker network or from other services.

You will continue using the docker-compose.yml file from the previous steps.

version: "3.8"
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    networks:
      - my_network
  app:
    image: ubuntu:latest
    command: sleep infinity
    networks:
      - my_network

networks:
  my_network:
    driver: bridge

To include the service ports in the graph output, you will use the --ports flag along with the --graph flag. You can combine multiple flags to include different types of information in the graph. Let's include images, networks, and ports in the graph.

docker-compose config --graph --images --networks --ports

Executing this command will generate the DOT graph output, which will now include information about the ports for each service. The output will be even more detailed, showing the service names, image names, network connections, and port mappings.

For the web service, you will see the port mapping 80:80 included in its node definition. This indicates that port 80 on the host is mapped to port 80 inside the web container.

The DOT output for the web service node might look something like this:

"web" [label="web\nnginx:latest\n80:80"]

By including ports in the graph, you get a clearer picture of how your services are exposed and how external traffic or traffic from other containers can reach them. This is particularly useful for debugging connectivity issues or understanding the service's external interface.

Customize indentation in the output graph

In this step, you will learn how to customize the indentation of the generated DOT graph output. While the default indentation is usually acceptable, you might want to adjust it for better readability or to conform to specific formatting standards if you plan to process the output further.

You will continue using the docker-compose.yml file from the previous steps.

version: "3.8"
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    networks:
      - my_network
  app:
    image: ubuntu:latest
    command: sleep infinity
    networks:
      - my_network

networks:
  my_network:
    driver: bridge

To customize the indentation of the graph output, you can use the --indent flag followed by the number of spaces you want to use for indentation. Let's generate the graph with images, networks, and ports included, and set the indentation to 4 spaces.

docker-compose config --graph --images --networks --ports --indent 4

Executing this command will produce the same graph content as in the previous step, but the lines in the DOT output will be indented with 4 spaces. Compare the output of this command with the output from the previous step to see the difference in indentation.

For example, a section of the output might now look like this with 4 spaces of indentation:

digraph {
    compound=true
    "web" [label="web\nnginx:latest\n80:80"]
    "app" [label="app\nubuntu:latest"]
    "my_network" [label="my_network"]
    "web" -> "my_network" [label="network"]
    "app" -> "my_network" [label="network"]
    "web" -> "app" [label="depends_on"]
}

You can experiment with different indentation values to see how it affects the output formatting. This option is primarily for aesthetic purposes or for tools that require a specific indentation style for DOT files.

Summary

In this lab, you learned how to visualize Docker Compose files using the docker-compose config --graph command. You began by installing Docker Compose and creating a basic docker-compose.yml file with two services. You then generated a basic graph representation of this file in DOT format, which visually depicts the services and their relationships.

Building upon the basic visualization, you explored how to include additional details in the graph. This included incorporating service images, networks, and ports into the visual output, providing a more comprehensive view of the compose file's configuration. Finally, you learned how to customize the indentation of the output graph for improved readability.