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:
- Problems with starting containers:
- Checking container logs.
- Checking container status.
- Checking config files.
- Network issues:
- Inspecting network connections.
- Using network utilities inside containers.
- 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
.
docker compose logs
You can specify a particular service to view its logs:
docker compose logs <service_name>
2. Checking Container Status
The docker compose ps
command shows the status of all containers managed by Docker Compose.
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:
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.
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.
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:
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.
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.
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:
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:
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.
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
.
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:
docker compose logs web
Checking network connectivity between web
and db
:
docker compose exec web ping db
docker compose exec web curl http://db:5432
Connecting to the web
container for diagnostics:
docker compose exec web /bin/bash
Example 2: Checking Volume Issues
Checking volume mounts:
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:
docker compose exec db /bin/bash
ls -l /var/lib/postgresql/data
GO TO FULL VERSION