How to view log output using docker-compose run?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Docker Compose is a tool used for defining and running multi-container Docker applications. While working with Docker Compose, developers often need to view the log outputs of the services to debug issues or verify that services are running correctly. Especially when using docker-compose run, understanding how to view logs can be invaluable for troubleshooting.
Understanding docker-compose run
docker-compose run is used to start a one-time command in a service container. When you invoke this command, it creates a new container for the specified service, executes a command, and then exits. Unlike docker-compose up, which starts all defined services and tailors them into the background or the foreground, docker-compose run will not carry over the configured behavior of up regarding logging unless explicitly managed.
Basic Command Structure
The basic syntax for docker-compose run is:
<service>: Specifies the service in thedocker-compose.ymlfile.[command]: An optional command override that runs instead of the default command.
Viewing Logs with docker-compose run
When using docker-compose run, capturing log outputs can be different compared to docker-compose up. By default, docker-compose run outputs the results of executed commands directly to the terminal. However, if you want to ensure continuous logging or need to analyze logs post execution, certain techniques and compositional practices can help.
Example Usage
Consider a docker-compose.yml file with the following service:
To use docker-compose run and view the log immediately, you might execute:
This command will run the python app.py command in the app service and print the log output directly to your terminal.
Tips for Handling Logs
- Direct Terminal Output: The default behavior is to direct all output to your terminal. This is useful for quick, interactive sessions.
- Log to Files: If you need a permanent record, redirect output to a file:
- Set Log Driver: Configure Docker's logging driver to manage how log messages are handled and where they're stored. This can be declared in the
docker-compose.yml:
- Combining with
docker-compose logs: After running a command that detaches (-doption), usedocker-compose logsto see log output:
- Interactive Mode: For interactive use, the
-Toption prevents a pseudo-TTY allocation which might be desirable for script captures or batch processes.
Troubleshooting Tips
- Ensure that your
docker-compose.ymlhas correctly mapped volumes, especially if your application logs to a file inside the container. - Always check the configuration of your logging driver; misconfigurations can lead to no logs being captured.
- Use the
--rmflag indocker-compose runto automatically clean up the container after it exits to prevent clutter, which can also prevent log collection for post-mortem analysis.
Summary Table
| Task | Command/Options | Notes |
| Run service and view log | docker-compose run app | Direct output to terminal |
| Log to a file | docker-compose run app > app-log.txt | Redirects all terminal output to a specified file |
Use log driver (e.g., json-file) | Configure in docker-compose.yml | Allows for structured and managed logging |
| Access logs after service run | docker-compose logs app | Access logs stored during the runtime of docker-compose containers |
| Non-interactive run | docker-compose run -T app | Useful for non-terminal environments or script-based executions |
| Clean up on exit | docker-compose run --rm app | Prevents container build-up post-execution |
Conclusion
Viewing log output using docker-compose run is straightforward when you understand the basic concepts and best practices associated with Docker's logging mechanism. By leveraging these tools and options, you can effectively monitor your services and ensure that your application runs optimally.
Related reading
- How to write a kubernetes pod configuration to start two containers
- How to write data to host file system from Docker container
- How to yank to host clipboard from inside a Docker container?
- How upgrade a docker image without creating new image?
- How to view logs of failed jobs with kubectl?
- How to view the SQL queries issued by JPA?
- Hyper-v and VirtualBox conflict in Dockers with Minikube
- I am using Azure Devops to build and push my Docker image. How can I pass arguments while doing buildAndPush using Docker task?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.