CodeGym /Courses /Docker SELF /Debugging Applications in Docker Compose

Debugging Applications in Docker Compose

Docker SELF
Level 16 , Lesson 3
Available

9.1 Tools and Commands for Debugging

Docker Compose makes developing and deploying multi-container apps way easier, but just like with any technology, issues might happen that require debugging and fixing. In this lecture, we’re gonna check out the main approaches and tools for finding and fixing bugs in Docker Compose apps.

Main problems and ways to tackle them:

  1. Problems with starting containers:
    • Checking container logs.
    • Checking container status.
    • Checking config files.
  2. Network issues:
    • Inspecting network connections.
    • Using network utilities inside containers.
  3. Volume problems:
    • Checking volume mounts.
    • Inspecting volume permissions.

Tools and Commands for Debugging:

1. Checking Container Logs

Container logs are your go-to spot when issues pop up. The docker compose logs command lets you view logs for all services defined in the compose.yaml.

Terminal

docker compose logs

You can specify a particular service to view its logs:

Terminal

docker compose logs <service_name>

2. Checking Container Status

The docker compose ps command shows the status of all containers managed by Docker Compose.

Terminal

docker compose ps

The output of this command will show the state, ports, and container IDs.

3. Restarting Containers

If a container is acting up, try restarting it:

Terminal

docker compose restart <service_name>

4. Connecting to a Running Container

To dig into the issue further, you can connect to a running container using the docker compose exec command.

Terminal

docker compose exec <service_name> /bin/bash

This command will open a terminal inside the container, giving you access to run diagnostic commands.

5. Using Network Utilities

Testing network connections inside containers can help troubleshoot network issues. You can use tools like ping, curl, netcat, etc.

Terminal

docker compose exec <service_name> ping <hostname>

docker compose exec <service_name> curl http://<hostname>

docker compose exec <service_name> netcat -z -v <hostname> <port>

9.2 Advanced Techniques

Additional approaches for finding bugs

1. Checking the Docker Compose file

Errors in the compose.yaml file can lead to container malfunctions. Check the file syntax using this command:

Terminal

docker compose config

This command will check the file for errors and output the final configuration.

2. Using Healthcheck

You can define a healthcheck for services in compose.yaml so Docker can automatically monitor the container states.

Yaml

version: '3.8'

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3

3. Checking volume mounting

Issues with volumes may arise due to incorrect mounting or insufficient permissions. Make sure that volumes are mounted properly and the containers have access to them.

Yaml

version: '3.8'

services:
  db:
    image: postgres:latest
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  db-data:

3.1 Checking volume accessibility

To verify that the volume is accessible from a container, you can run a container and execute a command to check the volume (e.g., listing the files in the volume). Let's say the volume is mounted in the container at /mnt/volume:

Terminal

docker run --rm -v my_volume:/mnt/volume alpine ls /mnt/volume

If the volume is accessible, the ls command will output the list of files and directories in the volume. If the volume is inaccessible or empty, you'll get an appropriate message.

3.2 Checking available space in the volume

To check how much space is available on the volume from inside the container, use the df command:

Terminal

docker run --rm -v my_volume:/mnt/volume alpine df -h /mnt/volume

It shows the available space on the filesystem where the volume is mounted. The -h option outputs the information in a human-readable format (with units such as MB, GB, etc.).

4. Using IDE

To make debugging easier, use an IDE with debugging features and logging. You can create separate configuration files for development and production using the --file parameter.

Terminal

docker compose --file docker-compose.dev.yml up

Important! Here's a helpful guide on setting up remote debugging for an application running inside Docker using IntelliJ IDEA: Remote Debug Setup .

5. Checking environment variables

Incorrect or missing environment variables can cause problems. Make sure all necessary variables are defined in the .env file or directly in compose.yaml.

Yaml

version: '3.8'

services:
  app:
    image: myapp:latest
    environment:
      - APP_ENV=development
      - DEBUG=true

9.3 Examples of Debugging Commands

Examples of using debugging commands

Example 1: Checking and Fixing Database Connection Issues

Viewing the web service logs:

Terminal

docker compose logs web

Checking network connectivity between web and db:

Terminal

docker compose exec web ping db
docker compose exec web curl http://db:5432

Connecting to the web container for diagnostics:

Terminal

docker compose exec web /bin/bash

Example 2: Checking Volume Issues

Checking volume mounts:

Yaml

version: '3.8'

services:
  db:
    image: postgres:latest
    volumes:
      - db-data:/var/lib/postgresql/data
    
volumes:
  db-data:

Connecting to the db container and checking data:

Terminal

docker compose exec db /bin/bash
ls -l /var/lib/postgresql/data
3
Task
Docker SELF, level 16, lesson 3
Locked
Checking service logs
Checking service logs
3
Task
Docker SELF, level 16, lesson 3
Locked
Checking Network Connection
Checking Network Connection
3
Task
Docker SELF, level 16, lesson 3
Locked
Volume Mounting Check
Volume Mounting Check
3
Task
Docker SELF, level 16, lesson 3
Locked
Using Healthcheck
Using Healthcheck
1
Опрос
Scaling Services,  16 уровень,  3 лекция
недоступен
Scaling Services
Scaling Services
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION