Senger CodeLab 🚀

How to view log output using docker-compose run

September 29, 2026

📂 Categories: Docker
🏷 Tags: Docker-Compose
How to view log output using docker-compose run

Debugging containerized applications can sometimes feel like searching for a needle in a haystack. When you’re working with Docker Compose, understanding how to effectively view log output becomes crucial for troubleshooting and monitoring your services. This guide focuses on how to view log output using docker-compose run, a powerful technique that allows you to execute commands in your application’s environment and inspect the resulting logs. Whether you’re a seasoned developer or just starting with Docker Compose, mastering this skill will significantly enhance your ability to diagnose issues and ensure your applications run smoothly. Learning to interpret these logs is a key skill in modern DevOps practices, ensuring rapid identification and resolution of problems, ultimately leading to more reliable and efficient software deployment. By the end of this article, you’ll have a solid understanding of how to use docker-compose run to access and analyze your application’s logs, enabling you to tackle even the most complex debugging scenarios.

Understanding Docker Compose Run and Its Benefits

The docker-compose run command is a valuable tool within the Docker Compose ecosystem. It allows you to execute a one-off command within the environment of a defined service in your docker-compose.yml file. This means you can run commands like debugging tools, database migrations, or, most importantly, inspect log output directly within the context of your application’s containers. Unlike docker-compose up, which starts and runs your entire application stack, docker-compose run executes a single command and then exits, making it perfect for targeted tasks like viewing specific log entries or testing isolated functionalities.

One of the primary benefits of using docker-compose run for viewing logs is that it provides access to the application’s environment variables and dependencies as defined in your Compose file. This ensures that the logs you’re examining are generated under the same conditions as the running application, providing accurate and reliable insights. Furthermore, it simplifies the process of accessing logs from services that might not expose them directly through external ports or volumes. According to Docker’s official documentation, “docker-compose run is designed for executing one-off commands against a service” Docker Documentation. This makes it an ideal choice for tasks like log analysis, where you need to execute a command specifically for that purpose.

Imagine you’re troubleshooting an issue with your web application. Instead of trying to SSH into the container or configuring complex logging drivers, you can simply use docker-compose run to execute a command that streams the application’s logs directly to your terminal. This streamlined approach saves time and effort, allowing you to focus on identifying and resolving the root cause of the problem. Another benefit is the isolation it provides; the command runs in its own container, minimizing the risk of interfering with other running services.

Practical Steps to View Log Output Using Docker Compose Run

To effectively view log output using docker-compose run, follow these steps. This method allows you to pinpoint issues by executing commands within your application’s environment and inspecting the resulting logs.

  1. Navigate to Your Project Directory: Open your terminal and change the directory to the location of your docker-compose.yml file. This is crucial because Docker Compose needs to read the configuration file to understand your application’s services.
  2. Execute the docker-compose run Command: Use the following command structure: docker-compose run [service_name] [command]. Replace [service_name] with the name of the service you want to inspect (as defined in your docker-compose.yml file), and [command] with the command to execute. For example, to view the logs of a web application service named “web,” you might use: docker-compose run web tail -f /var/log/nginx/access.log.
  3. Interpret the Log Output: The command will execute, and the log output will stream to your terminal. Analyze the logs to identify any errors, warnings, or relevant information that can help you diagnose the issue. Use tools like grep to filter the output for specific keywords or patterns.
  4. Clean Up (Optional): After you’ve finished analyzing the logs, you can stop the container created by docker-compose run. However, in most cases, it will stop automatically after the command completes.

For example, let’s say you want to check the logs of a database service named “db” for any connection errors. You could use the command docker-compose run db tail -f /var/log/mysql/error.log. This will stream the MySQL error log to your terminal, allowing you to monitor for any connection-related issues. This is a powerful way to proactively identify and address potential problems before they impact your application’s performance.

Using docker-compose run provides a focused and efficient way to access and analyze logs within your Docker Compose environment. It allows you to execute specific commands tailored to your debugging needs, providing valuable insights into your application’s behavior. This technique is particularly useful when dealing with complex microservices architectures where logs are distributed across multiple containers.

Advanced Techniques for Log Analysis with Docker Compose Run

Beyond the basic usage, several advanced techniques can further enhance your ability to analyze logs using docker-compose run. These techniques involve leveraging tools and commands to filter, format, and aggregate log data for more efficient troubleshooting. One powerful approach is to combine docker-compose run with command-line utilities like grep, awk, and sed to extract specific information from the log output. For example, you can use grep to filter logs for specific error messages or keywords, allowing you to quickly identify the root cause of an issue.

Consider the following example: docker-compose run web tail -f /var/log/nginx/error.log | grep "500 Internal Server Error". This command streams the Nginx error log and filters it to show only lines containing “500 Internal Server Error.” This allows you to focus on the most critical errors and ignore irrelevant log entries. Another useful technique is to use awk to format the log output into a more readable format. For instance, you can extract specific fields from the log entries and display them in a tabular format. According to a Stack Overflow discussion, combining docker-compose run with grep is a common practice for debugging Stack Overflow.

