赞
踩
我们的代码环境到最后发布市场一般都要经过开发环境,测试环境和生产环境,正常情况下,开发人员开发好的代码发送给测试人员,由测试人员在测试环境上测试,测试成功后,就会部署到生产环境,让运维人员维护。
但是也会存在异常情况,如:
开发人员在python3.7的环境开发的代码发送给测试人员,而测试人员是在python2.7的测试环境进行部署的, 这样就会导致测试人员运行代码出现bug,向开发人员反馈,开发人员就纳闷,我在我的环境运行都是正常的啊,于是,由于中间件环境的影响,导致开发环境和测试环境存在差异,代码无法正常使用。这是典型的水土不服啊。
为了应对这种水土不服,那开发人员能不能用一种全新的方式把水土和代码都移植给测试人员呢?容器应用而生,它就实现了把python2.7到python3.7的水土环境和代码一起发给了测试人员,测试人员也不用再担心因为环境的原因影响测试效果。这样就很好的解决了兼容性问题。
Docker使用客户端-服务器架构。Docker 客户端与Docker 守护进程进行对话,该守护进程完成了构建,运行和分发Docker容器的繁重工作。Docker客户端和守护程序可以 在同一系统上运行,或者您可以将Docker客户端连接到远程Docker守护程序。Docker客户端和守护程序在UNIX套接字或网络接口上使用REST API进行通信。
yum update
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce
docker -v
docker仓库(Registory)有两种,Docker hub和private registry,Docker hub(https://hub.docker.com)属于公共镜像仓库,一般都防止在国外,我们可以配置国内镜像加速器来加快docker hub下载速度。
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://bdxzq1r4.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
systemctl start docker
systemctl stauts docker
systemctl stop docker
systemctl restart docker
systemctl enable docker
[root@docker yum.repos.d]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker yum.repos.d]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [root@docker yum.repos.d]# docker search redis NAME DESCRIPTION STARS OFFICIAL AUTOMATED redis Redis is an open source key-value store that… 8034 [OK] bitnami/redis Bitnami Redis Docker Image 141 [OK] sameersbn/redis 79 [OK] grokzen/redis-cluster Redis cluster 3.0, 3.2, 4.0 & 5.0 65 rediscommander/redis-commander Alpine image for redis-commander - Redis man… 37 [OK] kubeguide/redis-master redis-master with "Hello World!" 31 redislabs/redis Clustered in-memory database engine compatib… 24 redislabs/redisearch Redis With the RedisSearch module pre-loaded… 21 oliver006/redis_exporter Prometheus Exporter for Redis Metrics. Supp… 21 arm32v7/redis Redis is an open source key-value store that… 21 bitnami/redis-sentinel Bitnami Docker Image for Redis Sentinel 12 [OK] webhippie/redis Docker images for Redis 11 [OK] redislabs/redisgraph A graph database module for Redis 10 [OK] s7anley/redis-sentinel-docker Redis Sentinel 9 [OK] arm64v8/redis Redis is an open source key-value store that… 9 insready/redis-stat Docker image for the real-time Redis monitor… 9 [OK] redislabs/redismod An automated build of redismod - latest Redi… 7 [OK] centos/redis-32-centos7 Redis in-memory data structure store, used a… 5 redislabs/redisinsight RedisInsight - The GUI for Redis 5 circleci/redis CircleCI images for Redis 4 [OK] clearlinux/redis Redis key-value data structure server with t… 2 wodby/redis Redis container image with orchestration 1 [OK] tiredofit/redis Redis Server w/ Zabbix monitoring and S6 Ove… 1 [OK] runnable/redis-stunnel stunnel to redis provided by linking contain… 1 [OK] xetamus/redis-resource forked redis-resource 0 [OK]
[root@docker yum.repos.d]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
c499e6d256d6: Pull complete
bf1bc8a5a7e4: Pull complete
7564fb795604: Pull complete
ec6e86f783e4: Pull complete
1371d6223f46: Pull complete
021fd554320f: Pull complete
Digest: sha256:a732b1359e338a539c25346a50bf0a501120c41dc248d868e546b33e32bf4fe4
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
可以看到已经下载成功
[root@docker yum.repos.d]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 4cdbec704e47 2 weeks ago 98.2MB
如果需要下载对应的版本号,可以先到官网查看下redis的版本信息
[root@docker yum.repos.d]# docker pull redis:6.0-rc
6.0-rc: Pulling from library/redis
c499e6d256d6: Already exists
bf1bc8a5a7e4: Already exists
7564fb795604: Already exists
3cd873a7c410: Pull complete
0b301c8e1d2d: Pull complete
fd94dfb55d0e: Pull complete
Digest: sha256:cd55cd7447488fc644884bfece112c619f7940ac39b03df826c36d0ec84772fc
Status: Downloaded newer image for redis:6.0-rc
docker.io/library/redis:6.0-rc
[root@docker yum.repos.d]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 6.0-rc a3bebd1090ce 2 weeks ago 104MB
redis latest 4cdbec704e47 2 weeks ago 98.2MB
[root@docker yum.repos.d]# docker rmi a3bebd1090ce
Untagged: redis:6.0-rc
Untagged: redis@sha256:cd55cd7447488fc644884bfece112c619f7940ac39b03df826c36d0ec84772fc
Deleted: sha256:a3bebd1090ce23e24fa77bd2cff464af43c700c3109487ebe9fa112c31ca653c
Deleted: sha256:b1591c0b280a41906c8db3769d0f55d5a0a610ef005b41b6348c911b8ecc22ba
Deleted: sha256:9401ef6c4a5478be84644dfd49727f62ba25f1ddf1534cf0bff0d57fe7efae21
Deleted: sha256:68dccea5b7f1ef7bf25f4aad48f13c7126a6d0cbe8ffb3b6443939bec9c2a746
[root@docker yum.repos.d]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 4cdbec704e47 2 weeks ago 98.2MB
[root@docker yum.repos.d]# docker rmi redis:latest
Untagged: redis:latest
Untagged: redis@sha256:a732b1359e338a539c25346a50bf0a501120c41dc248d868e546b33e32bf4fe4
Deleted: sha256:4cdbec704e477aab9d249262e60b9a8a25cbef48f0ff23ac5eae879a98a7ebd0
Deleted: sha256:c844d6fba483bf3c0e4f62491a85943654f989ae50b59299a55da4be5f2caf94
Deleted: sha256:d231c4db7f81ecc2f6cf0ae546481d59615645f4313268ff1098731195066d6a
Deleted: sha256:0f6a2a93807106ed5097541095f1bff014c6c4bca565e32a40a907c62abc1952
Deleted: sha256:80cfcce29d3c062c592a64c7fc1e25e91c28ef926e787fa3e0db3b28e5a42249
Deleted: sha256:a8f3582e4f96758f10a36eaa93c8aef7b5d647f0a00f5e2deb8344ce3dc6b760
Deleted: sha256:c3a984abe8a88059915bb6c7a1d249fd1ccc16d931334ac8816540b0eb686b45
[root@docker yum.repos.d]# docker rmi `docker images -q`
[root@docker yum.repos.d]# docker run -it --name=c1 centos /bin/bash [root@3ff94a0caa60 /]# ls -al total 0 drwxr-xr-x. 1 root root 6 Apr 16 10:26 . drwxr-xr-x. 1 root root 6 Apr 16 10:26 .. -rwxr-xr-x. 1 root root 0 Apr 16 10:26 .dockerenv lrwxrwxrwx. 1 root root 7 May 11 2019 bin -> usr/bin drwxr-xr-x. 5 root root 360 Apr 16 10:26 dev drwxr-xr-x. 1 root root 66 Apr 16 10:26 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 Jan 13 21:48 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. 127 root root 0 Apr 16 10:26 proc dr-xr-x---. 2 root root 162 Jan 13 21:49 root drwxr-xr-x. 11 root root 163 Jan 13 21:49 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 Apr 16 08:52 sys drwxrwxrwt. 7 root root 145 Jan 13 21:49 tmp drwxr-xr-x. 12 root root 144 Jan 13 21:49 usr drwxr-xr-x. 20 root root 262 Jan 13 21:49 var
[root@3ff94a0caa60 /]# exit
exit
[root@docker yum.repos.d]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker yum.repos.d]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3ff94a0caa60 centos "/bin/bash" About a minute ago Exited (0) 10 seconds ago c1
[root@docker yum.repos.d]# docker run -id --name=c2 centos:latest
b29c30f91eeb81e5654b68585d1d539f2fbe5006f77cde09cbb7c506decfb482
[root@docker yum.repos.d]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b29c30f91eeb centos:latest "/bin/bash" 5 seconds ago Up 4 seconds c2
[root@docker yum.repos.d]# docker exec -it c2 /bin/bash
[root@b29c30f91eeb /]# exit
exit
[root@docker yum.repos.d]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b29c30f91eeb centos:latest "/bin/bash" About a minute ago Up About a minute c2
3ff94a0caa60 centos "/bin/bash" 7 minutes ago Exited (0) 6 minutes ago c1
[root@docker yum.repos.d]#
[root@docker yum.repos.d]# docker start c2
c2
[root@docker yum.repos.d]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b29c30f91eeb centos:latest "/bin/bash" 7 minutes ago Up 5 seconds c2
3ff94a0caa60 centos "/bin/bash" 13 minutes ago Exited (0) 12 minutes ago c1
[root@docker yum.repos.d]# docker stop c2
c2
[root@docker yum.repos.d]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b29c30f91eeb centos:latest "/bin/bash" 7 minutes ago Exited (137) 6 seconds ago c2
3ff94a0caa60 centos "/bin/bash" 13 minutes ago Exited (0) 11 minutes ago c1
[root@docker yum.repos.d]# docker rm c1
c1
[root@docker yum.repos.d]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b29c30f91eeb centos:latest "/bin/bash" 8 minutes ago Up 56 seconds c2
删除所有容器
[root@docker yum.repos.d]# docker rm `docker ps -aq`
Error response from daemon: You cannot remove a running container b29c30f91eeb81e5654b68585d1d539f2fbe5006f77cde09cbb7c506decfb482. Stop the container before attempting removal or force remove
这里由于c2还在运行,所以报错,如果要删除c2,需要先stop再删除
[root@docker yum.repos.d]# docker inspect c2 [ { "Id": "b29c30f91eeb81e5654b68585d1d539f2fbe5006f77cde09cbb7c506decfb482", "Created": "2020-04-16T10:32:51.084489295Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 75393, "ExitCode": 0, "Error": "", "StartedAt": "2020-04-16T10:40:20.616062424Z", "FinishedAt": "2020-04-16T10:39:50.162033543Z" }, "Image": "sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1ee", "ResolvConfPath": "/var/lib/docker/containers/b29c30f91eeb81e5654b68585d1d539f2fbe5006f77cde09cbb7c506decfb482/resolv.conf", "HostnamePath": "/var/lib/docker/containers/b29c30f91eeb81e5654b68585d1d539f2fbe5006f77cde09cbb7c506decfb482/hostname", "HostsPath": "/var/lib/docker/containers/b29c30f91eeb81e5654b68585d1d539f2fbe5006f77cde09cbb7c506decfb482/hosts", "LogPath": "/var/lib/docker/containers/b29c30f91eeb81e5654b68585d1d539f2fbe5006f77cde09cbb7c506decfb482/b29c30f91eeb81e5654b68585d1d539f2fbe5006f77cde09cbb7c506decfb482-json.log", "Name": "/c2", "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/c80b2937536acb1f4196203f127c40bddc75bfa88e9562d103c8a82c0a8c7b4d-init/diff:/var/lib/docker/overlay2/4397e473bbca2dc2d6d04a72569e11a8961182664b3b23baf954a7991d095760/diff", "MergedDir": "/var/lib/docker/overlay2/c80b2937536acb1f4196203f127c40bddc75bfa88e9562d103c8a82c0a8c7b4d/merged", "UpperDir": "/var/lib/docker/overlay2/c80b2937536acb1f4196203f127c40bddc75bfa88e9562d103c8a82c0a8c7b4d/diff", "WorkDir": "/var/lib/docker/overlay2/c80b2937536acb1f4196203f127c40bddc75bfa88e9562d103c8a82c0a8c7b4d/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "b29c30f91eeb", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": true, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash" ], "Image": "centos:latest", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20200114", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS", "org.opencontainers.image.created": "2020-01-14 00:00:00-08:00", "org.opencontainers.image.licenses": "GPL-2.0-only", "org.opencontainers.image.title": "CentOS Base Image", "org.opencontainers.image.vendor": "CentOS" } }, "NetworkSettings": { "Bridge": "", "SandboxID": "7fd278d461cea87360fb0eed672859bc2d1fccda302eafdb73540be40826e411", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/7fd278d461ce", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "8a2bb306497635e696771f56c4913fc8c5851d0bf0777aa0c35d4b6cbdb35be3", "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": "e2ee9a6eeba79ad196e03911804f7a470a503c6a4f160ed7eef3f3a26cec95f0", "EndpointID": "8a2bb306497635e696771f56c4913fc8c5851d0bf0777aa0c35d4b6cbdb35be3", "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容器删除后,在容器中产生的数据还在吗?
- Docker容器和外部机器和直接交换文件吗?
- 容器之间想要进行数据交互
数据卷
- 数据卷是宿主机中的一个目录或文件
- 当容器目录和数据卷目录绑定之后,对方的修改立即同步
- 一个数据卷可以被多个容器同时挂载
- 一个容器也可以挂载多个数据卷
数据卷作用
- 容器数据的持久化
- 外部机器和容器间的通信
- 容器之间的数据交换
创建新的容器,同时挂载宿主机/root/data目录到容器下的/root/data_container目录下,并在宿主机下touch一个test.txt文件
[root@docker yum.repos.d]# docker run -id --name=c1 -v /root/data:/root/data_container centos:latest /bin/bash
09dc1345ad747c19b189db358c4f3baf1676a61db573fc1469b0c6eb55557bb2
[root@docker yum.repos.d]# ls /root/data
[root@docker yum.repos.d]# touch /root/data/test.txt
[root@docker yum.repos.d]# ls /root/data
test.txt
进入容器里查看/root/data_container目录下是否存在test.txt文件
[root@docker yum.repos.d]# docker exec -it c1 /bin/bash
[root@09dc1345ad74 /]# ll /root/data_container/
bash: ll: command not found
[root@09dc1345ad74 /]# ls -al /root/data_container/
total 0
drwxr-xr-x. 2 root root 22 Apr 16 10:57 .
dr-xr-x---. 1 root root 28 Apr 16 10:57 ..
-rw-r--r--. 1 root root 0 Apr 16 10:57 test.txt
通过容器增加test.txt文件的内容
[root@09dc1345ad74 /]# echo 'hello container' > /root/data_container/test.txt
[root@09dc1345ad74 /]# cat /root/data_container/test.txt
hello container
[root@09dc1345ad74 /]# exit
exit
[root@docker yum.repos.d]# cat /root/data/test.txt
hello container
[root@docker yum.repos.d]#
可以看到宿主机中的test.txt文件内容
我们再做一个实验,把刚才创建的c1给stop并删除掉,重新run一个新的c1容器,然后在容器里查看test.txt文件的内容是否存在。
[root@docker yum.repos.d]# docker stop c1
c1
[root@docker yum.repos.d]# docker rm c1
c1
[root@docker yum.repos.d]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker yum.repos.d]# docker run -it --name=c1 -v /root/data:/root/data_container centos
[root@9d2887c74b33 /]# cat /root/data_container/test.txt
hello container
[root@9d2887c74b33 /]#
可以看出文件内容仍然存在,这样就可以说明数据卷的持久化是一直保持的。
也可以给一个容器同时挂载多个数据卷
[root@docker yum.repos.d]# docker run -it --name=c2 -v /root/data2:/root/data2 -v /root/data3:/root/data3 centos /bin/bash
[root@docker ~]# docker run -id --name=c3 -v /volume centos
68f6bd67ee3ceb780e15e49138efd161cfc0d04134537dbe8731d7847d65293a
2、创建启动c1,c2容器,使用–volumes-from参数设置数据卷
[root@docker ~]# docker run -id --name=c1 --volumes-from c3 centos
d2982f3ba596d5bdfc8a59aa6d2cf15ab89045b4e35c7662ae00179496b4cc70
[root@docker ~]# docker run -id --name=c2 --volumes-from c3 centos
4ce9f3321cc6ba88462eac4288f498f6359985fbf2de3a04450e0d72c2bbc472
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。