Guest User

Untitled

a guest
Jan 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. # Detached Containers and Logging
  2.  
  3. ## Running a Container in the Background
  4.  
  5. 1. First try running a container as usual; the STDOUT and STDERR streams
  6. from whatever is PID 1 inside the container are directed to the terminal:
  7.  
  8. ```
  9. $ docker container run centos:7 ping 127.0.0.1 -c 2
  10. PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
  11. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.021 ms
  12. 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.029 ms
  13. --- 127.0.0.1 ping statistics ---
  14. 2 packets transmitted, 2 received, 0% packet loss, time 1019ms
  15. rtt min/avg/max/mdev = 0.021/0.025/0.029/0.004 ms
  16. ```
  17.  
  18. 2. The same process can be run in the background with the `-d` flag:
  19.  
  20. ```
  21. $ docker container run -d centos:7 ping 127.0.0.1
  22. d5ef517cc113f36738005295066b271ae604e9552ce4070caffbacdc3893ae04
  23. ```
  24. This time, we only see the container’s ID; its STDOUT isn’t being sent to the terminal.
  25.  
  26. 3. Use this second container’s ID to inspect the logs it generated.
  27. ```
  28. $ docker container logs <container ID>
  29. ```
  30.  
  31. These logs correspond to STDOUT and STDERR from the container’s PID 1. Also
  32. note when using container IDs: you don’t need to specify the entire ID.
  33. Just enough characters from the start of the ID to uniquely identify it,
  34. often just 2 or 3, is sufficient.
  35.  
  36. ## Attaching to Container Output
  37.  
  38. 1. We can attach a terminal to a container’s PID 1 output with the `attach` command;
  39. try it with the last container you made in the previous step:
  40.  
  41. ```
  42. $ docker container attach <container ID>
  43. ```
  44.  
  45. 2. We can leave attached mode by then pressing CTRL+C. After doing so,
  46. list your running containers; you should see that the container you attached
  47. to has been killed, since the CTRL+C issued killed PID 1 in the container,
  48. and therefore the container itself.
  49.  
  50. 3. Try running the same thing in detached interactive mode:
  51.  
  52. ```
  53. $ docker container run -d -it centos:7 ping 127.0.0.1
  54. ```
  55. Attach to this container like you did the rst one, but this time detach with
  56. `CTRL+P` `CTRL+Q` (sequential, not simultaneous), and list your running containers.
  57. In this case, the container should still be happily running in the background
  58. after detaching from it.
  59.  
  60. ## Using Logging Options
  61.  
  62. 1. We saw previously how to read the entire log of a container’s PID 1; we can
  63. also use a couple of flags to control what logs are displayed. `--tail n` limits
  64. the display to the last n lines; try it with the container that should be running
  65. from the last step:
  66.  
  67. ```
  68. $ docker container logs --tail 5 <container ID>
  69. ```
  70.  
  71. You should see the last 5 pings from this container.
  72.  
  73. 2. We can also follow the logs as they are generated with `-f`:
  74.  
  75. ```
  76. $ docker container logs -f <container ID>
  77. ```
  78.  
  79. The container’s logs get piped in real time to the terminal (`CTRL+C` to break out
  80. of following mode - note this doesn’t kill the process like when we attached to
  81. it, since now we’re tailing the logs, not attaching to the process).
  82.  
  83. 3. Finally, try combining the tail and follow flags to begin following the logs
  84. from a point further back in history.
Add Comment
Please, Sign In to add comment