容器: 直接共享宿主机,资源占用更小,几乎无性能损耗,可移植性高,轻量、灵活迁移。部署更快速,运行密度:单机支持上千容器。 可以理解为操作系统的一个进程 虚拟机: 什么都不干就会有50M的内存开销,操作系统额外的cpu、内存消耗。笨重、与虚拟化技术耦合度高
docker-ce:社区版 docker-ee:商务版
对于开发人员:build once,run anywhere(一次构建,可以在任何地方运行) 对于运维人员:configure once,run anything(一次配置,可以运行所有东西)
开发,运维与线上环境不同,docker可以解决相互沟通问题
解决方法:
[root@server1 ~]# cd /etc/sysctl.d/ [root@server1 sysctl.d]# vim docker.conf bridge-nf-call-iptables=1 bridge-nf-call-ip6tables=1 [root@server1 sysctl.d]# sysctl -a|grep /proc/sys/bridge-nf-call-iptables sysctl: reading key "net.ipv6.conf.all.stable_secret" sysctl: reading key "net.ipv6.conf.default.stable_secret" sysctl: reading key "net.ipv6.conf.docker0.stable_secret" sysctl: reading key "net.ipv6.conf.eth0.stable_secret" sysctl: reading key "net.ipv6.conf.lo.stable_secret" [root@server1 sysctl.d]# sysctl net.bridge.bridge-nf-call-iptables net.bridge.bridge-nf-call-iptables = 1 [root@server1 sysctl.d]# sysctl net.bridge.bridge-nf-call-ip6tables net.bridge.bridge-nf-call-ip6tables = 1 [root@server1 sysctl.d]# docker info # 显示系统相关信息 Client: Debug Mode: false Server: Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 19.03.12 Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd init version: fec3683 Security Options: seccomp Profile: default Kernel Version: 3.10.0-957.el7.x86_64 Operating System: Red Hat Enterprise Linux Server 7.6 (Maipo) OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 991.1MiB Name: server1 ID: RNUW:Y4L6:4TLX:G66T:6FXP:2T65:QRLW:PREX:CXWD:6TRM:NRAJ:GKWQ Docker Root Dir: /var/lib/docker Debug Mode: false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false网页测试: http://172.25.4.1/
镜像分层结构的优点主要在于共享资源
copy-on-write 可写容器层容器层以下所有镜像层都是只读的docker从上往下依次查找文件容器层保存镜像变化的部分,并不会对镜像本本身进行任何修改一个镜像最多127层 [root@server1 ~]# docker history game2048:latest #查看镜像分层结构(镜像的构建历史) IMAGE CREATED CREATED BY SIZE COMMENT 19299002fdbe 3 years ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "sed … 0B <missing> 3 years ago /bin/sh -c #(nop) EXPOSE 80/tcp 0B <missing> 3 years ago /bin/sh -c #(nop) COPY dir:cb74e9c037a3d501c… 600kB <missing> 3 years ago /bin/sh -c #(nop) MAINTAINER Golfen Guo <go… 0B <missing> 3 years ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B <missing> 3 years ago /bin/sh -c #(nop) EXPOSE 443/tcp 80/tcp 0B <missing> 3 years ago /bin/sh -c #(nop) COPY file:d15ceb73c6ea776c… 1.1kB <missing> 3 years ago /bin/sh -c #(nop) COPY file:af94db45bb7e4b8f… 643B <missing> 3 years ago /bin/sh -c GPG_KEYS=B0F4253373F8F6F510D42178… 50.1MB <missing> 3 years ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.11.7 0B <missing> 3 years ago /bin/sh -c #(nop) MAINTAINER NGINX Docker M… 0B <missing> 3 years ago /bin/sh -c #(nop) ADD file:7afbc23fda8b0b387… 4.8MBmiss是因为是在官方主机构建的,本地没有构建信息。但是可以使用
[root@server1 ~]# docker pull nginx # 拉取nginx镜像,默认情况下拉取最新版本 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest [root@server1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 4bb46517cac3 2 weeks ago 133MB game2048 latest 19299002fdbe 3 years ago 55.5MB [root@server1 ~]# docker history nginx:latest IMAGE CREATED CREATED BY SIZE COMMENT 4bb46517cac3 2 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B <missing> 2 weeks ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B <missing> 2 weeks ago /bin/sh -c #(nop) EXPOSE 80 0B <missing> 2 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["/docker-entr… 0B <missing> 2 weeks ago /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7… 1.04kB <missing> 2 weeks ago /bin/sh -c #(nop) COPY file:1d0a4127e78a26c1… 1.96kB <missing> 2 weeks ago /bin/sh -c #(nop) COPY file:e7e183879c35719c… 1.2kB <missing> 2 weeks ago /bin/sh -c set -x && addgroup --system -… 63.4MB <missing> 2 weeks ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~buster 0B <missing> 2 weeks ago /bin/sh -c #(nop) ENV NJS_VERSION=0.4.3 0B <missing> 2 weeks ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.19.2 0B <missing> 4 weeks ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B <missing> 4 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B <missing> 4 weeks ago /bin/sh -c #(nop) ADD file:3af3091e7d2bb40bc… 69.2MB不同版本的镜像,使同一层的镜像
[root@server1 ~]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 13768 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1879 [OK] # nginx 是官方上传的镜像 # jwilder/nginx-proxy 是个人上传的镜像内核相同,文件系统不同 镜像里面封装的是根文件系统
[root@server1 ~]# uname -r 3.10.0-957.el7.x86_64 [root@server1 ~]# docker pull busybox Using default tag: latest latest: Pulling from library/busybox 9c075fe2c773: Pull complete Digest: sha256:c3dbcbbf6261c620d133312aee9e858b45e1b686efbcead7b34d9aae58a37378 Status: Downloaded newer image for busybox:latest docker.io/library/busybox:latest [root@server1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest edabd795951a 2 days ago 1.22MB nginx latest 4bb46517cac3 2 weeks ago 133MB game2048 latest 19299002fdbe 3 years ago 55.5MB [root@server1 ~]# docker ps #查看容器进程 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c76c29d74a33 game2048 "/bin/sh -c 'sed -i …" 58 minutes ago Up 58 minutes 0.0.0.0:80->80/tcp, 443/tcp demo [root@server1 ~]# docker rm -f demo #强制删除正在运行的容器 demo [root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@server1 ~]# docker run -it --name test busybox / # ls bin dev etc home proc root sys tmp usr var / # uname -r #查看内核版本,与宿主机内核版本相同(因为共享宿主机内核) 3.10.0-957.el7.x86_64意为这在同一台宿主机可以运行不同的操作系统,不同的操作系统只是根文件系统不同
[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cf75faf2a77e busybox "sh" 2 minutes ago Exited (0) 14 seconds ago test [root@server1 ~]# docker rm test test