จะตรวจสอบบันทึกของ Docker ได้อย่างไร

เผยแพร่แล้ว: 2021-08-30

Docker ซึ่งเป็นแพลตฟอร์มเวอร์ชวลไลเซชันระดับ OS ยอดนิยมที่ช่วยให้เราสามารถส่งมอบแอปพลิเคชันในแพ็คเกจที่เรียกว่าคอนเทนเนอร์ ซึ่งในแง่ง่ายๆ คือสภาพแวดล้อมที่แยกจากกันด้วยซอฟต์แวร์ ไลบรารี และไฟล์การกำหนดค่าของตนเอง

เช่นเดียวกับซอฟต์แวร์สมัยใหม่อื่นๆ การบันทึกเหตุการณ์และข้อความ เช่น คำเตือนและข้อผิดพลาด เป็นส่วนหนึ่งของแพลตฟอร์ม Docker ซึ่งช่วยให้คุณแก้ไขจุดบกพร่องของแอปพลิเคชันและปัญหาในการผลิตได้

เราจะพูดถึงวิธีง่ายๆ บางอย่างที่คุณสามารถจัดการและตรวจสอบบันทึกสำหรับคอนเทนเนอร์ของคุณได้ มาเริ่มกันเลยดีกว่า

คำสั่ง Docker Logs

ไวยากรณ์พื้นฐานในการดึงบันทึกของคอนเทนเนอร์คือ:

 $ docker logs [OPTIONS] <CONTAINER-NAME OR ID>

หรือ

 $ docker container logs [OPTIONS] <CONTAINER-NAME OR ID>

โดยพื้นฐานแล้วไวยากรณ์ทั้งสองจะเหมือนกัน ดังนั้นเราจะเน้นที่คำสั่งที่เหลือในบทความนี้ในฐานะ docker logs

แม้ว่าโปรดทราบว่าคำสั่งดังกล่าวใช้ได้เฉพาะกับคอนเทนเนอร์ที่เริ่มต้นด้วย json-file หรือไดรเวอร์การบันทึก journald

OPTIONS อ้างถึงแฟล็กที่รองรับพร้อมคำสั่ง docker logs ซึ่งแสดงอยู่ด้านล่าง:

ชื่อ ชวเลข ค่าเริ่มต้น คำอธิบาย
--details แสดงรายละเอียดเพิ่มเติมที่ให้ไว้ในบันทึก
--follow , -f ติดตามการส่งออกบันทึก
--since แสดงบันทึกตั้งแต่ประทับเวลา (เช่น 2021-08-28T15:23:37Z) หรือแบบสัมพัทธ์ (เช่น 56 นาทีเป็นเวลา 56 นาที)
--tail , -n all จำนวนบรรทัดที่จะแสดงต่อท้ายบันทึก
--timestamps, -t แสดงการประทับเวลา
--until API 1.35+
แสดงบันทึกก่อนการประทับเวลา (เช่น 2021-08-28T15:23:37Z) หรือแบบสัมพัทธ์ (เช่น 56 นาทีเป็นเวลา 56 นาที)

ตัวอย่าง:

 $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 28913415ed22 nginx "/docker-entrypoint.…" 2 seconds ago Up 1 second 80/tcp gifted_edison $ docker logs 28913415ed22 /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2021/08/28 09:02:59 [notice] 1#1: using the "epoll" event method 2021/08/28 09:02:59 [notice] 1#1: nginx/1.21.1 2021/08/28 09:02:59 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 2021/08/28 09:02:59 [notice] 1#1: OS: Linux 5.8.0-1039-azure 2021/08/28 09:02:59 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2021/08/28 09:02:59 [notice] 1#1: start worker processes 2021/08/28 09:02:59 [notice] 1#1: start worker process 31 2021/08/28 09:02:59 [notice] 1#1: start worker process 32 $

ตำแหน่งบันทึกนักเทียบท่า

ตามค่าเริ่มต้น Docker จะจับเอาต์พุตมาตรฐาน (และข้อผิดพลาดมาตรฐาน) ของคอนเทนเนอร์ทั้งหมดของคุณและเขียนลงในไฟล์โดยใช้รูปแบบ JSON ซึ่งทำได้โดยใช้ไดรเวอร์การบันทึกไฟล์ JSON หรือไฟล์ json โดยค่าเริ่มต้น บันทึกเหล่านี้จะถูกเก็บไว้ที่ตำแหน่งเฉพาะคอนเทนเนอร์ภายใต้ /var/lib/docker filesystem

 /var/lib/docker/containers/<container_id>/<container_id>-json.log

