Introduction
In this lab, you will learn how to use the docker compose top command to display running processes within your Docker containers. We will begin by preparing a simple docker-compose.yml file to define a service, then start the service using Docker Compose. Finally, you will use the docker compose top command, both generally and with a specific service name, to inspect the processes running inside the containers.
Prepare a simple docker-compose.yml file
In this step, we will prepare a simple docker-compose.yml file. Before we start, let's understand what Docker Compose is. 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.
Since Docker Compose is not pre-installed in the LabEx environment, we need to install it first. 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 create a simple docker-compose.yml file in the ~/project directory. This file will define a single service that uses the ubuntu image and simply runs the sleep infinity command to keep the container running.
We will use the nano editor to create and edit the file.
nano ~/project/docker-compose.yml
In the nano editor, paste the following content:
version: "3.8"
services:
ubuntu_service:
image: ubuntu
command: sleep infinity
Let's break down this docker-compose.yml file:
version: '3.8'specifies the Compose file format version.services:defines the services for your application.ubuntu_service:is the name of our service. You can choose any name you like.image: ubuntuspecifies the Docker image to use for this service. In this case, we are using the officialubuntuimage. Since the image might not be present locally, Docker Compose will automatically pull it if needed.command: sleep infinityspecifies the command to run when the container starts.sleep infinityis a simple command that keeps the container running indefinitely.
After pasting the content, save the file by pressing Ctrl + X, then Y to confirm, and Enter to save to the default filename docker-compose.yml.
To ensure the ubuntu image is available, let's manually pull it using the docker pull command. This is a good practice to ensure the image is ready before starting the service with Docker Compose.
docker pull ubuntu
You should see output indicating that the ubuntu image is being pulled.
Start the services defined in the docker-compose.yml file
In this step, we will start the services defined in the docker-compose.yml file we created in the previous step. We will use the docker compose up command to achieve this.
Navigate to the directory where you saved the docker-compose.yml file, which is ~/project.
cd ~/project
Now, run the docker compose up command. The -d flag runs the containers in detached mode, meaning they will run in the background and not block your terminal.
docker compose up -d
You should see output indicating that the service is being created and started. Docker Compose will create a network, a volume (if defined, though not in our simple example), and the container for the ubuntu_service.
To verify that the container is running, you can use the docker ps command.
docker ps
You should see a container listed with the image ubuntu and the command sleep infinity. The status should be Up. The container name will be prefixed with the directory name (project in this case) and the service name (ubuntu_service).
Use docker compose top to display all running processes
In this step, we will use the docker compose top command to display the running processes within the containers managed by Docker Compose. This command is similar to the top command in Linux, but it shows the processes running inside the containers.
Make sure you are still in the ~/project directory where your docker-compose.yml file is located.
cd ~/project
Now, run the docker compose top command.
docker compose top
You should see output similar to the following:
ID NAME PID COMMAND ELAPSED
<container_id> project-ubuntu_service-1 <pid> sleep infinity <time>
This output shows the processes running inside each container managed by your docker-compose.yml file. In our case, we only have one service (ubuntu_service), and the only process running is sleep infinity, which is the command we specified in the docker-compose.yml file.
The output includes:
ID: The container ID.NAME: The name of the container, which is typically in the format<directory_name>-<service_name>-<instance_number>.PID: The process ID of the command running inside the container.COMMAND: The command being executed inside the container.ELAPSED: The time elapsed since the command started.
This command is useful for inspecting what is running inside your containers and troubleshooting issues.
Use docker compose top with service name to display specific processes
In this step, we will learn how to use the docker compose top command to display the processes of a specific service defined in your docker-compose.yml file. This is useful when you have multiple services running and you only want to inspect the processes of one of them.
Make sure you are still in the ~/project directory.
cd ~/project
To display the processes of a specific service, you append the service name to the docker compose top command. In our docker-compose.yml file, the service name is ubuntu_service.
docker compose top ubuntu_service
You should see output similar to the previous step, but this time it will only show the processes for the ubuntu_service container:
ID NAME PID COMMAND ELAPSED
<container_id> project-ubuntu_service-1 <pid> sleep infinity <time>
If you had other services defined in your docker-compose.yml file and they were running, running docker compose top without a service name would show processes for all services. By specifying ubuntu_service, we filter the output to only show the processes within the container for that specific service.
This targeted approach is helpful for debugging and monitoring individual components of your multi-container application.
Summary
In this lab, we learned how to use the docker compose top command to display running processes within Docker containers managed by Docker Compose. We began by preparing a simple docker-compose.yml file, which involved installing Docker Compose and defining a service using the ubuntu image with a sleep infinity command to keep the container running.
Following the preparation, we started the services defined in the docker-compose.yml file. Finally, we explored the usage of the docker compose top command, first to display all running processes across all services and then specifically for a single service by providing its name. This hands-on experience demonstrated how to inspect the processes running inside our Docker Compose-managed containers.



