How to view log output using docker-compose run?

2019-02-02 20:25发布

问题:

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?

回答1:

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 <container-name>

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


回答2:

If you want to see output logs from multiple containers. Eg. say you have API container and portal container then you can do something like below :

docker-compose logs -t -f --tail 5 portal api

Where 5 represents last 5 lines from both logs.



回答3:

  1. use the command to start containers in detached mode. : docker-compose up -d
  2. to view the containers : use: docker ps
  3. to view logs for a container :
    docker logs