ตัวอย่างเช่น สำหรับคอนเทนเนอร์ redis ของฉันที่แสดงด้านล่าง ฉันสามารถตรวจสอบ json ไฟล์ json ตามที่แสดงในตัวอย่างด้านล่าง:

 $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 551c9273bbea redis "docker-entrypoint.s…" 19 minutes ago Up 19 minutes 6379/tcp redis 6cc871763df1 nginx "/docker-entrypoint.…" 7 hours ago Up 7 hours 0.0.0.0:8080->80/tcp, :::8080->80/tcp nostalgic_wescoff $ sudo ls -l /var/lib/docker/containers/551c9273bbea6eaf66523ed735866b9ebe6924c3b504dfeb44bef90e69d59c73/551c9273bbea6eaf66523ed735866b9ebe6924c3b504dfeb44bef90e69d59c73-json.log -rw-r----- 1 root root 1437 Aug 28 16:53 /var/lib/docker/containers/551c9273bbea6eaf66523ed735866b9ebe6924c3b504dfeb44bef90e69d59c73/551c9273bbea6eaf66523ed735866b9ebe6924c3b504dfeb44bef90e69d59c73-json.log $ sudo tail -10 /var/lib/docker/containers/551c9273bbea6eaf66523ed735866b9ebe6924c3b504dfeb44bef90e69d59c73/551c9273bbea6eaf66523ed735866b9ebe6924c3b504dfeb44bef90e69d59c73-json.log {"log":"1:C 28 Aug 2021 16:53:42.160 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo\n","stream":"stdout","time":"2021-08-28T16:53:42.16031257Z"} {"log":"1:C 28 Aug 2021 16:53:42.160 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=1, just started\n","stream":"stdout","time":"2021-08-28T16:53:42.160337871Z"} {"log":"1:C 28 Aug 2021 16:53:42.160 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf\n","stream":"stdout","time":"2021-08-28T16:53:42.160342171Z"} {"log":"1:M 28 Aug 2021 16:53:42.160 * monotonic clock: POSIX clock_gettime\n","stream":"stdout","time":"2021-08-28T16:53:42.160792578Z"} {"log":"1:M 28 Aug 2021 16:53:42.161 * Running mode=standalone, port=6379.\n","stream":"stdout","time":"2021-08-28T16:53:42.161148683Z"} {"log":"1:M 28 Aug 2021 16:53:42.161 # Server initialized\n","stream":"stdout","time":"2021-08-28T16:53:42.161170984Z"} {"log":"1:M 28 Aug 2021 16:53:42.161 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.\n","stream":"stdout","time":"2021-08-28T16:53:42.161186984Z"} {"log":"1:M 28 Aug 2021 16:53:42.161 * Ready to accept connections\n","stream":"stdout","time":"2021-08-28T16:53:42.161484389Z"} $

แสดงรายละเอียดเพิ่มเติม

หากต้องการแสดงรายละเอียดเพิ่มเติมในบันทึก ให้ใช้แฟ --details

ตัวอย่าง:

 $ docker logs 6cc871763df1 --details /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2021/08/28 10:29:05 [notice] 1#1: using the "epoll" event method 2021/08/28 10:29:05 [notice] 1#1: nginx/1.21.1 2021/08/28 10:29:05 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 2021/08/28 10:29:05 [notice] 1#1: OS: Linux 5.8.0-1039-azure 2021/08/28 10:29:05 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2021/08/28 10:29:05 [notice] 1#1: start worker processes 2021/08/28 10:29:05 [notice] 1#1: start worker process 33 2021/08/28 10:29:05 [notice] 1#1: start worker process 34 172.17.0.1 - - [28/Aug/2021:10:29:26 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" $

ติดตามล็อกเอาท์พุต

คุณสามารถใช้ --follow หรือ -f เพื่อติดตามเอาต์พุตบันทึก ซึ่งช่วยให้คุณสามารถตรวจสอบการอัปเดตใหม่ในสตรีมบันทึกจาก STDOUT และ STDERR อย่างต่อเนื่อง

