WeiChing 林煒清
WeiChing 林煒清

Reputation: 4469

How to use docker logs

The question may be a bit of newbie. I run docker exec -it mycontainer bash to enter into a daemon container(postgresSQL ), and echo something. now I exit it , and use docker logs mycontainer so as to see my echos.

According to

The docker logs command batch-retrieves logs present at the time of execution. The docker logs --follow command will continue streaming the new output from the container's STDOUT and STDERR.

The docker logs listen STDOUT of the container, why I don't see my string just echoed inside it?

Upvotes: 1

Views: 2697

Answers (2)

Fabien Balageas
Fabien Balageas

Reputation: 634

Docker engine only stores stdout from process with ID 0 (i.e. the process launched by the CMD directive of your Dockerfile).

By the way, on your Docker host, you can inspect the content of container's logs by viewing the file /var/lib/docker/containers/<ID of your container>/<ID of your container>-json.log.

This file stores logs in JSON format.

Upvotes: 4

Adrian Mouat
Adrian Mouat

Reputation: 46480

I assume logging only occurs for the main process in a container. As exec creates a new process, it won't get logged.

Note that docker logs works for processes given in the run command e.g:

$ ID=$(docker run -d debian sh -c "while true; do echo "hello"; sleep 1; done;")
$ docker logs $ID
hello
hello
hello
hello
hello

Upvotes: 1

Related Questions