Docker 进阶需要了解的事情

docker 分层

docker 镜像是一个或者多个层组成的只读堆栈, 可以使用docker history ${image} 查看镜像的构建方式.
无论何时创建容器, 都会在基础镜像的顶部添加可写层, 如果镜像的文件被修改, 则把文件复制到自己的可写层, 这种办法可以防止相同镜像创建的容器互相干扰. docker diff ${container_id}可以查看镜像中修改的文件.
可写层中的数据会随着镜像一起删除, 如果要保留数据, 则需要使用docker commit ${container_id} 命令将容器层提交为新的镜像.

docker 导出镜像

  • docker commit ${container} : 将容器层的更改提交到新的镜像中去
  • docker save --output ${filename} ${image} : 将镜像保存到tar文件
  • docker load -i ${filename} : 将tar镜像加载到本地
  • docker export --output ${filename} ${image} : 将镜像保存到tar文件
  • docker import ${filename} : 将tar文件导入到镜像

export 和 save都会生成tar文件, 区别是save会保留元数据和每一层的历史记录, export的镜像不会保留, 所以export的镜像会比较小.

docker网络模式

docker提供以下几种网络模式管理容器间/容器主机间的通信

  • none 无任何网络配置
  • bridge相当于主机做nat
  • host与主机共享网络, 容器的端口也会映射到主机上.
  • overlay swarm 中的网络, 名为docker_gwbridge的bridge网络和名为ingress的overlay网络. swarm 集群节点上的容器可以通过overlay网络通信(--attachable)
  • macvlan 在eth0上虚拟一个新的mac地址, 需要把eth0 设置为混杂模式.

docker 如果有网络映射到主机, 启动容器时也会新建一个虚拟网口, ip link list 可以看到 类似 veth8b4bf0a@if22,

sudo iptables -t nat -nL POSTROUTING 可以看到一条iptables规则.

pi@raspberrypi  ~  sudo iptables -t nat -nL POSTROUTING
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
MASQUERADE  all  --  172.17.0.0/16        0.0.0.0/0           
MASQUERADE  all  --  172.18.0.0/16        0.0.0.0/0           
MASQUERADE  tcp  --  172.17.0.2           172.17.0.2           tcp dpt:9000
MASQUERADE  tcp  --  172.17.0.3           172.17.0.3           tcp dpt:8083
MASQUERADE  tcp  --  172.17.0.5           172.17.0.5           tcp dpt:8000
# Warning: iptables-legacy tables present, use iptables-legacy to see them

Dockerfile

RUN 构建时运行
CMD 容器启动时运行, 只有最后一条生效, 会被docker run 后面的命令覆盖
ENTRYPOINT 容器启动时运行, 只有最后一条生效

其他有用的docker命令

docker history xxx:xxx # 查看镜像每一层的命令
pi@raspberrypi  ~  docker history nginx
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
635c1d40c095   8 weeks ago   /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B        
<missing>      8 weeks ago   /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B        
<missing>      8 weeks ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
<missing>      8 weeks ago   /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B        
<missing>      8 weeks ago   /bin/sh -c #(nop) COPY file:09a214a3e07c919a…   4.61kB    
<missing>      8 weeks ago   /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7…   1.04kB    
<missing>      8 weeks ago   /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0…   1.96kB    
<missing>      8 weeks ago   /bin/sh -c #(nop) COPY file:65504f71f5855ca0…   1.2kB     
<missing>      8 weeks ago   /bin/sh -c set -x     && addgroup --system -…   52.5MB    
<missing>      8 weeks ago   /bin/sh -c #(nop)  ENV PKG_RELEASE=1~bullseye   0B        
<missing>      8 weeks ago   /bin/sh -c #(nop)  ENV NJS_VERSION=0.7.2        0B        
<missing>      8 weeks ago   /bin/sh -c #(nop)  ENV NGINX_VERSION=1.21.6     0B        
<missing>      8 weeks ago   /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B        
<missing>      8 weeks ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B        
<missing>      8 weeks ago   /bin/sh -c #(nop) ADD file:7c0451fffe8a520c2…   56.6MB 

docker diff ${CONTAINER}  # 查看容器相对于镜像所做的文件修改
pi@raspberrypi  ~  docker diff 1127157f6c92
A /boot
C /etc
C /etc/dnsmasq.conf
C /etc/exports
A /etc/urandom.seed
C /etc/opkg
C /etc/opkg/distfeeds.conf
C /etc/crontabs
A /etc/crontabs/cron.update
A /etc/crontabs/root
C /etc/samba
A /etc/samba/smbpasswd
A /etc/samba/secrets.tdb
A /etc/samba/smb.conf
C /etc/nginx
C /etc/nginx/conf.d
A /etc/nginx/conf.d/_lan.crt
...
...
posted @ 2022-07-08 08:58  Chinor  阅读(57)  评论(0编辑  收藏  举报