ตัวอย่าง:

 $ docker logs 6cc871763df1 -f /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2021/08/28 10:29:05 [notice] 1#1: using the "epoll" event method 2021/08/28 10:29:05 [notice] 1#1: nginx/1.21.1 2021/08/28 10:29:05 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 2021/08/28 10:29:05 [notice] 1#1: OS: Linux 5.8.0-1039-azure 2021/08/28 10:29:05 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2021/08/28 10:29:05 [notice] 1#1: start worker processes 2021/08/28 10:29:05 [notice] 1#1: start worker process 33 2021/08/28 10:29:05 [notice] 1#1: start worker process 34 172.17.0.1 - - [28/Aug/2021:10:29:26 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-"

บันทึกหาง

บันทึกคอนเทนเนอร์สามารถปรับได้เพื่อจำกัดจำนวนเอาต์พุตที่แสดงบนหน้าจอด้วย --tail หรือ -n โดยค่าเริ่มต้น แฟล็กนี้จะถือว่า all เป็นอาร์กิวเมนต์ที่แสดงสตรีมบันทึกทั้งหมด หากต้องการแสดงจำนวนบรรทัดคงที่จากส่วนท้ายของบันทึก ให้ระบุจำนวนเต็มบวกตาม --tail หรือ -n

ตัวอย่าง:

 $ docker logs 6cc871763df1 -n 10 /docker-entrypoint.sh: Configuration complete; ready for start up 2021/08/28 10:29:05 [notice] 1#1: using the "epoll" event method 2021/08/28 10:29:05 [notice] 1#1: nginx/1.21.1 2021/08/28 10:29:05 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 2021/08/28 10:29:05 [notice] 1#1: OS: Linux 5.8.0-1039-azure 2021/08/28 10:29:05 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2021/08/28 10:29:05 [notice] 1#1: start worker processes 2021/08/28 10:29:05 [notice] 1#1: start worker process 33 2021/08/28 10:29:05 [notice] 1#1: start worker process 34 172.17.0.1 - - [28/Aug/2021:10:29:26 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" $

แสดงบันทึกตั้งแต่

เราสามารถจำกัดเอาต์พุตบันทึกได้โดยใช้ --since flag และให้การประทับเวลาเป็นค่าสัมบูรณ์ด้วยไวยากรณ์ 2021-08-28T15:23:37Z หรือค่าที่สัมพันธ์กัน เช่น 56m เป็นเวลา 56 นาที

ตัวเลือก --since จะแสดงเฉพาะบันทึกคอนเทนเนอร์ที่สร้างขึ้นหลังจากวันที่กำหนด คุณสามารถระบุวันที่เป็นวันที่ RFC 3339 การประทับเวลา UNIX หรือสตริงระยะเวลา Go (เช่น 1m30s , 3h ) ระบบจะใช้โซนเวลาท้องถิ่นในไคลเอนต์หากคุณไม่ได้ระบุโซนเวลา Z หรือ +-00:00 ที่ส่วนท้ายของการประทับเวลา คุณสามารถรวม --since ตัวเลือกกับตัวเลือก --follow หรือ --tail อย่างใดอย่างหนึ่งหรือทั้งสองอย่าง

