再谈docker基本命令
子曰,温故而知新
今日,再次看书之际,又寻得docker的几条使用命令,用小本本记下来
配置docker镜像源
当我们在拉去一些共有镜像时,默认,docker会向docker.io去获取,如果在拉取的过程中出现下载速度慢的情况呢,其实可以试试用一些国内的镜像源,例如:daocloud镜像站
官方文档: https://www.daocloud.io/mirror
仅需要一条明明足以
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
当前命令适用于Ubuntu14.04、Debian、CentOS6 、CentOS7、Fedora、Arch Linux、openSUSE Leap 42.1
执行完毕后,需要重启一下docker
# systemctl restart docker
如果在重启的过程中报错:
Apr 20 09:02:48 localhost systemd: Starting Docker Application Container Engine... Apr 20 09:02:48 localhost dockerd-current: unable to configure the Docker daemon with file /etc/docker/daemon.json: invalid character '}' looking for beginning of object key string Apr 20 09:02:48 localhost systemd: docker.service: main process exited, code=exited, status=1/FAILURE
这是由于配置json文档的格式出错了
,则需要将/etc/docker/daemon.json文件中后面那个,号给删掉即可,例如
修改前:
{"registry-mirrors": ["http://f8214db2.m.daocloud.io"],}
修改后:
{"registry-mirrors": ["http://f8214db2.m.daocloud.io"]}
再次重启docker即可
查看docker 最新的log
使用docker logs -ft 即可
-f: 动态输出
-t: 增加时间戳
--tail n: 获取最新输出的n条
例如:
# docker logs -ft --tail 10 centos_name_1 2019-04-20T13:27:19.366905000Z 2019-04-20T13:27:19.368079000Z lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 2019-04-20T13:27:19.368478000Z inet 127.0.0.1 netmask 255.0.0.0 2019-04-20T13:27:19.368746000Z inet6 ::1 prefixlen 128 scopeid 0x10<host> 2019-04-20T13:27:19.369000000Z loop txqueuelen 1000 (Local Loopback) 2019-04-20T13:27:19.371869000Z RX packets 0 bytes 0 (0.0 B) 2019-04-20T13:27:19.374114000Z RX errors 0 dropped 0 overruns 0 frame 0 2019-04-20T13:27:19.374525000Z TX packets 0 bytes 0 (0.0 B) 2019-04-20T13:27:19.374886000Z TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 2019-04-20T13:27:19.375426000Z
查看docker进程
使用docker top命令可以查看docker中的进程
例如:
# docker top centos_name_1 UID PID PPID C STIME TTY TIME CMD root 4468 4452 0 09:16 pts/2 00:00:00 /bin/bash #
查看docker容器的负载
# docker stats centos_name_1 CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS centos_name_1 0.00% 392 KiB / 991 MiB 0.04% 22 MB / 600 kB 22.8 MB / 46 MB 1
docker自重启
在docker run 时,加入--restart=[always,on-failure:[number]]
--restart=always :docker 自动重启
--restart=on-failure :docker 只有在退出码非0的时候才会重启
--restart=on-failure:3 在退出码状态为非0的时候,重启,最多重启3次
例子 --restart=always :
# docker run -d --restart=always nginx
创建一个docker 容器,现在来删除它
# ps aux | grep `docker ps | grep nginx | awk '{print $1}'` | grep -v grep | awk '{print $2}' 7583 # kill -9 7583 # ps aux | grep `docker ps | grep nginx | awk '{print $1}'` | grep -v grep | awk '{print $2}' 7842 # docker ps ONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d1ed69cb3238 nginx "nginx -g 'daemon ..." 8 minutes ago Up 47 seconds 0.0.0.0:80->80/tcp affectionate_wiles #
我们可以发现,kill后进程又起来了
例子 --restart=--restart=on-failure:
# docker run -d --restart=on-failure:2 nginx 6fabc743a488a857909ac4f1472a7f1012e2224e300da70aa15c1cb8f5db0767 # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6fabc743a488 nginx "nginx -g 'daemon ..." 2 seconds ago Up 2 seconds 80/tcp cocky_heisenberg #
# ps aux | grep `docker ps | grep nginx | awk '{print $1}'` | grep -v grep | awk '{print $2}' 8030 # kill -9 8030 # ps aux | grep `docker ps | grep nginx | awk '{print $1}'` | grep -v grep | awk '{print $2}' 8145 # kill -9 8145 # ps aux | grep `docker ps | grep nginx | awk '{print $1}'` | grep -v grep | awk '{print $2}' 8242 # kill -9 8242 # ps aux | grep `docker ps | grep nginx | awk '{print $1}'` | grep -v grep | awk '{print $2}' Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information. #
发现,在重启两次后,该容器不重启了。
其实基础真的很重要~~~