sleep 1s 表示延遲一秒
sleep 1m 表示延遲一分鐘
sleep 1h 表示延遲一小時
sleep 1d 表示延遲一天
2、usleep : 默認(rèn)以微秒。
1s = 1000ms = 1000000us
按照前文中查看日志文件的方法
# pwd /var/lib/docker/containers/aa3307f5b42770319129e126122be123cfd8e0ebe1c412371ad27e62faa007e3 # ls -lah total 2.6M drwx------ 4 root root 4.0K Mar 24 16:22 . drwx------ 3 root root 4.0K Mar 24 16:21 .. -rw-r----- 1 root root 647K Mar 24 16:22 aa3307f5b42770319129e126122be123cfd8e0ebe1c412371ad27e62faa007e3-json.log -rw-r----- 1 root root 977K Mar 24 16:22 aa3307f5b42770319129e126122be123cfd8e0ebe1c412371ad27e62faa007e3-json.log.1 -rw-r----- 1 root root 977K Mar 24 16:21 aa3307f5b42770319129e126122be123cfd8e0ebe1c412371ad27e62faa007e3-json.log.2
很容易發(fā)現(xiàn),日志文件的策略就是維持3個1m大小文件存在,和我們設(shè)置保持一致。
測試完成后,記得使用docker stop aa3 清理測試現(xiàn)場,max-size也可以按照真實(shí)需求調(diào)整大小。
nginx容器日志
了解docker容器的日志策略后,再看看常用的容器是如何處理的。先看看nginx容器。
首先? docker run -it –rm -d -p 80:80 nginx:1.15.8-alpine? 創(chuàng)建一個nginx容器,然后? docker exec -it b6d sh? 進(jìn)入容器,查看/etc/nginx/nginx.conf可以看到下面內(nèi)容:
error_log /var/log/nginx/error.log warn; access_log /var/log/nginx/access.log main;
也就是nginx會將錯誤日志和訪問日志寫入對應(yīng)的日志文件。繼續(xù)查看/var/log/nginx目錄:
/var/log/nginx # ls -lah total 0 drwxr-xr-x 2 root root 39 Mar 4 07:54 . drwxr-xr-x 3 root root 18 Mar 4 07:54 .. lrwxrwxrwx 1 root root 11 Jan 31 23:32 access.log -> /dev/stdout lrwxrwxrwx 1 root root 11 Jan 31 23:32 error.log -> /dev/stderr
這就發(fā)現(xiàn)奧秘了,access.log文件會通過軟鏈接重定向到標(biāo)準(zhǔn)輸出,而錯誤日志error.log則會重定向標(biāo)準(zhǔn)錯誤。這樣使用docker log命令就可以看到nginx的訪問日志了。
為了進(jìn)一步驗證,查看nginx dockerfile文件,其中有:
# forward request and error logs to docker log collector && ln -sf /dev/stdout /var/log/nginx/access.log \ && ln -sf /dev/stderr /var/log/nginx/error.log
可見nginx鏡像創(chuàng)建時候就定義好了日志文件的輸出。
同樣使用docker stop 524清理現(xiàn)場,以后就不再介紹清理這一步驟了。
mysql容器日志
啟動一個mysql容器
docker run --rm -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
不難看到mysql容器日志輸出, 截取其中片段如下:
Initializing database
2019-03-24T08:48:19.102726Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).
2019-03-24T08:48:20.241459Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-03-24T08:48:20.414933Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-03-24T08:48:20.509897Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 935a6ee7-4e11-11e9-b135-0242ac110002.
2019-03-24T08:48:20.519148Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
2019-03-24T08:48:20.519843Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the –initialize-insecure option.
2019-03-24T08:48:24.066683Z 1 [Warning] ‘user’ entry ‘root@localhost’ ignored in –skip-name-resolve mode.
2019-03-24T08:48:24.066730Z 1 [Warning] ‘user’ entry ‘mysql.session@localhost’ ignored in –skip-name-resolve mode.
2019-03-24T08:48:24.066740Z 1 [Warning] ‘user’ entry ‘mysql.sys@localhost’ ignored in –skip-name-resolve mode.
2019-03-24T08:48:24.066756Z 1 [Warning] ‘db’ entry ‘performance_schema mysql.session@localhost’ ignored in –skip-name-resolve mode.
2019-03-24T08:48:24.066761Z 1 [Warning] ‘db’ entry ‘sys mysql.sys@localhost’ ignored in –skip-name-resolve mode.
2019-03-24T08:48:24.066772Z 1 [Warning] ‘proxies_priv’ entry ‘@ root@localhost’ ignored in –skip-name-resolve mode.
2019-03-24T08:48:24.066814Z 1 [Warning] ‘tables_priv’ entry ‘user mysql.session@localhost’ ignored in –skip-name-resolve mode.
2019-03-24T08:48:24.066822Z 1 [Warning] ‘tables_priv’ entry ‘sys_config mysql.sys@localhost’ ignored in –skip-name-resolve mode.
Database initialized
Initializing certificates
Generating a RSA private key
查看mysql Dockerfile? 文件,可以知道m(xù)ysql鏡像啟動入口在entrypoint.sh,從腳本中發(fā)現(xiàn):
echo 'Initializing database' "$@" --initialize-insecure echo 'Database initialized'
這樣就是mysql容器啟動時候的輸出對應(yīng)起來了。entrypoint.sh比較復(fù)雜,主要功能就是啟動mysqld,并將日志輸出,因為不是本文重點(diǎn),就不詳細(xì)介紹了。
總結(jié)