ตัวอย่าง:

 $ docker logs --since=1m nostalgic_wescoff 172.17.0.1 - - [28/Aug/2021:15:19:24 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" 172.17.0.1 - - [28/Aug/2021:15:19:25 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" $

ในตัวอย่างข้างต้น บันทึกตั้งแต่ 2 นาทีจะแสดงขึ้นโดยที่ nostalgic_wescoff เป็นชื่อที่สร้างขึ้นโดยอัตโนมัติซึ่งกำหนดให้กับคอนเทนเนอร์ nginx

แสดงบันทึกจนถึง

เช่นเดียวกับ -- --since ตั้งค่าสถานะ docker logs ยังรองรับ -- --until ตั้งค่าสถานะ ซึ่งแสดงบันทึกก่อนการประทับเวลาที่กำหนด ในทำนองเดียวกันการประทับเวลาดังต่อไปนี้การประชุมที่คล้ายกันก่อนหน้านี้และสามารถระบุเป็นค่าแน่นอนกับไวยากรณ์ 2021-08-28T15:23:37Z หรือญาติเช่น 56m 56 นาที

ตัวอย่าง:

 $ docker logs --until=1h30m nostalgic_wescoff /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2021/08/28 10:29:05 [notice] 1#1: using the "epoll" event method 2021/08/28 10:29:05 [notice] 1#1: nginx/1.21.1 2021/08/28 10:29:05 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 2021/08/28 10:29:05 [notice] 1#1: OS: Linux 5.8.0-1039-azure 2021/08/28 10:29:05 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2021/08/28 10:29:05 [notice] 1#1: start worker processes 2021/08/28 10:29:05 [notice] 1#1: start worker process 33 2021/08/28 10:29:05 [notice] 1#1: start worker process 34 172.17.0.1 - - [28/Aug/2021:10:29:26 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" $

ในตัวอย่างข้างต้น บันทึกทั้งหมดก่อน 1 ชั่วโมง 30 นาทีจะแสดงขึ้น

แสดงการประทับเวลา

แอปพลิเคชั่นคอนเทนเนอร์จำนวนมากเสนอการประทับเวลาในตัวเอาต์พุตบันทึก ดังนั้น Docker จึงแสดงด้วยคำสั่ง docker logs หากคุณต้องการให้ Docker นำหน้าการประทับเวลาในเอาต์พุตอย่างชัดเจน ให้ใช้ --timestamps หรือ -t

ตัวอย่าง:

 $ docker logs -t redis 2021-08-28T16:53:42.160312570Z 1:C 28 Aug 2021 16:53:42.160 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 2021-08-28T16:53:42.160337871Z 1:C 28 Aug 2021 16:53:42.160 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=1, just started 2021-08-28T16:53:42.160342171Z 1:C 28 Aug 2021 16:53:42.160 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 2021-08-28T16:53:42.160792578Z 1:M 28 Aug 2021 16:53:42.160 * monotonic clock: POSIX clock_gettime 2021-08-28T16:53:42.161148683Z 1:M 28 Aug 2021 16:53:42.161 * Running mode=standalone, port=6379. 2021-08-28T16:53:42.161170984Z 1:M 28 Aug 2021 16:53:42.161 # Server initialized 2021-08-28T16:53:42.161186984Z 1:M 28 Aug 2021 16:53:42.161 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 2021-08-28T16:53:42.161484389Z 1:M 28 Aug 2021 16:53:42.161 * Ready to accept connections $

ผสานธง

Docker เสนอให้รวมแฟล็กบางอย่างเพื่อรับเอาต์พุตที่กรองมากกว่าการพิมพ์เนื้อหาบันทึกทั้งหมดบนหน้าจอ เป็นตัวอย่างง่ายๆ เราสามารถรวม --tail flag กับ --since เพื่อรับเอาต์พุตที่จำกัดมากขึ้น

ตัวอย่าง:

 $ docker logs --since=2h -f nostalgic_wescoff 172.17.0.1 - - [28/Aug/2021:15:19:24 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" 172.17.0.1 - - [28/Aug/2021:15:19:25 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-"

สิ่งนี้สามารถทำงานกับแฟล็กอื่นได้เช่นกัน

กรองด้วยเชลล์ยูทิลิตี้

ยูทิลิตี้เชลล์ของ Linux ยังสามารถใช้เพื่อความคล่องตัวในเอาต์พุตบันทึกได้อีกด้วย ยูทิลิตีเช่น grep , head , tail เป็นต้น สามารถส่งไปยังเอาต์พุต docker logs เพื่อการดำเนินการขั้นสูงเพิ่มเติม

ตัวอย่าง:

 $ docker logs --since=7h nostalgic_wescoff 2>&1 | grep GET 172.17.0.1 - - [28/Aug/2021:10:29:26 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" 172.17.0.1 - - [28/Aug/2021:15:19:24 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" 172.17.0.1 - - [28/Aug/2021:15:19:25 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" $

โปรดทราบว่าเราจำเป็นต้องเปลี่ยนเส้นทางสตรีมบันทึกเพื่อให้อินพุตแบบไพพ์เดียวสำหรับ grep โดยใช้ 2>&1

สรุป

Docker เป็นแพลตฟอร์มอเนกประสงค์ที่มีคุณสมบัติมากมายในการจัดการสภาพแวดล้อม การจัดการบันทึกสำหรับระบบเป็นหนึ่งในทักษะที่จำเป็นที่ผู้ดูแลระบบทุกคนควรรู้ การจัดการบันทึกใน Docker นั้นง่ายดายเมื่อคุณทราบคำสั่งที่มีอยู่และแฟล็กที่เป็นไปได้ตามความต้องการของคุณ

หากต้องการอ่านเพิ่มเติมเกี่ยวกับ Docker และฟังก์ชันต่างๆ โปรดดูเอกสารประกอบของ Docker