Docker
安装docker
https://docs.docker.com/engine/install/centos/ docker version查看是否安装成功 [root@iZbp10a8w2ct7pZ ~]# docker run hello-world # 本地没有hello-world,从仓库pull Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:7f0a9f93b4aa3022c3a4c147a449bf11e0941a1fd0bf4a8e6c9408b2600777c5 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ [root@iZbp10a8w2ct7pZ ~]# docker images # 查看docker镜像 REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 8 months ago 13.3kB [root@iZbp10a8w2ct7pZ ~]#
阿里云容器镜像加速配置使用
从阿里云docker库中下载,代替DockerHub网站下载 sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://6gjccqh6.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
底层原理
docker是一个client-server结构系统,docker守护进程运行在主机上,通过socket从客户端访问。
DockerServer接收到Docker-Client指令,就会执行这个命令。
常用命令
docker version # 显示docker版本信息 docker info # 显示docker系统信息,包括镜像和容器数量 docker --help docker images --help # 帮助命令 更多命令https://docs.docker.com/engine/reference/commandline/docker/
镜像命令
[root@iZbp10a8w2ct7pZ ~]# docker images # 查看docker镜像 REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 8 months ago 13.3kB REPOSITORY # 镜像的仓库源 TAG # 镜像的标签 IMAGE ID # 镜像id CREATED # 镜像创建时间 SIZE # 镜像大小 docker images -a, docker images --all # 列出所有镜像 docker images -q, docker images --quiet # 只显示镜像id [root@iZbp10a8w2ct7pZ ~]# docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 8 months ago 13.3kB [root@iZbp10a8w2ct7pZ ~]# docker images -q bf756fb1ae65 [root@iZbp10a8w2ct7pZ ~]# docker images -aq bf756fb1ae65 搜索命令 docker search 镜像名 # docker搜索命令,也可去DockerHub去搜索,结果一样 docker search mysql --filert=STARS=5000 dock pull mysql # 下载镜像 [root@iZbp10a8w2ct7pZ ~]# docker pull mysql:5.7 # docker pull默认最新版本,:5.7指定版本下载 5.7: Pulling from library/mysql bf5952930446: Pull complete # 分层下载,docker image核心,联合文件系统 8254623a9871: Pull complete 938e3e06dac4: Pull complete ea28ebf28884: Pull complete f3cef38785c2: Pull complete 894f9792565a: Pull complete 1d8a57523420: Pull complete 5f09bf1d31c1: Pull complete 1591b28581e8: Pull complete 96ef942f7603: Pull complete 2e009731422e: Pull complete Digest: sha256:1a83136153b238b659b0887ceb4e08275473af1eab2e67de4c22b37c5f4130cd # 签名 Status: Downloaded newer image for mysql:5.7 docker.io/library/mysql:5.7 # 镜像地址,docker pull mysql:5.7等同于docker pull docker.io/library/mysql:5.7 docker rmi -f 容器id # 删除容器 docker rmi -f 容器id 容器id 容器id 容器id # 删除多个容器 docker rmi -f $(docker images -aq) # 删除全部容器 [root@iZbp10a8w2ct7pZ ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 d589ea3123e0 3 days ago 448MB hello-world latest bf756fb1ae65 8 months ago 13.3kB [root@iZbp10a8w2ct7pZ ~]# docker rmi -f d589ea3123e0 Untagged: mysql:5.7 Untagged: mysql@sha256:1a83136153b238b659b0887ceb4e08275473af1eab2e67de4c22b37c5f4130cd Deleted: sha256:d589ea3123e06ae2cfb548f3a697fc82986878afb1ef7b4fe1d8a3981d420620 Deleted: sha256:a014b34bfc08ca4a4fd6a3ddbc9882b4dc3b192d0a0b1e864662cd1d23ea7567 Deleted: sha256:abf85fe645d8297b56e0363050e6df5e1ccc0649d4a71dc2c2f978a1de8ab1da Deleted: sha256:4299450e41032ab45b35e5d929bfebfa6e3d06528b23b02dc1223fcff7fb0667 Deleted: sha256:165805124136fdee738ed19021a522bb53de75c2ca9b6ca87076f51c27385fd7 Deleted: sha256:904abdc2d0bea0edbb1a8171d1a1353fa6de22150a9c5d81358799a5b6c38c8d Deleted: sha256:d26f7649f78cf789267fbbca8aeb234932e230109c728632c6b9fbc60ca5591b Deleted: sha256:7fcf7796e23ea5b42eb3bbd5bec160ba5f5f47ecb239053762f9cf766c143942 Deleted: sha256:826130797a5760bcd2bb19a6c6d92b5f4860bbffbfa954f5d3fc627904a76e9d Deleted: sha256:53e0181c63e41fb85bce681ec8aadfa323cd00f70509107f7001a1d0614e5adf Deleted: sha256:d6854b83e83d7eb48fb0ef778c58a8b839adb932dd036a085d94a7c2db98f890 Deleted: sha256:d0f104dc0a1f9c744b65b23b3fd4d4d3236b4656e67f776fe13f8ad8423b955c [root@iZbp10a8w2ct7pZ ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 8 months ago 13.3kB [root@iZbp10a8w2ct7pZ ~]# docker rmi -f $(docker images -aq) Untagged: hello-world:latest Untagged: hello-world@sha256:7f0a9f93b4aa3022c3a4c147a449bf11e0941a1fd0bf4a8e6c9408b2600777c5 Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b [root@iZbp10a8w2ct7pZ ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE
容器命令
进入容器 docker run -it # 使用交互方式运行,进入容器查看内容 [root@iZbp10a8w2ct7pZ ~]# docker pull centos ... [root@iZbp10a8w2ct7pZ ~]# docker run -it centos /bin/bash # 启动并进入容器 [root@ba8ce9ec552c /]# ls # 查看容器内centos,基础版本,很多命令不完善 bin etc lib lost+found mnt proc run srv tmp var dev home lib64 media opt root sbin sys usr [root@ba8ce9ec552c /]# exit # 从容器中退回主机 exit [root@iZbp10a8w2ct7pZ ~]# cd / [root@iZbp10a8w2ct7pZ /]# ls bin dev home lib64 mnt proc run srv tmp var boot etc lib media opt root sbin sys usr 查看容器 docker ps # 列出所有运行的容器 docker ps -a # 列出当前正在运行的容器和历史运行过的容器 docker ps -n=? # 显示最近创建的容器 docker ps -q # 只显示容器的编号 [root@iZbp10a8w2ct7pZ /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@iZbp10a8w2ct7pZ /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ba8ce9ec552c centos "/bin/bash" 9 minutes ago Exited (0) 3 minutes ago laughing_satoshi 46d8229f9713 bf756fb1ae65 "/hello" 19 hours ago Exited (0) 19 hours ago happy_wozniak [root@iZbp10a8w2ct7pZ /]# docker ps -n=1 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ba8ce9ec552c centos "/bin/bash" 18 minutes ago Exited (0) 12 minutes ago laughing_satoshi [root@iZbp10a8w2ct7pZ /]# docker ps -aq ba8ce9ec552c 46d8229f9713 退出容器 exit # 容器停止并退出容器 ctrl + p + q # 容器不停止,退出容器 [root@iZbp10a8w2ct7pZ /]# docker run -it centos /bin/bash [root@b829a2218ef7 /]# [root@iZbp10a8w2ct7pZ /]# # ctrl + p + q [root@iZbp10a8w2ct7pZ /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b829a2218ef7 centos "/bin/bash" About a minute ago Up About a minute tender_maxwell [root@iZbp10a8w2ct7pZ /]# 删除容器 docker rm 容器id # 删除指定容器,不能删除正在运行的容器,强制删除的话,docker rm -f 容器id docker rm -f $(docker ps -aq) # 删除所有容器 [root@iZbp10a8w2ct7pZ /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b829a2218ef7 centos "/bin/bash" 5 minutes ago Up 5 minutes tender_maxwell ba8ce9ec552c centos "/bin/bash" 27 minutes ago Exited (0) 22 minutes ago laughing_satoshi 46d8229f9713 bf756fb1ae65 "/hello" 19 hours ago Exited (0) 19 hours ago happy_wozniak [root@iZbp10a8w2ct7pZ /]# docker rm ba8ce9ec552c ba8ce9ec552c [root@iZbp10a8w2ct7pZ /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b829a2218ef7 centos "/bin/bash" 5 minutes ago Up 5 minutes tender_maxwell 46d8229f9713 bf756fb1ae65 "/hello" 19 hours ago Exited (0) 19 hours ago happy_wozniak [root@iZbp10a8w2ct7pZ /]# docker rm b829a2218ef7 Error response from daemon: You cannot remove a running container b829a2218ef7dba121e92cad4dda9e56ca8cac479d37fe378f302e5f6be6daf4. Stop the container before attempting removal or force remove [root@iZbp10a8w2ct7pZ /]# docker rm -f $(docker ps -aq) b829a2218ef7 46d8229f9713 [root@iZbp10a8w2ct7pZ /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 启动和停止容器的操作 docker start 容器id docker restart 容器id docker stop 容器id docker kill 容器id 常用的其他命令 docker run -d 镜像名 # 后台启动镜像 [root@iZbp10a8w2ct7pZ /]# docker run -d centos 745994dafb897aabdde808019d054f8ccbe392b87ff8b7fd5e0557b41656db97 [root@iZbp10a8w2ct7pZ /]# docker ps # 发现centos停止了,因为docker run -d centos是使用后台运行,docker必须要有一个前台进程,docker发现没有应用,就会停止 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@iZbp10a8w2ct7pZ /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 745994dafb89 centos "/bin/bash" 36 seconds ago Exited (0) 36 seconds ago frosty_payne 查看日志 docker logs --help docker logs -tf --tail 10 容器id [root@iZbp10a8w2ct7pZ /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4ffb701fc4e7 centos "/bin/bash" 4 minutes ago Up 4 minutes kind_sammet [root@iZbp10a8w2ct7pZ /]# docker logs -tf --tail 10 4ffb701fc4e7 # docker无日志 ^C [root@iZbp10a8w2ct7pZ /]# docker run -d centos /bin/sh -c 'while true;do echo test;sleep 1;done' # 启动新镜像并写入日志 5981cc92455a011175fa792c724f0c1c4306fe66cc20797565159b9f08d1f7c1 [root@iZbp10a8w2ct7pZ /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5981cc92455a centos "/bin/sh -c 'while t…" 5 seconds ago Up 4 seconds interesting_joliot 4ffb701fc4e7 centos "/bin/bash" 5 minutes ago Up 5 minutes kind_sammet [root@iZbp10a8w2ct7pZ /]# docker logs -tf --tail 5 5981cc92455a # 查询新镜像日志 2020-09-09T06:42:52.369272302Z test 2020-09-09T06:42:53.371656047Z test 2020-09-09T06:42:54.373763782Z test 2020-09-09T06:42:55.375862725Z test 2020-09-09T06:42:56.377941891Z test ^C [root@iZbp10a8w2ct7pZ /]# 查看容器中进程信息 docker top 容器id [root@iZbp10a8w2ct7pZ /]# docker run -it centos /bin/bash [root@639db7579b14 /]# [root@iZbp10a8w2ct7pZ /]# [root@iZbp10a8w2ct7pZ /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 639db7579b14 centos "/bin/bash" 14 seconds ago Up 12 seconds angry_liskov [root@iZbp10a8w2ct7pZ /]# docker top 639db7579b14 UID PID PPID C STIME TTY TIME CMD root 408484 408467 0 14:53 pts/0 00:00:00 /bin/bash [root@iZbp10a8w2ct7pZ /]# 查看镜像的元数据 docker inspect --help docker inspect 容器id [root@iZbp10a8w2ct7pZ /]# docker inspect 639db7579b14 [ { "Id": "639db7579b1463b90adcd5aabd03b57c2690a7b38056626ebc8beff63e299320", "Created": "2020-09-09T06:53:23.993543845Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 408484, "ExitCode": 0, "Error": "", "StartedAt": "2020-09-09T06:53:24.50544931Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566", "ResolvConfPath": "/var/lib/docker/containers/639db7579b1463b90adcd5aabd03b57c2690a7b38056626ebc8beff63e299320/resolv.conf", "HostnamePath": "/var/lib/docker/containers/639db7579b1463b90adcd5aabd03b57c2690a7b38056626ebc8beff63e299320/hostname", "HostsPath": "/var/lib/docker/containers/639db7579b1463b90adcd5aabd03b57c2690a7b38056626ebc8beff63e299320/hosts", "LogPath": "/var/lib/docker/containers/639db7579b1463b90adcd5aabd03b57c2690a7b38056626ebc8beff63e299320/639db7579b1463b90adcd5aabd03b57c2690a7b38056626ebc8beff63e299320-json.log", "Name": "/angry_liskov", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "Capabilities": null, "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "private", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DeviceRequests": null, "KernelMemory": 0, "KernelMemoryTCP": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": null, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": [ "/proc/asound", "/proc/acpi", "/proc/kcore", "/proc/keys", "/proc/latency_stats", "/proc/timer_list", "/proc/timer_stats", "/proc/sched_debug", "/proc/scsi", "/sys/firmware" ], "ReadonlyPaths": [ "/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger" ] }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/3e8a3bec4a8c09a4a966c912e60b907743689bd431769026b824a056feab9318-init/diff:/var/lib/docker/overlay2/aa3d962c927b7e4f593f39a25dfe5fa6e51de31b35f067627a5ecf6d769f6023/diff", "MergedDir": "/var/lib/docker/overlay2/3e8a3bec4a8c09a4a966c912e60b907743689bd431769026b824a056feab9318/merged", "UpperDir": "/var/lib/docker/overlay2/3e8a3bec4a8c09a4a966c912e60b907743689bd431769026b824a056feab9318/diff", "WorkDir": "/var/lib/docker/overlay2/3e8a3bec4a8c09a4a966c912e60b907743689bd431769026b824a056feab9318/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "639db7579b14", "Domainname": "", "User": "", "AttachStdin": true, "AttachStdout": true, "AttachStderr": true, "Tty": true, "OpenStdin": true, "StdinOnce": true, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash" ], "Image": "centos", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20200809", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS" } }, "NetworkSettings": { "Bridge": "", "SandboxID": "abe33ebc01096550b046cd1e5c179529cdcbe70dabc4359c5ce2e50424978e32", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/abe33ebc0109", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "ee3ba1aa46f130548f0ffb0ebead5bc5dc0a3270aca6e101478f4283a448c52c", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "3c4d44d1a438f03e8cf11b6a0498ac3bda27e88cfd972a8fd8a1bd4b20656755", "EndpointID": "ee3ba1aa46f130548f0ffb0ebead5bc5dc0a3270aca6e101478f4283a448c52c", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } } ] 进入当前正在运行的容器 docker exec -it 容器id bashShell # 进入容器后开启新的终端,在容器内操作(常用) docker attach 容器id # 进入容器内正在执行的终端,不会有新的进程 [root@iZbp10a8w2ct7pZ /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 639db7579b14 centos "/bin/bash" 2 hours ago Up 2 hours angry_liskov [root@iZbp10a8w2ct7pZ /]# docker exec -it 639db7579b14 /bin/bash [root@639db7579b14 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@639db7579b14 /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 06:53 pts/0 00:00:00 /bin/bash root 14 0 1 08:54 pts/1 00:00:00 /bin/bash root 28 14 0 08:54 pts/1 00:00:00 ps -ef [root@iZbp10a8w2ct7pZ ~]# docker attach 639db7579b14 [root@639db7579b14 /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 06:53 pts/0 00:00:00 /bin/bash root 29 1 0 09:09 pts/0 00:00:00 ps -ef 从容器内拷贝文件到主机 docker cp 容器id:容器内文件目录 目的主机路径 [root@iZbp10a8w2ct7pZ /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 639db7579b14 centos "/bin/bash" 3 hours ago Up About a minute angry_liskov [root@iZbp10a8w2ct7pZ /]# docker attach 639db7579b14 [root@639db7579b14 /]# cd /home [root@639db7579b14 home]# ls [root@639db7579b14 home]# touch test.txt [root@639db7579b14 home]# ls test.txt [root@639db7579b14 home]# exit exit [root@iZbp10a8w2ct7pZ /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@iZbp10a8w2ct7pZ /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 639db7579b14 centos "/bin/bash" 3 hours ago Exited (0) 19 seconds ago angry_liskov [root@iZbp10a8w2ct7pZ /]# docker cp 639db7579b14:/home/test.txt /home # 容器不管是运行还是停止,只要容器在就能拷贝成功 [root@iZbp10a8w2ct7pZ /]# cd /home [root@iZbp10a8w2ct7pZ home]# ls test.txt
部署nginx
[root@iZbp10a8w2ct7pZ /]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx bf5952930446: Pull complete cb9a6de05e5a: Pull complete 9513ea0afb93: Pull complete b49ea07d2e93: Pull complete a5e4a503d449: Pull complete Digest: sha256:b0ad43f7ee5edbc0effbc14645ae7055e21bc1973aee5150745632a24a752661 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest [root@iZbp10a8w2ct7pZ /]# docker run -d --name nginx01 -p 3344:80 nginx # -d后台运行,--name容器命名,-p宿主机端口:容器内部端口 59810f42fe02d855e384de2cc1588bb167efdefb4def41ff8b84a00c1f5702b2 [root@iZbp10a8w2ct7pZ /]# curl localhost:3344 # 本地访问 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> [root@iZbp10a8w2ct7pZ /]# [root@iZbp10a8w2ct7pZ /]# docker exec -it nginx01 /bin/bash # 进入容器 root@59810f42fe02:/# whereis nginx nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx root@59810f42fe02:/#
图形界面管理工具portainer
docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer # -v 卷挂载 访问测试:http://ip:8088/ ...
提交镜像
docker commit -m='提交描述信息' -a='作者' 容器id 目标镜像名:[TAG] # tag为目标镜像版本 [root@iZbp10a8w2ct7pZ ~]# docker run -it redis /bin/bash root@dc9201da1906:/data# cd .. root@dc9201da1906:/# ls bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@dc9201da1906:/# mkdir lib01 root@dc9201da1906:/# cp -r lib/* lib01 root@dc9201da1906:/# cd lib01 root@dc9201da1906:/lib01# ls init lsb systemd terminfo udev x86_64-linux-gnu root@dc9201da1906:/lib01# cd .. root@dc9201da1906:/# ls bin boot data dev etc home lib lib01 lib64 media mnt opt proc root run sbin srv sys tmp usr var root@dc9201da1906:/# [root@iZbp10a8w2ct7pZ ~]# [root@iZbp10a8w2ct7pZ ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dc9201da1906 redis "docker-entrypoint.s…" About a minute ago Up About a minute 6379/tcp heuristic_edison [root@iZbp10a8w2ct7pZ ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis latest 41de2cc0b30e 8 days ago 104MB nginx latest 4bb46517cac3 3 weeks ago 133MB centos latest 0d120b6ccaa8 4 weeks ago 215MB [root@iZbp10a8w2ct7pZ ~]# docker commit -m='redis01' -a='xyp' dc9201da1906 redis01:1.0 sha256:ae42197572a6dcdcafd4b001236304d36008324c48727e5c681fccc83d80a715 [root@iZbp10a8w2ct7pZ ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis01 1.0 ae42197572a6 3 seconds ago 116MB # 生成新的镜像redis01 redis latest 41de2cc0b30e 8 days ago 104MB nginx latest 4bb46517cac3 3 weeks ago 133MB centos latest 0d120b6ccaa8 4 weeks ago 215MB [root@iZbp10a8w2ct7pZ ~]#
容器数据卷
容器之间数据共享、持久化和同步操作。 目录的挂载,将容器内的目录挂载到linux上。 使用命令挂载 docker run -it -v 主机目录:容器目录 镜像名 bashShell [root@iZbp10a8w2ct7pZ home]# ls [root@iZbp10a8w2ct7pZ home]# docker run -it -v /home/ceshi:/home centos /bin/bash [root@e6782e5447e3 /]# [root@iZbp10a8w2ct7pZ home]# # ctrl + p + q退出容器 [root@iZbp10a8w2ct7pZ home]# ls ceshi [root@iZbp10a8w2ct7pZ home]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e6782e5447e3 centos "/bin/bash" About a minute ago Up About a minute gracious_haibt dc9201da1906 redis "docker-entrypoint.s…" 2 hours ago Up 2 hours 6379/tcp heuristic_edison [root@iZbp10a8w2ct7pZ home]# docker attach e6782e5447e3 # 进入容器 [root@e6782e5447e3 /]# cd /home [root@e6782e5447e3 home]# ls [root@e6782e5447e3 home]# read escape sequence # ctrl + p + q退出容器 [root@iZbp10a8w2ct7pZ home]# docker inspect e6782e5447e3 # 容器元信息 ... "Mounts": [ { "Type": "bind", # bind双向绑定 "Source": "/home/ceshi", # 主机内路径 "Destination": "/home", # docker容器内路径 "Mode": "", "RW": true, "Propagation": "rprivate" } ], ... [root@iZbp10a8w2ct7pZ home]# [root@iZbp10a8w2ct7pZ home]# docker attach e6782e5447e3 [root@e6782e5447e3 home]# ls [root@e6782e5447e3 home]# touch test.txt [root@e6782e5447e3 home]# ls test.txt [root@e6782e5447e3 /]# [root@iZbp10a8w2ct7pZ home]# [root@iZbp10a8w2ct7pZ home]# [root@iZbp10a8w2ct7pZ home]# ls ceshi [root@iZbp10a8w2ct7pZ home]# cd ceshi/ [root@iZbp10a8w2ct7pZ ceshi]# ls test.txt # 测试文件同步 [root@iZbp10a8w2ct7pZ ceshi]# [root@iZbp10a8w2ct7pZ ceshi]# docker attach e6782e5447e3 [root@e6782e5447e3 home]# exit exit [root@iZbp10a8w2ct7pZ ceshi]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@iZbp10a8w2ct7pZ ceshi]# ls test.txt [root@iZbp10a8w2ct7pZ ceshi]# vim test.txt [root@iZbp10a8w2ct7pZ ceshi]# cat test.txt hello, world! [root@iZbp10a8w2ct7pZ ceshi]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e6782e5447e3 centos "/bin/bash" 16 minutes ago Exited (0) 2 minutes ago gracious_haibt [root@iZbp10a8w2ct7pZ ceshi]# docker start e6782e5447e3 e6782e5447e3 [root@iZbp10a8w2ct7pZ ceshi]# docker attach e6782e5447e3 [root@e6782e5447e3 /]# cd /home [root@e6782e5447e3 home]# ls test.txt [root@e6782e5447e3 home]# cat test.txt # 文件数据同步成功 hello, world! mysql数据同步 # 可以挂载多个路径,-e配置环境,DockerHub搜索mysql查看使用方法 docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7 启动成功后,本地使用mysql图形界面管理工具连接数据库成功 将容器删除后mysql挂载在linux系统的数据和文件不会丢失
具名挂载和匿名挂载
docker run -d -P --name nginx01 -v 容器内路径 nginx # 匿名挂载,-P随机映射端口 docker volume ls # 查看所有volume的情况 docker run -d -P --name nginx02 -v 卷名:容器内路径 nginx # 具名挂载 -v 容器内路径 # 匿名挂载,不推荐使用 -v 卷名:容器内路径 # 具名挂载,所有的docker容器内的卷,没有指定目录的情况下都是在/var/lib/docker/volumes/卷名/_data -v /宿主机路径:容器内路径 # 指定路径挂载 设置权限,一旦权限确定就不能通过容器内部改变权限,只能通过宿主机来操作 docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:ro nginx # ro,readonly,只读 docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:rw nginx # rw,readwrite,读写 [root@iZbp10a8w2ct7pZ ~]# docker run -d -P --name nginx01 -v /etc/nginx nginx # docker run运行镜像,如果没有该镜像自动下载并运行 Unable to find image 'nginx:latest' locally latest: Pulling from library/nginx d121f8d1c412: Already exists ebd81fc8c071: Pull complete 655316c160af: Pull complete d15953c0e0f8: Pull complete 2ee525c5c3cc: Pull complete Digest: sha256:9a1f8ed9e2273e8b3bbcd2e200024adac624c2e5c9b1d420988809f5c0c41a5e Status: Downloaded newer image for nginx:latest bc4dcad6827a1f9d5c52f5c854c349814591d3ef90b75b9a2b49ae37367e87b9 [root@iZbp10a8w2ct7pZ ~]# docker volume ls DRIVER VOLUME NAME local 1c262ed249f8288f04cc33d736b6b5449c3cfdc97970d4b0977cddeef0015c88 # 没有名字只是一串字符串匿名挂载 local 4686bb8f3fa9eea72c8386ff6d55d9331bf62a0d2b4da1bbe202b5b45693082b local deb1ac57c31c6bb6d5d940240d2c35d7999560f16f4fb3daa5d3be49c66743a8 local f2ada5a5484150aac28d8270cbfc15d7153610b4d468ec17f1953b99dbdfd91d [root@iZbp10a8w2ct7pZ ~]# [root@iZbp10a8w2ct7pZ ~]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx # juming-nginx卷名,/juming-nginx是目录 0242c80485a49ecdf1a79d688c6bbf4a30c3cbb8be822ba3bc8ca001b53b7fe2 [root@iZbp10a8w2ct7pZ ~]# docker volume ls DRIVER VOLUME NAME local 1c262ed249f8288f04cc33d736b6b5449c3cfdc97970d4b0977cddeef0015c88 local 4686bb8f3fa9eea72c8386ff6d55d9331bf62a0d2b4da1bbe202b5b45693082b local deb1ac57c31c6bb6d5d940240d2c35d7999560f16f4fb3daa5d3be49c66743a8 local f2ada5a5484150aac28d8270cbfc15d7153610b4d468ec17f1953b99dbdfd91d local juming-nginx # 具名挂载 [root@iZbp10a8w2ct7pZ ~]# docker volume inspect juming-nginx [ { "CreatedAt": "2020-09-11T10:56:51+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data", # 挂载具体目录地址 "Name": "juming-nginx", "Options": null, "Scope": "local" } ] [root@iZbp10a8w2ct7pZ ~]# cd /var/lib/docker [root@iZbp10a8w2ct7pZ docker]# ls builder buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes [root@iZbp10a8w2ct7pZ docker]# cd volumes/ [root@iZbp10a8w2ct7pZ volumes]# ls 1c262ed249f8288f04cc33d736b6b5449c3cfdc97970d4b0977cddeef0015c88 deb1ac57c31c6bb6d5d940240d2c35d7999560f16f4fb3daa5d3be49c66743a8 juming-nginx 4686bb8f3fa9eea72c8386ff6d55d9331bf62a0d2b4da1bbe202b5b45693082b f2ada5a5484150aac28d8270cbfc15d7153610b4d468ec17f1953b99dbdfd91d metadata.db [root@iZbp10a8w2ct7pZ volumes]# cd juming-nginx/ [root@iZbp10a8w2ct7pZ juming-nginx]# ls _data [root@iZbp10a8w2ct7pZ juming-nginx]# cd _data/ [root@iZbp10a8w2ct7pZ _data]# ls conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf [root@iZbp10a8w2ct7pZ _data]#
初识dockfile
dockerfile就是用来构建docker镜像的文件,命令脚本,通过脚本生成镜像。 [root@iZbp10a8w2ct7pZ home]# ls [root@iZbp10a8w2ct7pZ home]# mkdir docker-test-volume [root@iZbp10a8w2ct7pZ home]# cd docker-test-volume/ [root@iZbp10a8w2ct7pZ docker-test-volume]# vim dockerfile1 [root@iZbp10a8w2ct7pZ docker-test-volume]# cat dockerfile1 # dockerfile文件每个命令就是镜像的一层 FROM centos VOLUME ["volume01","volume02"] # 挂载卷,匿名挂载。dockerfile文件使用“”,列表中元素不要加空格 CMD echo "-----end-----" CMD /bin/bash [root@iZbp10a8w2ct7pZ docker-test-volume]# docker build -f dockerfile1 -t xyp/centos . # 构建镜像。-f 地址,-t生成镜像,xyp/centos镜像名, .当前目录 Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos ---> 0d120b6ccaa8 Step 2/4 : VOLUME ["volume01","volume02"] ---> Running in e5409dcc3399 Removing intermediate container e5409dcc3399 ---> e1756afde2f0 Step 3/4 : CMD echo "-----end-----" ---> Running in 5cc2d440795d Removing intermediate container 5cc2d440795d ---> 22418aff3240 Step 4/4 : CMD /bin/bash ---> Running in 63765d3fe757 Removing intermediate container 63765d3fe757 ---> 859dd2ad60b6 Successfully built 859dd2ad60b6 Successfully tagged xyp/centos:latest [root@iZbp10a8w2ct7pZ docker-test-volume]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE xyp/centos latest 859dd2ad60b6 17 seconds ago 215MB # dockerfile1生成的镜像 nginx latest 7e4d58f0e5f3 20 hours ago 133MB mysql 5.7 ef08065b0a30 26 hours ago 448MB centos latest 0d120b6ccaa8 4 weeks ago 215MB [root@iZbp10a8w2ct7pZ docker-test-volume]# docker run -it 859dd2ad60b6 /bin/bash [root@e4d407e955e1 /]# ls -l total 0 lrwxrwxrwx 1 root root 7 May 11 2019 bin -> usr/bin drwxr-xr-x 5 root root 360 Sep 11 08:45 dev drwxr-xr-x 1 root root 66 Sep 11 08:45 etc drwxr-xr-x 2 root root 6 May 11 2019 home lrwxrwxrwx 1 root root 7 May 11 2019 lib -> usr/lib lrwxrwxrwx 1 root root 9 May 11 2019 lib64 -> usr/lib64 drwx------ 2 root root 6 Aug 9 21:40 lost+found drwxr-xr-x 2 root root 6 May 11 2019 media drwxr-xr-x 2 root root 6 May 11 2019 mnt drwxr-xr-x 2 root root 6 May 11 2019 opt dr-xr-xr-x 130 root root 0 Sep 11 08:45 proc dr-xr-x--- 2 root root 162 Aug 9 21:40 root drwxr-xr-x 11 root root 163 Aug 9 21:40 run lrwxrwxrwx 1 root root 8 May 11 2019 sbin -> usr/sbin drwxr-xr-x 2 root root 6 May 11 2019 srv dr-xr-xr-x 13 root root 0 Sep 11 08:45 sys drwxrwxrwt 7 root root 145 Aug 9 21:40 tmp drwxr-xr-x 12 root root 144 Aug 9 21:40 usr drwxr-xr-x 20 root root 262 Aug 9 21:40 var drwxr-xr-x 2 root root 6 Sep 11 08:45 volume01 # 生成镜像时自动挂载的数据卷目录 drwxr-xr-x 2 root root 6 Sep 11 08:45 volume02 # 生成镜像时自动挂载的数据卷目录 [root@e4d407e955e1 /]# [root@e4d407e955e1 /]# cd volume01 [root@e4d407e955e1 volume01]# ls [root@e4d407e955e1 volume01]# touch container.txt [root@e4d407e955e1 volume01]# ls container.txt [root@e4d407e955e1 volume01]# [root@iZbp10a8w2ct7pZ /]# # ctrl + p + q退出 [root@iZbp10a8w2ct7pZ /]# docker ps # CONTAINER ID 为e4d407e955e1,和容器内终端[root@e4d407e955e1 volume01]]#中 e4d407e955e1相同 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e4d407e955e1 859dd2ad60b6 "/bin/bash" 8 minutes ago Up 8 minutes angry_burnell [root@iZbp10a8w2ct7pZ /]# [root@iZbp10a8w2ct7pZ /]# docker inspect e4d407e955e1 # 查看卷挂载路径 ... "Mounts": [ { "Type": "volume", "Name": "e99639e1bb42e26f197763b77da8fb917072dda289d18240a2496f344d823e55", "Source": "/var/lib/docker/volumes/e99639e1bb42e26f197763b77da8fb917072dda289d18240a2496f344d823e55/_data", # 挂载容器外的目录 "Destination": "volume01", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "145ee13966150221c513f4f19d33bad712ec31f638138533443a841c4f46e511", "Source": "/var/lib/docker/volumes/145ee13966150221c513f4f19d33bad712ec31f638138533443a841c4f46e511/_data", # 挂载容器外的目录 "Destination": "volume02", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], ... # 这种方式挂载使用非常多,因为会经常构建我们自己的镜像。如果构建镜像时没有挂载卷,要手动镜像挂载 -v 卷名:容器内路径 [root@iZbp10a8w2ct7pZ /]# cd /var/lib/docker/volumes/e99639e1bb42e26f197763b77da8fb917072dda289d18240a2496f344d823e55/_data [root@iZbp10a8w2ct7pZ _data]# ls container.txt [root@iZbp10a8w2ct7pZ _data]#
数据卷容器
多个容器之间实现数据共享 [root@iZbp10a8w2ct7pZ ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE xyp/centos latest 859dd2ad60b6 32 minutes ago 215MB nginx latest 7e4d58f0e5f3 21 hours ago 133MB mysql 5.7 ef08065b0a30 26 hours ago 448MB centos latest 0d120b6ccaa8 4 weeks ago 215MB [root@iZbp10a8w2ct7pZ ~]# docker run -it --name docker01 xyp/centos # 启动docker01 [root@2ae6635131db /]# ls -l total 0 lrwxrwxrwx 1 root root 7 May 11 2019 bin -> usr/bin drwxr-xr-x 5 root root 360 Sep 11 09:18 dev drwxr-xr-x 1 root root 66 Sep 11 09:18 etc drwxr-xr-x 2 root root 6 May 11 2019 home lrwxrwxrwx 1 root root 7 May 11 2019 lib -> usr/lib lrwxrwxrwx 1 root root 9 May 11 2019 lib64 -> usr/lib64 drwx------ 2 root root 6 Aug 9 21:40 lost+found drwxr-xr-x 2 root root 6 May 11 2019 media drwxr-xr-x 2 root root 6 May 11 2019 mnt drwxr-xr-x 2 root root 6 May 11 2019 opt dr-xr-xr-x 131 root root 0 Sep 11 09:18 proc dr-xr-x--- 2 root root 162 Aug 9 21:40 root drwxr-xr-x 11 root root 163 Aug 9 21:40 run lrwxrwxrwx 1 root root 8 May 11 2019 sbin -> usr/sbin drwxr-xr-x 2 root root 6 May 11 2019 srv dr-xr-xr-x 13 root root 0 Sep 11 09:18 sys drwxrwxrwt 7 root root 145 Aug 9 21:40 tmp drwxr-xr-x 12 root root 144 Aug 9 21:40 usr drwxr-xr-x 20 root root 262 Aug 9 21:40 var drwxr-xr-x 2 root root 6 Sep 11 09:18 volume01 drwxr-xr-x 2 root root 6 Sep 11 09:18 volume02 [root@2ae6635131db /]# [root@iZbp10a8w2ct7pZ ~]# [root@iZbp10a8w2ct7pZ ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2ae6635131db xyp/centos "/bin/sh -c /bin/bash" About a minute ago Up About a minute docker01 [root@iZbp10a8w2ct7pZ ~]# docker run -it --name docker02 --volumes-from docker01 xyp/centos # --volumes-from,docker02挂载到了docker01,docker01数据卷容器 [root@020ef95e5a0e /]# ls -l total 0 lrwxrwxrwx 1 root root 7 May 11 2019 bin -> usr/bin drwxr-xr-x 5 root root 360 Sep 11 09:20 dev drwxr-xr-x 1 root root 66 Sep 11 09:20 etc drwxr-xr-x 2 root root 6 May 11 2019 home lrwxrwxrwx 1 root root 7 May 11 2019 lib -> usr/lib lrwxrwxrwx 1 root root 9 May 11 2019 lib64 -> usr/lib64 drwx------ 2 root root 6 Aug 9 21:40 lost+found drwxr-xr-x 2 root root 6 May 11 2019 media drwxr-xr-x 2 root root 6 May 11 2019 mnt drwxr-xr-x 2 root root 6 May 11 2019 opt dr-xr-xr-x 135 root root 0 Sep 11 09:20 proc dr-xr-x--- 2 root root 162 Aug 9 21:40 root drwxr-xr-x 11 root root 163 Aug 9 21:40 run lrwxrwxrwx 1 root root 8 May 11 2019 sbin -> usr/sbin drwxr-xr-x 2 root root 6 May 11 2019 srv dr-xr-xr-x 13 root root 0 Sep 11 09:20 sys drwxrwxrwt 7 root root 145 Aug 9 21:40 tmp drwxr-xr-x 12 root root 144 Aug 9 21:40 usr drwxr-xr-x 20 root root 262 Aug 9 21:40 var drwxr-xr-x 2 root root 6 Sep 11 09:18 volume01 drwxr-xr-x 2 root root 6 Sep 11 09:18 volume02 [root@020ef95e5a0e /]# [root@iZbp10a8w2ct7pZ ~]# [root@iZbp10a8w2ct7pZ ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 020ef95e5a0e xyp/centos "/bin/sh -c /bin/bash" About a minute ago Up About a minute docker02 2ae6635131db xyp/centos "/bin/sh -c /bin/bash" 3 minutes ago Up 3 minutes docker01 [root@iZbp10a8w2ct7pZ ~]# docker attach 2ae6635131db # 进入docker01 [root@2ae6635131db /]# ls -l total 0 lrwxrwxrwx 1 root root 7 May 11 2019 bin -> usr/bin drwxr-xr-x 5 root root 360 Sep 11 09:18 dev drwxr-xr-x 1 root root 66 Sep 11 09:18 etc drwxr-xr-x 2 root root 6 May 11 2019 home lrwxrwxrwx 1 root root 7 May 11 2019 lib -> usr/lib lrwxrwxrwx 1 root root 9 May 11 2019 lib64 -> usr/lib64 drwx------ 2 root root 6 Aug 9 21:40 lost+found drwxr-xr-x 2 root root 6 May 11 2019 media drwxr-xr-x 2 root root 6 May 11 2019 mnt drwxr-xr-x 2 root root 6 May 11 2019 opt dr-xr-xr-x 134 root root 0 Sep 11 09:18 proc dr-xr-x--- 2 root root 162 Aug 9 21:40 root drwxr-xr-x 11 root root 163 Aug 9 21:40 run lrwxrwxrwx 1 root root 8 May 11 2019 sbin -> usr/sbin drwxr-xr-x 2 root root 6 May 11 2019 srv dr-xr-xr-x 13 root root 0 Sep 11 09:18 sys drwxrwxrwt 7 root root 145 Aug 9 21:40 tmp drwxr-xr-x 12 root root 144 Aug 9 21:40 usr drwxr-xr-x 20 root root 262 Aug 9 21:40 var drwxr-xr-x 2 root root 6 Sep 11 09:18 volume01 drwxr-xr-x 2 root root 6 Sep 11 09:18 volume02 [root@2ae6635131db /]# cd volume01 [root@2ae6635131db volume01]# touch docker01 [root@2ae6635131db volume01]# ls docker01 [root@2ae6635131db /]# [root@iZbp10a8w2ct7pZ ~]# [root@iZbp10a8w2ct7pZ ~]# docker attach 020ef95e5a0e [root@020ef95e5a0e /]# cd volume01 [root@020ef95e5a0e volume01]# ls # docker01创建的数据同步到docker02上 docker01 [root@020ef95e5a0e volume01]# read escape sequence # ctrl + p + q [root@iZbp10a8w2ct7pZ ~]# docker run -it --name docker03 --volumes-from docker01 xyp/centos # docker02挂载到docker01数据卷 [root@19515c5f1cab /]# ls -l total 0 lrwxrwxrwx 1 root root 7 May 11 2019 bin -> usr/bin drwxr-xr-x 5 root root 360 Sep 11 09:30 dev drwxr-xr-x 1 root root 66 Sep 11 09:30 etc drwxr-xr-x 2 root root 6 May 11 2019 home lrwxrwxrwx 1 root root 7 May 11 2019 lib -> usr/lib lrwxrwxrwx 1 root root 9 May 11 2019 lib64 -> usr/lib64 drwx------ 2 root root 6 Aug 9 21:40 lost+found drwxr-xr-x 2 root root 6 May 11 2019 media drwxr-xr-x 2 root root 6 May 11 2019 mnt drwxr-xr-x 2 root root 6 May 11 2019 opt dr-xr-xr-x 135 root root 0 Sep 11 09:30 proc dr-xr-x--- 2 root root 162 Aug 9 21:40 root drwxr-xr-x 11 root root 163 Aug 9 21:40 run lrwxrwxrwx 1 root root 8 May 11 2019 sbin -> usr/sbin drwxr-xr-x 2 root root 6 May 11 2019 srv dr-xr-xr-x 13 root root 0 Sep 11 09:30 sys drwxrwxrwt 7 root root 145 Aug 9 21:40 tmp drwxr-xr-x 12 root root 144 Aug 9 21:40 usr drwxr-xr-x 20 root root 262 Aug 9 21:40 var drwxr-xr-x 2 root root 22 Sep 11 09:22 volume01 drwxr-xr-x 2 root root 6 Sep 11 09:18 volume02 [root@19515c5f1cab /]# cd volume01 [root@19515c5f1cab volume01]# ls # docker03与docker01数据同步 docker01 [root@19515c5f1cab volume01]# touch docker03 [root@19515c5f1cab volume01]# ls docker01 docker03 [root@19515c5f1cab volume01]# [root@iZbp10a8w2ct7pZ ~]# [root@iZbp10a8w2ct7pZ ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 19515c5f1cab xyp/centos "/bin/sh -c /bin/bash" 3 minutes ago Up 3 minutes docker03 020ef95e5a0e xyp/centos "/bin/sh -c /bin/bash" 12 minutes ago Up 12 minutes docker02 2ae6635131db xyp/centos "/bin/sh -c /bin/bash" 15 minutes ago Up 15 minutes docker01 [root@iZbp10a8w2ct7pZ ~]# docker attach 2ae6635131db [root@2ae6635131db volume01]# ls # docker01和docker03数据同步 docker01 docker03 [root@2ae6635131db volume01]# lsread escape sequence [root@iZbp10a8w2ct7pZ ~]# docker attach 020ef95e5a0e [root@020ef95e5a0e volume01]# ls # docker02和docker03数据同步 docker01 docker03 [root@020ef95e5a0e volume01]# read escape sequence [root@iZbp10a8w2ct7pZ ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 19515c5f1cab xyp/centos "/bin/sh -c /bin/bash" 5 minutes ago Up 5 minutes docker03 020ef95e5a0e xyp/centos "/bin/sh -c /bin/bash" 15 minutes ago Up 15 minutes docker02 2ae6635131db xyp/centos "/bin/sh -c /bin/bash" 17 minutes ago Up 17 minutes docker01 [root@iZbp10a8w2ct7pZ ~]# docker kill 2ae6635131db 2ae6635131db [root@iZbp10a8w2ct7pZ ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 19515c5f1cab xyp/centos "/bin/sh -c /bin/bash" 6 minutes ago Up 5 minutes docker03 020ef95e5a0e xyp/centos "/bin/sh -c /bin/bash" 15 minutes ago Up 15 minutes docker02 2ae6635131db xyp/centos "/bin/sh -c /bin/bash" 17 minutes ago Exited (137) 13 seconds ago docker01 [root@iZbp10a8w2ct7pZ ~]# docker rm -f 2ae6635131db # 删除docker01 2ae6635131db [root@iZbp10a8w2ct7pZ ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 19515c5f1cab xyp/centos "/bin/sh -c /bin/bash" 8 minutes ago Up 8 minutes docker03 020ef95e5a0e xyp/centos "/bin/sh -c /bin/bash" 18 minutes ago Up 18 minutes docker02 [root@iZbp10a8w2ct7pZ ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 19515c5f1cab xyp/centos "/bin/sh -c /bin/bash" 9 minutes ago Up 9 minutes docker03 020ef95e5a0e xyp/centos "/bin/sh -c /bin/bash" 18 minutes ago Up 18 minutes docker02 [root@iZbp10a8w2ct7pZ ~]# docker attach 020ef95e5a0e [root@020ef95e5a0e volume01]# ls # 删除docker01,docker02数据同样存在,docker03也一样 docker01 docker03 [root@020ef95e5a0e volume01]# 多个mysql实现数据共享 docker run -d -p 3310:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7 docker run -d -p 3310:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volumes-from mysql01 mysql:5.7
Dockerfile
介绍 dockerfile是用来构建docker镜像的文件,命令脚本。 构建步骤: 编写dockfile文件 docker build构建成为一个镜像 docker run运行镜像 docker push 发布镜像(DockerHub、阿里云镜像仓库) 构建过程 每个保留关键字(指令)都必须是大写字母 执行从上到下顺序进行 # 表示注释 每个指令都会创建提交一个新的镜像层,并提交 DockerFile: 构建文件,定义一切步骤,源代码 DockerImages: 通过dockerfile构建生成的镜像,最终发布和运行的产品 Docker容器: 容器就是镜像运行起来提供服务的 指令说明 FROM # 基础镜像 MAINTAINER # 镜像维护者,姓名+邮箱 RUN # 镜像运行命令 ADD # 步骤,添加内容 WORKDIR # 镜像的工作目录 VOLUME # 挂载的目录 EXPOSE # 保留端口设置 CMD # 指定容器启动时运行的命令,只有最后一个会生效可被替代 ENTRYPOINT # 指定容器启动时运行的命令,可以追加命令 ONBUILD # 构建被继承DockerFile时运行ONBULID指令,指令出发 COPY # 类似ADD命令,将文件拷贝到镜像中 ENV # 构建镜像时设置环境变量 构建自己的centos DockerHub搜索centos,找到centos7跳转进github中 FROM scratch # 基础镜像,DockerHub中99%镜像都是这个基础镜像 ADD centos-7-x86_64-docker.tar.xz / # 添加centos7压缩包 LABEL \ org.label-schema.schema-version="1.0" \ org.label-schema.name="CentOS Base Image" \ org.label-schema.vendor="CentOS" \ org.label-schema.license="GPLv2" \ org.label-schema.build-date="20200809" \ org.opencontainers.image.title="CentOS Base Image" \ org.opencontainers.image.vendor="CentOS" \ org.opencontainers.image.licenses="GPL-2.0-only" \ org.opencontainers.image.created="2020-08-09 00:00:00+01:00" CMD ["/bin/bash"] 1.编写dockerfile文件 root@iZbp10a8w2ct7pZ home]# ls docker-test-volume [root@iZbp10a8w2ct7pZ home]# mkdir dockerfile [root@iZbp10a8w2ct7pZ home]# cd dockerfile/ [root@iZbp10a8w2ct7pZ dockerfile]# vim mydockercentos # 编写dockerfile文件 [root@iZbp10a8w2ct7pZ dockerfile]# cat mydockercentos FROM centos MAINTAINER xyp<634338225@qq.com> ENV MYPATH /usr/local WORKDIR $MYPATH RUN yum -y install vim RUN yum -y install net-tools # ifconfig EXPOSE 80 CMD echo $MYPATH CMD echo "-----end-----" CMD /bin/bash 2.构建镜像 [root@iZbp10a8w2ct7pZ dockerfile]# docker build -f mydockercentos -t mycentos:0.1 . # 构建镜像,-f文件,-t tag, .当前目录 Sending build context to Docker daemon 2.048kB Step 1/10 : FROM centos ---> 0d120b6ccaa8 # 0d120b6ccaa8 docker images已经有centos了,所以直接拿来用 Step 2/10 : MAINTAINER xyp<634338225@qq.com> ---> Running in 64164f664934 Removing intermediate container 64164f664934 ---> cd6f03fb2eac Step 3/10 : ENV MYPATH /usr/local ---> Running in cf799f2e632d Removing intermediate container cf799f2e632d ---> 9a3dd2c23724 Step 4/10 : WORKDIR $MYPATH ---> Running in 00b78d805c85 Removing intermediate container 00b78d805c85 ---> a9601792225a Step 5/10 : RUN yum -y install vim ---> Running in 89e5c7b6291e ...... Step 6/10 : RUN yum -y install net-tools ---> Running in de49a3389045 ...... Step 7/10 : EXPOSE 80 ---> Running in fe8fb115d5ff Removing intermediate container fe8fb115d5ff ---> 8d87be4ea47b Step 8/10 : CMD echo $MYPATH ---> Running in d888190a2ca2 Removing intermediate container d888190a2ca2 ---> e37a46ff1f35 Step 9/10 : CMD echo "-----end-----" ---> Running in 17277dd683bb Removing intermediate container 17277dd683bb ---> 898763dddfec Step 10/10 : CMD /bin/bash ---> Running in e66ec505f302 Removing intermediate container e66ec505f302 ---> ca394e3f127c Successfully built ca394e3f127c Successfully tagged mycentos:0.1 3.测试 [root@iZbp10a8w2ct7pZ dockerfile]# docker run -it mycentos:0.1 [root@8a91c7375d56 local]# pwd # 默认进入的容器路径是根目录/,dockerfile文件配置ENV为/use/local /usr/local [root@8a91c7375d56 local]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 11 bytes 866 (866.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@8a91c7375d56 local]# vim test [root@8a91c7375d56 local]# cat test hello,world! 4.镜像历史 [root@iZbp10a8w2ct7pZ dockerfile]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mycentos 0.1 ca394e3f127c 9 minutes ago 295MB xyp/centos latest 859dd2ad60b6 3 days ago 215MB nginx latest 7e4d58f0e5f3 3 days ago 133MB mysql 5.7 ef08065b0a30 4 days ago 448MB centos latest 0d120b6ccaa8 4 weeks ago 215MB [root@iZbp10a8w2ct7pZ dockerfile]# docker history ca394e3f127c # docker history 容器id IMAGE CREATED CREATED BY SIZE COMMENT ca394e3f127c 9 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B 898763dddfec 9 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B e37a46ff1f35 9 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B 8d87be4ea47b 9 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B acb0d96ce4a0 9 minutes ago /bin/sh -c yum -y install net-tools 22.8MB fdfd29805dd0 9 minutes ago /bin/sh -c yum -y install vim 57.2MB a9601792225a 9 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B 9a3dd2c23724 9 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B cd6f03fb2eac 9 minutes ago /bin/sh -c #(nop) MAINTAINER xyp<634338225@… 0B 0d120b6ccaa8 4 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 4 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 4 weeks ago /bin/sh -c #(nop) ADD file:538afc0c5c964ce0d… 215MB
发布镜像
发布到DockerHub docker login -u username docker push 镜像:版本 [root@iZbp10a8w2ct7pZ /]# docker login -u 634338225 Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded [root@iZbp10a8w2ct7pZ /]# 发布到阿里云镜像仓库 1.创建命名空间 2.创建镜像仓库 3.按照阿里云官方文档操作
Docker网络
理解Docker0 清空所有环境 [root@iZbp10a8w2ct7pZ /]# docker rm -f $(docker ps -aq) 8a91c7375d56 5e3f11cf0321 7a43a61a48dd b409da379658 19515c5f1cab 020ef95e5a0e e4d407e955e1 0eb6a3bcf479 0242c80485a4 bc4dcad6827a b0a2d40b2944 [root@iZbp10a8w2ct7pZ /]# docker rmi -f $(docker images -aq) Untagged: mycentos:0.1 Deleted: sha256:ca394e3f127cfbda3f9bdfd56fd207216456997e38f794c325c55195dfca6aab Deleted: sha256:898763dddfec36bae6f49535fdd7386376184783756940ee7d065c760f1e0820 Deleted: sha256:e37a46ff1f356366db14c61c0d8d53acf186943de312ab6b171f8e22fadfe996 Deleted: sha256:8d87be4ea47ba14b64f8c103c270f71651407e98414b91ee722dbfe4ac817db2 Deleted: sha256:acb0d96ce4a0634e4d4a41149634233072c53e99726c9c79b3f6ad96d4ace8bf Deleted: sha256:08e269cbe24f078d16ab1bac86fe19ded07f0f0df39e85a2098beda2c135a81c Deleted: sha256:fdfd29805dd0e96c3b12646e74c3567e150763ef32caef2f72422455f04606e2 Deleted: sha256:22874a7594abedbc95b736a7944aa7a92b20190b5bf217b9e0c171c87cc7623d Deleted: sha256:a9601792225a70aa7ea7f92332d731db77d909db39b9ec059fd5210a593c8245 Deleted: sha256:9a3dd2c23724546c645f12c58221ae01008d191a9523b61433adca6a92583a48 Deleted: sha256:cd6f03fb2eac09b7856edae190ce5b340bfa525968cb02e72ca47ae9593dc93e Untagged: xyp/centos:latest Deleted: sha256:859dd2ad60b61e8170c1e6bb63d820b9518e1bd55d2234615882fd4959106481 Deleted: sha256:22418aff324094a25b9903ffeb60056372ae946e3a8e9cc963e36051a287ec93 Deleted: sha256:e1756afde2f0f6f5febc4d2d438cfc3edb55a4f4f1bafbd6b12fd52e7f4f03bc Untagged: nginx:latest Untagged: nginx@sha256:9a1f8ed9e2273e8b3bbcd2e200024adac624c2e5c9b1d420988809f5c0c41a5e Deleted: sha256:7e4d58f0e5f3b60077e9a5d96b4be1b974b5a484f54f9393000a99f3b6816e3d Deleted: sha256:eedfd37abc0b05520a719addf6675b372687fe9a2ed1746fa988335720d4376c Deleted: sha256:4f052ddd517f6dc5b74d2f7d881838b8ec6a5e67ec7a1da8bbbe91e1e068ad3a Deleted: sha256:5f78154d356577280e4ac0d9bf0d021d488828e57f953b011dcbadaecd539ce4 Deleted: sha256:db5783668220e16186080f3e9d69ad748aab8c72abb8e457f6fc45369461634b Untagged: mysql:5.7 Untagged: mysql@sha256:14fd47ec8724954b63d1a236d2299b8da25c9bbb8eacc739bb88038d82da4919 Deleted: sha256:ef08065b0a302111b56966aa92c89fa0bacdfc537741cbca88a15b10f14332ca Deleted: sha256:c8c81ac92392c394197759ca3d50f5f843d85ac1550d8c0bb2b21adc7334100d Deleted: sha256:2dc86a1b9b92e7c946c684bd349e448d7c4fbb3236686e1a48ddfe5adb86a425 Deleted: sha256:97b541df82456d38e987b630870fcd4e39f05f016717652466b3466841f4162e Deleted: sha256:aded9a11fc54761c770a9075cfc2d0bb72c72b59171a56cfa4322ab2b2d416e7 Deleted: sha256:3fc742b087b51753835895403bd70b649bf2e4e32c5eb5f2f15c2b7d22409964 Deleted: sha256:00d53c8dcb5c8fd12fbfd2b3b25afcbc3a044e73f1fd395af231f86827238fcf Deleted: sha256:007d9a78de83bec432afb0e4e847072a6943e853456fe21b70488d25c1c96bf4 Deleted: sha256:31fd54b7e8a4e80375cbfd9ead44c37e0ee03d8c4ca7ace27cc1896e9d40e2a2 Deleted: sha256:22d626b2851ecf92af46f616ce1ff0ef0879ef23b68be2fb92be3311f2308763 Deleted: sha256:5da80bf7a61d70d69c153f015cd2a5aa96594f36f3835ef6dbcd4da41da374f2 Deleted: sha256:07cab433985205f29909739f511777a810f4a9aff486355b71308bb654cdc868 Untagged: centos:latest Untagged: centos@sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd Deleted: sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566 Deleted: sha256:291f6e44771a7b4399b0c6fb40ab4fe0331ddf76eda11080f052b003d96c7726 Error response from daemon: conflict: unable to delete 898763dddfec (cannot be forced) - image has dependent child images Error: No such image: 8d87be4ea47b Error: No such image: e37a46ff1f35 Error: No such image: acb0d96ce4a0 Error: No such image: fdfd29805dd0 Error: No such image: 9a3dd2c23724 Error: No such image: a9601792225a Error: No such image: cd6f03fb2eac Error: No such image: 22418aff3240 Error: No such image: e1756afde2f0 [root@iZbp10a8w2ct7pZ /]# 测试 [root@iZbp10a8w2ct7pZ /]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo # 127.0.0.1 本机回环地址 valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:16:3e:0b:70:c5 brd ff:ff:ff:ff:ff:ff inet 172.16.136.181/20 brd 172.16.143.255 scope global dynamic noprefixroute eth0 # 172.16.136.181 阿里云内网地址 valid_lft 308878656sec preferred_lft 308878656sec inet6 fe80::216:3eff:fe0b:70c5/64 scope link valid_lft forever preferred_lft forever 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:ae:df:d8:31 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 # 172.17.0.1 docker0地址 valid_lft forever preferred_lft forever inet6 fe80::42:aeff:fedf:d831/64 scope link valid_lft forever preferred_lft forever docker0和容器之间桥接... --link容器互联 ...
自定义网络
查看所有的docker网络 [root@iZbp10a8w2ct7pZ ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 3c4d44d1a438 bridge bridge local 680eb3f56321 host host local b5d36dc07adc none null local [root@iZbp10a8w2ct7pZ ~]# 网络模式 bridge:桥接,docker默认,自己创建也使用bridge模式 none:不配置网络 host:和宿主机共享连接 container:容器网络互联,用的少局限性大 docker run -d -P --name tomcat01 --net bridge tomcat # --net bridge即docker0 docker0特点:默认,域名不能访问,--link可以实现容器间连接 docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet # 自定义网络,driver bridge桥接,subnet子网,gateway网关 [root@iZbp10a8w2ct7pZ ~]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet 47d322e7f7befe348a197d452f0dd3c316fb21dfd85c503802ed6266efa7bdbe [root@iZbp10a8w2ct7pZ ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 3c4d44d1a438 bridge bridge local 680eb3f56321 host host local 47d322e7f7be mynet bridge local # 创建的网络 b5d36dc07adc none null local [root@iZbp10a8w2ct7pZ ~]# docker network inspect mynet [ { "Name": "mynet", "Id": "47d322e7f7befe348a197d452f0dd3c316fb21dfd85c503802ed6266efa7bdbe", "Created": "2020-09-16T15:25:14.725287691+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "192.168.0.0/16", "Gateway": "192.168.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": {}, "Options": {}, "Labels": {} } ] [root@iZbp10a8w2ct7pZ ~]# docker run -d -P --name tomcat-net-01 --net mynet tomcat ... [root@iZbp10a8w2ct7pZ ~]# docker run -d -P --name tomcat-net-02 --net mynet tomcat ... [root@iZbp10a8w2ct7pZ ~]# docker network inspect mynet # 192.168.0.2为tomcat-net-01对应的IP,192.168.0.3为tomcat-net-02对应的IP ... [root@iZbp10a8w2ct7pZ ~]# docker exex -it tomcat-net-01 ping 192.168.0.3 # ping成功 ... [root@iZbp10a8w2ct7pZ ~]# docker exex -it tomcat-net-01 ping tomcat-net-02 # 不使用--link也可以直接ping通名字了 ... 自定义网络docker已经维护好了对应的关系,推荐使用这种方法,不推荐--link redis,mysql等不同的集群使用不同的网络,保证集群是安全的
docker0和自定义网络关联
[root@iZbp10a8w2ct7pZ ~]# docker network --help Usage: docker network COMMAND Manage networks Commands: connect Connect a container to a network create Create a network disconnect Disconnect a container from a network inspect Display detailed information on one or more networks ls List networks prune Remove all unused networks rm Remove one or more networks Run 'docker network COMMAND --help' for more information on a command. [root@iZbp10a8w2ct7pZ ~]# docker network connect --help Usage: docker network connect [OPTIONS] NETWORK CONTAINER Connect a container to a network Options: --alias strings Add network-scoped alias for the container --driver-opt strings driver options for the network --ip string IPv4 address (e.g., 172.30.100.104) --ip6 string IPv6 address (e.g., 2001:db8::33) --link list Add link to another container --link-local-ip strings Add a link-local address for the container [root@iZbp10a8w2ct7pZ ~]# docker network connect mynet tomcat01 # tomcat01 docker0关联的镜像 ... [root@iZbp10a8w2ct7pZ ~]# docker network connect mynet tomcat02 # tomcat02 docker0关联的镜像 ...