Skip to content

Instantly share code, notes, and snippets.

@remilapeyre
Created January 22, 2019 15:34
Show Gist options
  • Save remilapeyre/9423750e66c9bffc1d510dac45f070c6 to your computer and use it in GitHub Desktop.
Save remilapeyre/9423750e66c9bffc1d510dac45f070c6 to your computer and use it in GitHub Desktop.

Detached Containers and Logging

Running a Container in the Background

  1. First try running a container as usual; the STDOUT and STDERR streams from whatever is PID 1 inside the container are directed to the terminal:
$ docker container run centos:7 ping 127.0.0.1 -c 2
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.021 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.029 ms
--- 127.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1019ms
rtt min/avg/max/mdev = 0.021/0.025/0.029/0.004 ms
  1. The same process can be run in the background with the -d flag:
$ docker container run -d centos:7 ping 127.0.0.1
d5ef517cc113f36738005295066b271ae604e9552ce4070caffbacdc3893ae04

This time, we only see the container’s ID; its STDOUT isn’t being sent to the terminal.

  1. Use this second container’s ID to inspect the logs it generated.
$ docker container logs <container ID>

These logs correspond to STDOUT and STDERR from the container’s PID 1. Also note when using container IDs: you don’t need to specify the entire ID. Just enough characters from the start of the ID to uniquely identify it, often just 2 or 3, is sufficient.

Attaching to Container Output

  1. We can attach a terminal to a container’s PID 1 output with the attach command; try it with the last container you made in the previous step:
$ docker container attach <container ID>
  1. We can leave attached mode by then pressing CTRL+C. After doing so, list your running containers; you should see that the container you attached to has been killed, since the CTRL+C issued killed PID 1 in the container, and therefore the container itself.

  2. Try running the same thing in detached interactive mode:

$ docker container run -d -it centos:7 ping 127.0.0.1

Attach to this container like you did the rst one, but this time detach with CTRL+P CTRL+Q (sequential, not simultaneous), and list your running containers. In this case, the container should still be happily running in the background after detaching from it.

Using Logging Options

  1. We saw previously how to read the entire log of a container’s PID 1; we can also use a couple of flags to control what logs are displayed. --tail n limits the display to the last n lines; try it with the container that should be running from the last step:
$ docker container logs --tail 5 <container ID>

You should see the last 5 pings from this container.

  1. We can also follow the logs as they are generated with -f:
$ docker container logs -f <container ID>

The container’s logs get piped in real time to the terminal (CTRL+C to break out of following mode - note this doesn’t kill the process like when we attached to it, since now we’re tailing the logs, not attaching to the process).

  1. Finally, try combining the tail and follow flags to begin following the logs from a point further back in history.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment