Sub topic 14
The same tools in Docker Compose
This chapter assumes you are comfortable with docker logs and docker exec from the earlier chapters. It also assumes you have used a compose.yaml file with docker compose up. Compose commands take a service name instead of a container name. They work on every container that belongs to the project in the current directory.
Reading logs by service
docker compose logs reads the same logs as docker logs. The options you already know carry over.
docker compose logs --follow --tail 100 web
docker compose logs --since 10m --timestampsThe first command follows the last 100 lines of the web service. The second shows the last ten minutes from every service in the project, with timestamps.
If you leave out the service name, Compose merges the output of all services into one stream. Each line starts with a prefix such as web-1 |, so you can tell the services apart. This is the main advantage over docker logs, because you can see a request reach one service and the error it causes in another.
Useful flags for processing the output:
--no-log-prefixremoves theweb-1 |prefix, which helps when you pipe the output into a parser.--no-colorremoves color codes that would confusegrepor a file.--untilshows entries older than a given time, to pair with--since.
Compose does not separate stdout from stderr the way docker logs does. It merges both into its own output. If you need the streams apart, use docker logs on the container name that docker compose ps reports.
Running commands by service
docker compose exec starts a process in a running container of the service.
docker compose exec web nginx -t
docker compose exec web sh
docker compose exec -u root -w /etc/nginx -e DEBUG=1 web envThe options -u, -w, -e, and -d work as they do in docker exec. The exit code of your command is also the exit code of docker compose exec, so scripts can still test $?.
There is one difference that matters in scripts. docker compose exec allocates a terminal by default, so you do not pass -i and -t. A terminal breaks piping and non-interactive jobs such as cron and CI. Disable it with -T:
docker compose exec -T web cat /etc/nginx/nginx.conf > nginx.conf.backupWithout -T, you may see the error "the input device is not a TTY", or the saved output may contain carriage returns.
If a service runs several replicas, exec picks the first one. Choose another with --index:
docker compose exec --index 2 web shConfiguring log rotation in the file
You can set the logging driver and its limits per service in compose.yaml, instead of repeating --log-opt flags.
services:
web:
image: nginx
ports:
- "8080:80"
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"This is the Compose form of the rotation setting from the earlier chapter. Changing it requires recreating the container, which docker compose up -d does when it detects the change.
Common errors
- Running
execon a service that is stopped fails with an error that the service is not running. Check withdocker compose ps, then read the logs of the stopped container, which still exist. - Running
docker composein the wrong directory targets the wrong project or none. Use-p nameor-f fileto select one explicitly. docker compose run web shstarts a new container, asdocker rundoes. It does not enter the one that is already running. Useexecto inspect the live container.
Exercise
A CI job runs docker compose exec web cat /app/version.txt > version.txt, and the saved file ends each line with an extra carriage return. What is the cause, and what is the fix?
Show answer
Compose allocates a terminal by default, and a terminal adds carriage returns to the output. Add -T to disable it: docker compose exec -T web cat /app/version.txt > version.txt.