Furthermore, you can integrate docker-compose run with external logging services like Elasticsearch and Kibana to aggregate and visualize logs from multiple containers in a centralized location. This allows you to gain a holistic view of your application’s behavior and identify patterns that might be difficult to spot when analyzing individual log files. The integration typically involves configuring your containers to forward their logs to a central logging server, which then indexes and stores the data for analysis. This centralized logging approach is particularly beneficial for large-scale deployments with numerous microservices. Another method is to utilize tools like Fluentd or Logstash to collect, process, and forward logs to various destinations.

Troubleshooting Common Issues

While viewing log output using docker-compose run is generally straightforward, you might encounter some common issues. One frequent problem is incorrect file paths in the command. Ensure that the path to the log file specified in the docker-compose run command is correct and accessible within the container. If the path is incorrect, you’ll likely see an error message indicating that the file does not exist. To resolve this, double-check the file path within the container and update the command accordingly.

Another common issue is related to permissions. The user running the command might not have the necessary permissions to access the log file within the container. This can be resolved by adjusting the file permissions within the container or running the docker-compose run command with a user that has the required permissions. For example, you can use the --user flag to specify a different user when executing the command. For example: docker-compose run --user root web tail -f /var/log/nginx/error.log. This runs the command as the root user, which typically has access to all files within the container.

Sometimes, you might not see any log output even after executing the command. This could be due to several reasons, such as the application not generating any logs, the logs being written to a different location, or the logging level being set too low. To troubleshoot this, verify that the application is configured to generate logs and that the logging level is set appropriately. You can also check the application’s documentation or configuration files to determine where the logs are being written. Debugging Docker containers can be complex, but tools like docker-compose run make it easier. According to New Relic, effective monitoring is crucial for containerized environments New Relic.

Here’s a paragraph optimized for a featured snippet:

To view real-time logs in Docker Compose, use the command docker-compose logs -f [service_name]. Replace [service_name] with the name of the service from your docker-compose.yml file. The -f flag (follow) ensures that new log entries are streamed to your terminal as they are generated, providing a live view of your application’s activity. This is particularly useful for debugging and monitoring purposes, allowing you to quickly identify and address any issues that arise.

  • Verify file paths within containers.

  • Check and adjust file permissions.

  • Use grep for targeted log filtering.

  • Integrate with external logging services.

Infographic here
FAQ ---
What is the difference between docker-compose up and docker-compose run?
`docker-compose up` starts and runs all the services defined in your docker-compose.yml file, typically for long-running applications. `docker-compose run` executes a single command in the environment of a specified service and then exits.
Can I use docker-compose run to execute multiple commands?
No, `docker-compose run` is designed for executing a single command. If you need to execute multiple commands, you can combine them into a single command using shell scripting or create a custom entry point for the service.
How do I access the logs of a service that doesn't have a log file?
If a service doesn't write to a log file, you can redirect its standard output and standard error streams to a file using Docker's logging drivers. Alternatively, you can use `docker-compose exec` to access the container's shell and inspect the application's output directly.
Hopefully, this guide has illuminated the path to effectively **view log output using docker-compose run**. Remember, mastering this technique is not just about reading logs; it's about gaining deeper insights into your application's behavior and becoming a more proficient problem-solver. By leveraging the power of docker-compose run and combining it with other command-line tools and logging services, you can significantly enhance your ability to diagnose issues, optimize performance, and ensure the reliability of your containerized applications. Don't hesitate to experiment with different commands and techniques to find what works best for your specific use case. And consider further exploring topics such as Docker Compose best practices and container orchestration strategies to continue expanding your knowledge and skills. Also, remember to check out this resource on [container security](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c) to learn more.

Question & Answer :
When I use docker-compose up I can see logs for all containers in my docker-compose.yml file.

However, when I use docker-compose run app I only see console output for app but none of the services that app depends on. How can see log output for the other services?

At the time of writing this the docker-compose run command does not provide a switch to see the logs of other services, hence you need to use the docker-compose logs command to see the logs you want.

Update June 10th 2022

As commented by @Sandburg docker compose is now integrated into docker. As described here most of the docker compose commands can be called the same way just without the hyphen. So docker compose instead of docker-compose:

The new Compose V2, which supports the compose command as part of the Docker CLI, is now available.

Compose V2 integrates compose functions into the Docker platform, continuing to support most of the previous docker-compose features and flags. You can run Compose V2 by replacing the hyphen (-) with a space, using docker compose, instead of docker-compose.

Update July 1st 2019

docker-compose logs <name-of-service> 

for all services

docker-compose logs 

Use the following options from the documentation:

Usage: logs [options] [SERVICE…]

Options:

--no-color Produce monochrome output.

-f, –follow Follow log output.

-t, –timestamps Show timestamps.

--tail=“all” Number of lines to show from the end of the logs for each container.

See docker logs

You can start Docker compose in detached mode and attach yourself to the logs of all container later. If you’re done watching logs you can detach yourself from the logs output without shutting down your services.

  1. Use docker-compose up -d to start all services in detached mode (-d) (you won’t see any logs in detached mode)
  2. Use docker-compose logs -f -t to attach yourself to the logs of all running services, whereas -f means you follow the log output and the -t option gives you timestamps (See Docker reference)
  3. Use Ctrl + z or Ctrl + c to detach yourself from the log output without shutting down your running containers

If you’re interested in logs of a single container you can use the docker keyword instead:

  1. Use docker logs -t -f <name-of-service>

Save the output

To save the output to a file you add the following to your logs command:

  1. docker-compose logs -f -t >> myDockerCompose.log