1. [root@huan ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. alpine 3.10.1 b7b28af77ffe 13 months ago 5.58MB

提交容器

  1. [root@huan ~]# docker commit
  2. [root@huan ~]# docker commit -p myalpine oldboy1103/alpine:v3.10.3_with_1.txt

固化到只读层了

docker导出镜像到宿主机

  1. [root@huan ~]# docker save b7b28af77ffe > alpine:v3.10.3_with_1.txt.tar
  2. [root@huan ~]# ll
  3. -rw-r--r-- 1 root root 5852160 8 15 20:03 alpine:v3.10.3_with_1.txt.tar

导入镜像

  1. [root@huan ~]# docker load < alpine\:v3.10.3_with_1.txt.tar
  2. [root@huan ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. <none> <none> b7b28af77ffe 13 months ago 5.58MB

使用docker images发现REPOSITORY和TAG都是none,使用tag即可打标签

  1. [root@huan ~]# docker tag b7b28af77ffe oldboy1103/alpine:v3.10.3_with_1.txt
  2. [root@huan ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. oldboy1103/alpine v3.10.3_with_1.txt b7b28af77ffe 13 months ago 5.58MB

把标准输出重定向到日志

  1. docker run hello-world 2>&1 >>/dev/null

查看日志,不加-f也可以

  1. docker logs -f 容器镜像ID

docker容器的高级操作

docker - 图1

不是因为难,而是因为特别重要。

下载nginx

  1. [root@huan ~]# docker pull nginx:1.12.2
  2. [root@huan ~]# docker tag 4037a5562b03 oldboy1103/nginx:v1.12.2
  3. [root@huan ~]# docker images
  4. REPOSITORY TAG IMAGE ID CREATED SIZE
  5. oldboy1103/alpine v3.10.3_with_1.txt b7b28af77ffe 13 months ago 5.58MB
  6. nginx 1.12.2 4037a5562b03 2 years ago 108MB
  7. oldboy1103/nginx v1.12.2 4037a5562b03 2 years ago 108MB

端口映射,容器外端口:容器内端口

  1. [root@huan ~]# docker run --rm --name mynginx -d -p81:80 oldboy1103/nginx:v1.12.2

下载百度首页进行演示

  1. [root@huan ~]# mkdir html
  2. [root@huan html]# wget www.baidu.com -O index.html
  3. [root@huan html]# docker run -d --rm --name nginx_with_baidu -d -p82:80 -v /root/html:/usr/share/nginx/html oldboy1103/nginx:v1.12.2

docker - 图2

inspect命令

  1. docker inspect 容器ID

容器传递环境变量

  1. [root@huan ~]# docker run --rm -e E_OPTS=abcdefg oldboy1103/alpine:latest printenv
  2. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  3. HOSTNAME=b6759d28963f
  4. E_OPTS=abcdefg
  5. HOME=/root

进入容器

  1. [root@huan ~]# docker exec -ti nginx_with_baidu /bin/bash
  2. root@0ffcc450e2f6:/# tee /etc/apt/sources.list << EOF
  3. > deb http://mirrors.163.com/debian/ jessie main non-free contrib
  4. > deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
  5. > EOF
  6. deb http://mirrors.163.com/debian/ jessie main non-free contrib
  7. deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
  8. root@0ffcc450e2f6:/# apt-get update && apt-get install curl -y

开始固化

  1. [root@huan ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 0ffcc450e2f6 oldboy1103/nginx:v1.12.2 "nginx -g 'daemon of…" 5 minutes ago Up 4 minutes 0.0.0.0:82->80/tcp nginx_with_baidu
  4. [root@huan ~]# docker commit -p 0ffcc450e2f6 xxxxxxxxxxxx/nginx:curl
  5. [root@huan ~]# docker images
  6. REPOSITORY TAG IMAGE ID CREATED SIZE
  7. xxxxxxxxxxxx/nginx curl 6f10e7047510 2 minutes ago 136MB
  8. [root@huan ~]# docker push xxx/nginx:curl
  9. The push refers to repository [docker.io/xxxxxxxxxxxx/nginx]
  10. 761966e456bc: Pushed
  11. 4258832b2570: Mounted from library/nginx

mounted在push的时候,就会从公网的library/nginx中mount过来一层,这样就会节省网络流量z

docker - 图3

容器内安装软件(工具)

生产干货:公司用的容器最多的发行版本是debian系,生产上大量的都是用的debian系,反而红帽系很少,包括老外。

容器的生命周期

docker - 图4

分配一个文件系统,并在只读的镜像层外 挂载 一层可读写层,在文件系统之外,容器和容器之间是隔离的,容器和宿主机之间也是隔离的。

查看对应目录

  1. [root@huan ~]# cd /data/docker/
  2. [root@huan docker]# ll
  3. 总用量 48
  4. drwx------ 2 root root 4096 8 15 14:19 builder
  5. drwx--x--x 4 root root 4096 8 15 14:19 buildkit
  6. drwx------ 3 root root 4096 8 15 20:51 containers
  7. drwx------ 3 root root 4096 8 15 14:17 image
  8. drwxr-x--- 3 root root 4096 8 15 14:17 network
  9. drwx------ 10 root root 4096 8 15 21:04 overlay2
  10. drwx------ 4 root root 4096 8 15 14:17 plugins
  11. drwx------ 2 root root 4096 8 15 14:19 runtimes
  12. drwx------ 2 root root 4096 8 15 14:19 swarm
  13. drwx------ 2 root root 4096 8 15 20:47 tmp
  14. drwx------ 2 root root 4096 8 15 14:17 trust
  15. drwx------ 2 root root 4096 8 15 14:17 volumes
  16. [root@huan docker]#
  17. [root@huan docker]#
  18. [root@huan docker]# ll containers/
  19. 总用量 4
  20. drwx------ 4 root root 4096 8 15 21:04 0ffcc450e2f63796863cf55061a7bc406f6d12e76399d6893e6c3e6567f18f18

查看IP

  1. [root@huan docker]# ip add | grep -C 5 docker0
  2. link/ether 52:54:00:6e:c0:80 brd ff:ff:ff:ff:ff:ff
  3. inet 172.17.0.15/20 brd 172.17.15.255 scope global eth0
  4. valid_lft forever preferred_lft forever
  5. inet6 fe80::5054:ff:fe6e:c080/64 scope link
  6. valid_lft forever preferred_lft forever
  7. 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
  8. link/ether 02:42:19:f7:50:b4 brd ff:ff:ff:ff:ff:ff
  9. inet 172.17.0.15/24 brd 172.17.0.255 scope global docker0
  10. valid_lft forever preferred_lft forever
  11. inet6 fe80::42:19ff:fef7:50b4/64 scope link
  12. valid_lft forever preferred_lft forever
  13. 11: veth652299c@if10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
  14. link/ether 16:d4:8f:44:0b:e6 brd ff:ff:ff:ff:ff:ff link-netnsid 0
  15. inet6 fe80::14d4:8fff:fe44:be6/64 scope link
  16. valid_lft forever preferred_lft forever

生命周期图

docker - 图5