云计算部署与管理05-自定义镜像和仓库,持久化存储,docker网络架构

案例1:制作自定义镜像

案例2:创建私有镜像仓库

案例3:NFS共享存储

案例4:创建自定义网桥

案例5.客户端访问容器内的资源

1 案例1:制作自定义镜像

1.1 问题

本案例要求制作自定义镜像:
基于centos镜像使用commit创建新的镜像文件
基于centos镜像使用Dockerfile文件创建一个新的镜像文件

1.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:使用镜像启动容器
1)在该容器基础上修改yum源

[root@docker1 docker_images]# docker run -it docker.io/centos
[root@8d07ecd7e345 /]# rm -rf /etc/yum.repos.d/*
[root@8d07ecd7e345 /]# vi /etc/yum.repos.d/local.repo
[local_repo]
name=local_repo
baseurl=ftp://192.168.1.254/centos-1804
enabled=1
gpgcheck=0
[root@8d07ecd7e345 /]# yum clean all
[root@8d07ecd7e345 /]# yum repolist

2)安装测试软件

[root@8d07ecd7e345 /]# yum -y install net-tools iproute psmisc vim

3)ifconfig查看

[root@8d07ecd7e345 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:3 prefixlen 64 scopeid 0x20
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 2488 bytes 28317945 (27.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1858 bytes 130264 (127.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@8d07ecd7e345 /]# exit
exit

步骤二:另存为另外一个镜像
1)创建新建镜像

[root@docker1 ~]# docker start 8d07ecd7e345 可以简写为8d,要保证唯一性
[root@docker1 ~]# docker commit 8d07ecd7e345 docker.io/myos:latest 通过commit提交后创建镜像
sha256:ac3f9c2e8c7e13db183636821783f997890029d687b694f5ce590a473ad82c5f

2)查看新建的镜像

[root@docker1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/myos latest 87feda116c17 6 weeks ago 321.6 MB 
一旦成功之后,就变成只读的了,不能进行编辑了,除非再次基础上继续建自定义镜像
docker.io/busybox latest d8233ab899d4 7 weeks ago 1.199 MB
docker.io/nginx latest f09fe80eb0e7 9 weeks ago 109.2 MB
docker.io/redis latest 82629e941a38 11 weeks ago 94.98 MB
docker.io/ubuntu latest 20bb25d32758 11 weeks ago 87.47 MB
docker.io/registry latest 116995fd6624 11 weeks ago 25.76 MB
docker.io/centos latest 76d6bc25b8a5 4 months ago 201.8 MB
[root@docker1 ~]# docker history myos 此镜像有四层,但第三层ID号与镜像centos ID 号一致,说明myos镜像是在centos 镜像上创建的
IMAGE CREATED CREATED BY SIZE COMMENT
9f15ff91a533 48 seconds ago /bin/bash 118.1 MB
76d6bc25b8a5 12 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
12 months ago /bin/sh -c #(nop) LABEL org.label-schema.sch 0 B
12 months ago /bin/sh -c #(nop) ADD file:8f4b3be0c1427b158f 199.7 MB

3)验证新建镜像

[root@docker1 docker_images]# docker run -it docker.io/myos:latest
[root@497c7b4664bf /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.6 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:6 prefixlen 64 scopeid 0x20
ether 02:42:ac:11:00:06 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7 bytes 578 (578.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

步骤三:使用Dockerfile文件创建一个新的镜像文件

Dockerfile语法格式:
– FROM:基础镜像 以哪个镜像创建而成
– MAINTAINER:镜像创建者信息(说明)
– EXPOSE:开放的端口
– ENV:设置环境变量
– ADD:复制文件到镜像 将本地文件传到容器内文件,类似于scp
– RUN:制作镜像时执行的命令,可以有多个 在容器内执行命令,run进入容器内 RUN是在镜像中执行的命令
CMD是在容器中执行的命令 CMD中[]的命令就是上帝进程,只能写一条,要不然最后一条会覆盖前面所有的CMD
ENV是在容器中执行的命令

– WORKDIR:定义容器默认工作目录
– CMD:容器启动时执行的命令,仅可以有一条CMD
1)创建一个Apache的镜像文件
[root@docker1 ~]# mkdir aa
[root@docker1 ~]# cd aa
[root@docker1 aa]# touch Dockerfile //Dockerfile文件第一个字母要大写
[root@docker1 aa]# cp /etc/yum.repos.d/local.repo ./a.repo
[root@docker1 aa]# vim Dockerfile
FROM myos:latest  以哪个镜像为基础
RUN rm -rf /etc/yum.repos.d/*  
ADD a.repo /etc/yum.repos.d/local.repo 此处a.repo的文件必须处于aa路径下
RUN yum -y install vim net-tools iproute psmisc bash-completion
下载vim软件用于可以操作vim ,下载net-tools用于执行 ifconfig命令,下载iproute软件用于执行ip a show命令.下载pamisc执行pstree命令,下载bash-completion用于执行tab命令
[root@docker1 aa]# docker build -t docker.io/myos:test .
[root@docker1 aa]# docker run -d docker.io/myos:test
d9a5402709b26b42cd304c77be442559a5329dc784ec4f6c90e4abac1c88e206
[root@docker1 aa]# docker inspect d9
ssh remote_host touch f1---/root/f1
ssh remotw_host cd /tmp cd到tmp路径下
ssh remote_host touch f2 创建f2文件 —/root/f2
因为每次ssh都会断开=

RUN touch f1 创建f1文件---/root/f1
RUN cd /tmp cd到tmp路径下
RUN touch f2 创建f2文件 —/root/f2
因为每次RUN都会断开

RUN touch f1 创建f1文件---/root/f1
WORKDIR /tmp 固定cd到tmp路径下
RUN touch f2  创建f2文件 —/tmp/f2
WORKDIR固定切换,除非再次输入WORKDIR才会改变
[root@docker1 ~]# mkdir bb
[root@docker1 ~]# cd bb
[root@docker1 bb]# touch Dockerfile  此处的D必须大写
[root@docker1 bb]# vim Dockerfike
FROM myos:latest
RUN yum -y install httpd
EXPOSE 80 端口号80
WORKDIR /var/www/html 设置默认目录/var/www/html
ADD a.html index.html 此处的a.html必须处于bb路径下
CMD ["/usr/sbin/httpd","-DFOREGROUND"]
ENV EnvironmentFile=/etc/sysconfig/httpd
==在/usr/lib/systemd/system/httpd.service文件中找EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND这两条记录,其中ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND代表的是开启httpd服务,echo $OPTIONS为空,所以开启httpd服务的命令是/usr/sbin/httpd -DFOREGROUND,此命令是在容器中执行,所以需要在CMD中执行,即CMD ["/usr/sbin/httpd","-DFOREGROUND"].ENV设置变量为环境变量,即ENV EnvironmentFile=/etc/sysconfig/httpd
[root@docker1 bb]# docker build -t myos:httpd . 利用Dockerfile文件创建生成镜像,.必须在bb路径下
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM myos:latest
—> 53718145db0b
Step 2 : RUN yum -y install httpd
—> Running in 5e93c7d6094d
—> 65b069c7f3c6
Removing intermediate container 5e93c7d6094d
Step 3 : RUN echo “hello world” > /var/www/html/index.html
—> Running in 5a7f454791e7
—> 50ff6c53b38c
Removing intermediate container 5a7f454791e7
Step 4 : EXPOSE 80
—> Running in 320d250c64c4
—> 5e676eb1520e
Removing intermediate container 320d250c64c4
Step 5 : CMD /usr/sbin/httpd -DFOREGROUND
—> Running in 221de6809de3
—> bb44363dd7ce
Removing intermediate container 221de6809de3
Step 6 : ENV EnvironmentFile /etc/sysconfig/httpd
—> Running in 57f24dd1ea0e
—> d53dcd9d4e83
Removing intermediate container 57f24dd1ea0e
Successfully built d53dcd9d4e83
[root@docker1 ~]## docker images  查看镜像是否创建成功
REPOSITORY TAG IMAGE ID CREATED SIZE
myos httpd d53dcd9d4e83 39 seconds ago 372.5 MB
docker.io/myos test f599296ef55f 42 minutes ago 319.7 MB
myos latest 53718145db0b About an hour ago 317.9 MB
docker.io/busybox latest 19485c79a9bb 5 weeks ago 1.22 MB
docker.io/redis latest 82629e941a38 8 months ago 94.98 MB
docker.io/nginx latest 42b4762643dc 8 months ago 109.2 MB
docker.io/ubuntu latest 20bb25d32758 8 months ago 87.47 MB
docker.io/centos latest 76d6bc25b8a5 12 months ago 199.7 MB
[root@docker1 bb]# docker run -itd myos:httpd
6063f016bd27be55d71eabf2a62d364f7f112fb623999e43cf46e16514d5e558
[root@docker1 bb]# docker inspect 6063f | grep -i ipaddress 查找IP地址
“SecondaryIPAddresses”: null,
“IPAddress”: “172.17.0.2”,
“IPAddress”: “172.17.0.2”,
[root@docker1 bb]# curl 172.17.0.2  访问验证
hello world
docker1中的镜像,只能在docker1中使用,如果想在其他docker主机中用到这些镜像,就可以建网络镜像仓库.(如果docker主机少的话,可以通过将镜像压缩,tar打包,然后scp给其他docker主机上,但不适合主机数量太多)

2 案例2:创建私有镜像仓库

docker1与仓库服务器无关,仓库服务器是空的,需要将docker1中的镜像上传至仓库服务器,所以docker1与仓库服务器可以放置在同一台机器上,也可以分开装

2.1 问题

本案例要求创建私有的镜像仓库:
在resistry上创建私有仓库
在docker1上传镜像到resistry
在 docker2上配置使用resistry 的私有仓库
在 docker2上使用resistry 的远程仓库启动容器步骤
实现此案例需要按照如下步骤进行。

步骤一:自定义私有仓库
1)定义一个私有仓库

[root@resistry]# yum install docker-distribution 下载私有仓库的软件
[root@resistry]# systemctl start docker-distribution
[root@resistry]# systemctl enable docker-distribution
[root@resistry ~]# nmap 192.168.1.30  扫描nmap得到私有仓库服务的端口号是5000

Starting Nmap 6.40 ( http://nmap.org ) at 2019-10-11 15:17 CST
Nmap scan report for 192.168.1.30
Host is up (0.0000030s latency).
Not shown: 998 closed ports
PORT STATE SERVICE
22/tcp open ssh
5000/tcp open upnp

Nmap done: 1 IP address (1 host up) scanned in 0.09 seconds

2)客户端配置docker1 ==在改配置服务器前,先删开着的容器 ==

[root@docker1 ~]#docker stop $(docker ps -qa ) 关闭所有容器
[root@docker1 ~]#docker -rm $(docker ps -qa) 删除容器
[root@docker1 ~]# vim /etc/sysconfig/docker
+13 INSECURE_REGISTRY=’–insecure-registry 192.168.1.30:5000’ 允许非加密方式访问仓库
+24 ADD_REGISTRY=’–add-registry 192.168.1.30:5000’ docker仓库地址
[root@docker1~]# systemctl restart docker

3)上传镜像

[root@docker1 ~]# curl 192.168.1.30:5000/v2/_catalog   查看仓库里面有什么镜像
{“repositories”:[]}
[root@docker1 ~]# docker tag myos:httpd 192.168.1.30:5000/myos:httpd  打标签,这里面的第一个myos:httpd指的是本机的镜像,第二个myos:httpd是打标签之后的标签,第二个是自己想要的标签,两个可以不同
[root@docker1 ~]# docker push 192.168.1.30:5000/myos:httpd 上传
[root@docker1 ~]# curl 192.168.1.30:5000/v2/_catalog 查看仓库里面有什么镜像
{“repositories”:[“myos”]}
[root@docker1 ~]# curl 192.168.1.30:5000/v2/myos/tags/list 查看仓库里面的镜像标签
{“name”:“myos”,“tags”:[“httpd”]}

4)客户端启动docker2

[root@docker2 ~]#docker stop $(docker ps -qa ) 关闭所有容器
[root@docker2 ~]#docker -rm $(docker ps -qa)  删除容器
[root@docker2 ~]# vim /etc/sysconfig/docker
+13 INSECURE_REGISTRY=’–insecure-registry 192.168.1.30:5000’ 允许非加密方式访问仓库
+24 ADD_REGISTRY=’–add-registry 192.168.1.30:5000’ docker仓库地址
[root@docker2~]# systemctl restart docker
[root@docker2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker2 ~]# docker run -it 192.168.1.30:5000/myos:httpd /bin/bash 在docker2中直接从镜像仓库中直接启动镜像
Unable to find image ‘192.168.1.30:5000/myos:httpd’ locally
Trying to pull repository 192.168.1.30:5000/myos …
httpd: Pulling from 192.168.1.30:5000/myos
0ffa5ac9f3c5: Pull complete
3a2f95dd08c2: Pull complete
51512b928a14: Pull complete
10c99e1e630c: Pull complete
Digest: sha256:978a2b2e47208a739ab6b90bcb035278b7c864b2d9b87644e205e22698f170e5
AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using 172.17.0.2. Set the ‘ServerName’ directive globally to suppress this message
[root@docker2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.1.30:5000/myos httpd d53dcd9d4e83 3 hours ago 372.5 MB

3.持久化存储

3.1存储卷

[root@docker2 ~]# touch /opt/f{1…5}  在/opt路径下f1,f2,f3,f4,f5文件
[root@docker2 ~]#ls /opt
f1 f2 f3 f4 f5
[root@docker2 ~]# docker run -itd -v /opt:/abc myos:httpd  其中**-v**代表的是将本机的/opt路径映射到容器中的/abc路径下
e89a3f269b0a8b5e3dc26d5abccbd7362185018a88865d597cf16dc69a52cc53
[root@docker2 ~]#docker exec e89a3 -it /bin/bash
[root@e89a3f269b0a /]# ls  查看后出现/abc文件
abc boot etc lib media opt root sbin sys usr
bin dev home lib64 mnt proc run srv tmp var
[root@e89a3f269b0a /]# cd /abc
[root@e89a3f269b0a abc]# ls
f1 f2 f3 f4 f5
[root@e89a3f269b0a abc]# rm -rf f1 在容器中/abc路径下删除f1文件,本机中/opt路径下的f1也会删除
[root@e89a3f269b0a abc]# exit
exit
[root@docker2 ~]# ls /opt
f2 f3 f4 f5

3.2 NFS共享存储

本方案要求需要一台NFS服务器(NFS用resisry代替),ip为192.168.1.30,一台客户端docker1主机,ip为192.168.1.31,一台户端docker2主机,ip为192.168.1.32,实现客户端挂载共享,并将共享目录映射到容器中,docker1更新文件时,docker2实现同步更新,方案
在这里插入图片描述

步骤一:配置NFS服务器

[root@resisry ~]# yum -y install nfs-utils
[root@resisry ~]# mkdir /var/share
[root@resisry ~]# vim /etc/exports
/var/share 192.168.1.0/24(rw,no_root_squash)
[root@resisry ~]# systemctl restart nfs-server
[root@resisry ~]# exportfs -rv
exporting 192.168.1.0:/var/share
[root@resisry ~]# chmod 777 /var/share
[root@resisry ~]# echo “hello world” > /var/share/index.html
步骤二:配置客户端
[root@docker1~]# yum -y install nfs-utils
[root@docker1 ~]# systemctl restart nfs-server
[root@docker1 ~]# showmount -e 192.168.1.30
Export list for 192.168.1.30:
/var/share 192.168.1.0/24
[root@docker1 ~]# mount -t nfs 192.168.1.30:/var/share /mnt
[root@docker1 ~]# ls /mnt/
index.html
[root@docker1 ~]# docker run -itd -p 80:80 -v /mnt/:/var/www/html myos:httpd -p 80:80将宿主机的80端口绑定到httpd容器的80端口,使宿主机执行httpd服务,-v /mnt:/var/www/html将本机的/mnt文件映射到httpd容器中http的默认网页中
224248f0df5d795457c43c2a7dad0b7e5ec86abdc3f31d577e72f7929f020e01
[root@docker1 ~]# curl 192.168.1.31
hello world
[root@docker2 ~]# yum -y install nfs-utils
[root@docker2 ~]# showmount -e 192.168.1.30
Export list for 192.168.1.30:
/var/share 192.168.1.0/24
[root@docker2 ~]# mount -t nfs 192.168.1.30:/var/share /mnt
[root@docker2 ~]# docker run -p 80:80 -v /mnt:/usr/share/nginx/html -itd 192.168.1.30:5000 /docker.io/nginx:latest -p 80:80将宿主机的80端口绑定到nginx容器的80端口,使宿主机执行nginx服务,-v /mnt:/usr/share/nginx/html将本机的/mnt文件映射到nginx容器中nginx的默认网页中
00346dabec2c7a12958da4b7fee6551020249cdcb111ad6a1058352d2838742a
[root@docker2 ~]# curl 192.168.1.32
hello world
[root@docker1 ~]# docker exec -it 22424 /bin/bash
[root@22424 /var/www/html]# echo 123 >> index.html
[root@docker2 ~]# echo 22 >> /mnt//index.html
[root@resisry ~]# echo 11>> /var/share/index.html
[root@docker1 ~]#curl 192.168.1.31 无论是在nfs服务器上改动网页内容,还是在虚拟机上,或者在容器内改动,都可以对网页进行改动
hello world
123
22
11
[root@docker2 ~]# curl 192.168.1.32
hello world
123
22
11

4 案例4:创建自定义网桥

4.1启动4台容器

要求: 容器1 与 容器2 能够互通
   容器3 与 容器4 能够互通
   容器(12) 与 容器(34) 不能互通
在这里插入图片描述

4.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:新建Docker网络模型
1)新建docker1网络模型

[root@docker1 ~]# docker network create --subnet=10.10.10.0/24 docker1 创建新的网桥docker1,网段10.10.10.0/24
b447cacc0373631ff7c534f119047946be5c1498b5b2e31a31180c5ee6320ab5
[root@docker1 ~]# docker network list  查看网络清单
NETWORK ID NAME DRIVER SCOPE
996943486faa bridge bridge local
a2d309729af0 docker1 bridge local
63c88dcc3523 host host local
5e5ab3d45e27 none null local
[root@docker1 ~]# ifconfig
br-a2d309729af0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 但是在用ifconfig命令查看的时候,显示的名字并不是docker1,而是br-a2d309729af0
inet 10.10.10.1 netmask 255.255.255.0 broadcast 0.0.0.0
ether 02:42🇩🇪43:71:a3 txqueuelen 0 (Ethernet)
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

docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
ether 02:42:3f:ef:fb:97 txqueuelen 0 (Ethernet)
RX packets 3589 bytes 203904 (199.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4393 bytes 82635771 (78.8 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.31 netmask 255.255.255.0 broadcast 192.168.1.255
ether 52:54:00:6c:20:0b txqueuelen 1000 (Ethernet)
RX packets 42387 bytes 89935646 (85.7 MiB)
RX errors 0 dropped 13 overruns 0 frame 0
TX packets 30537 bytes 150336971 (143.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@docker1 ~]# docker network inspect docker1  查看的相信信息
[
{
“Name”: “docker1”, 
“Id”: “a2d309729af0b24dc015df4cbe9b088c940cc0c63325d81169027dd95ccf3f97”,
“Scope”: “local”,
“Driver”: “bridge”,
“EnableIPv6”: false,
“IPAM”: {
“Driver”: “default”,
“Options”: {},
“Config”: [
{
“Subnet”: “10.10.10.0/24”
}
]
},
“Internal”: false,
“Containers”: {
“96b1e8aeece7b04eb0d6511aebcf440352a4f85e995d012697507b86f0edf379”: {
“Name”: “silly_lamport”,
“EndpointID”: “85ba8b1260af3489cc422a76c677954daf1f20af0923449018cdc18ddbf121dc”,
“MacAddress”: “02:42:0a:0a:0a:02”,
“IPv4Address”: “10.10.10.2/24”,
“IPv6Address”: “”
}
},
“Options”: {},
“Labels”: {}
}
]

2)使用docker0(默认网桥)启动容器

[root@docker1 ~]# docker run -it myos:latest
[root@adc5c59573ef /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:2 prefixlen 64 scopeid 0x20
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7 bytes 578 (578.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@25a668057e60 /]# 按ctrl + p +q进入后台运行
[root@docker1 ~]# docker run -it myos:latest
[root@de9842e87e95 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:2 prefixlen 64 scopeid 0x20
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6 bytes 508 (508.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@de9842e87e95 /]# ping 172.17.0.2  相同网段能ping同,即容器1与容器2能通
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=255 time=0.134 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=255 time=0.046 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=255 time=0.044 ms
^C
— 172.17.0.2 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.044/0.074/0.134/0.043 ms
[root@de9842e87e95 /]# exit
exit
[root@docker1 ~]#

3)使用docker1(自定义网桥)启动容器

[root@docker1 ~]# docker run --network=docker1 -it myos:latest 使用docker1创建容器
[root@7ed460a1b2f3 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.10.10.2 netmask 255.255.255.0 broadcast 0.0.0.0
inet6 fe80::42:aff:fe0a:a02 prefixlen 64 scopeid 0x20
ether 02:42:0a:0a:0a:02 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6 bytes 508 (508.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@7ed460a1b2f3 /]#  按ctrl + p +q进入后台运行
[root@docker1 ~]# docker run --network=docker1 -it myos:latest
[root@4c02872edd5b /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.10.10.3 netmask 255.255.255.0 broadcast 0.0.0.0
inet6 fe80::42:aff:fe0a:a03 prefixlen 64 scopeid 0x20
ether 02:42:0a:0a:0a:03 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6 bytes 508 (508.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@4c02872edd5b /]# ping 10.10.10.2 相同网段能ping同,即容器1与容器2能通
PING 10.10.10.2 (10.10.10.2) 56(84) bytes of data.
64 bytes from 10.10.10.2: icmp_seq=1 ttl=255 time=0.152 ms
64 bytes from 10.10.10.2: icmp_seq=2 ttl=255 time=0.044 ms
64 bytes from 10.10.10.2: icmp_seq=3 ttl=255 time=0.044 ms
^C
— 10.10.10.2 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.044/0.080/0.152/0.050 ms
[root@4c02872edd5b /]# ping 172.17.0.2  不同网段不能ping同,即容器(1,2)与容器(3,4)不能通
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
^C
— 172.17.0.2 ping statistics —
4 packets transmitted, 0 received, 100% packet loss, time 2999ms
[root@4c02872edd5b /]# ping 172.17.0.1  能ping通172.17.0.1,因为真机中有10.10.10.1和172.17.0.1两个ip段,真机充作交换机
PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data.
64 bytes from 172.17.0.1: icmp_seq=1 ttl=255 time=0.051 ms
64 bytes from 172.17.0.1: icmp_seq=2 ttl=255 time=0.062 ms
^C
— 172.17.0.1 ping statistics —
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.051/0.056/0.062/0.009 ms
[root@4c02872edd5b /]# exit
exit
[root@4c02872edd5b /]#

4)把刚刚创建的docker1网桥删掉

[root@docker1 ~]# docker network rm docker1 //删除docker1

5)创建docker02以自己定义的名字

[root@docker1 ~]# docker network list
NETWORK ID NAME DRIVER SCOPE
0abe187e22c7 bridge bridge local
a2d309729af0 docker1 bridge local
83f0ee713a3f host host local
38871a206e82 none null local
[root@docker1 ~]# docker network inspect a2d309729af0 查看桥接网卡的相信信息
[
{
“Name”: “docker1”,
“Id”: “a2d309729af0b24dc015df4cbe9b088c940cc0c63325d81169027dd95ccf3f97”,
“Scope”: “local”,
“Driver”: “bridge”,
“EnableIPv6”: false,
“IPAM”: {
“Driver”: “default”,
“Options”: {},
“Config”: [
{
“Subnet”: “10.10.10.0/24”
}
]
},
“Internal”: false,
“Containers”: {
“7ed460a1b2f3426f824f7c9245ed226f42aeac5b79fc69066a93aa096274a86d”: {
“Name”: “high_sinoussi”,
“EndpointID”: “a3b53f6c9ea4c2c2822548e40af168f00f88816012ecca6851100556a1b84145”,
“MacAddress”: “02:42:0a:0a:0a:02”,
“IPv4Address”: “10.10.10.2/24”,
“IPv6Address”: “”
}
},
“Options”: {},
“Labels”: {}
}
]
[root@docker1 ~]# docker network inspect 0abe187e22c7
[
{
“Name”: “bridge”,
“Id”: “0abe187e22c7b967b47f52e5f40fef25684afc9475fc97f03100687cbf575789”,
“Scope”: “local”,
“Driver”: “bridge”,
“EnableIPv6”: false,
“IPAM”: {
“Driver”: “default”,
“Options”: null,
“Config”: [
{
“Subnet”: “172.17.0.0/16”,
“Gateway”: “172.17.0.1”
}
]
},
“Internal”: false,
“Containers”: {
“25a668057e609159888e95fd8c2def1af9640d8eaa213b057773576233441735”: {
“Name”: “gloomy_jones”,
“EndpointID”: “4676dc2b65b9948bb604d88f8fad46285b986998f97e484a6d97c4e360087731”,
“MacAddress”: “02:42:ac:11:00:02”,
“IPv4Address”: “172.17.0.2/16”,
“IPv6Address”: “”
}
},
“Options”: {
“com.docker.network.bridge.default_bridge”: “true”,
“com.docker.network.bridge.enable_icc”: “true”,
“com.docker.network.bridge.enable_ip_masquerade”: “true”,
“com.docker.network.bridge.host_binding_ipv4”: “0.0.0.0”,
“com.docker.network.bridge.name”: “docker0”,  将docker1名出现在ifconfig命令下的文件
“com.docker.network.driver.mtu”: “1500”
},
“Labels”: {}
}
]
[root@docker1 ~]# docker network create docker02 -o com.docker.network.bridge.name=docker02 创建docker02网桥,并且已dock02出现在ifconfig命令中
648bd5da03606d5a1a395c098662b5f820b9400c6878e2582a7ce754c8c05a3a
[root@docker1 ~]# ifconfig ifconfig查看有docker02
docker02: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.18.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
ether 02:42:94:27:a0:43 txqueuelen 0 (Ethernet)
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

6)若想在创建docker03的时候自定义网段(之前已经创建过docker01和02,这里用docker03),执行以下命令

[root@docker1 ~]# docker network create docker03 --subnet=172.30.0.0/16 -o com.docker.network.bridge.name=docker03
f003aa1c0fa20c81e4f73c12dcc79262f1f1d67589d7440175ea01dc0be4d03c
[root@docker1 ~]# ifconfig ifconfig查看,显示的是自己定义的网段
docker03: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.30.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
ether 02:42:27:9b:95:b3 txqueuelen 0 (Ethernet)
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

案例5.客户端访问容器内的资源

5.1默认容器可以访问外网,但外部网络的主机不可以访问容器内的资源,容器的特征是可以把宿主机变成对应的服务

1)我们可以使用-p 参数把容器端口和宿主机端口绑定 p宿主机端口:容器端口

[root@docker1 ~]# docker run -itd -p 80:80 myos:httpd 将宿主机的80端口绑定到httpd容器的80端口,使宿主机执行httpd服务
82d768cd301e05532953003be856d95427fa2ec8ce2bac7ddaf1c16aa4f64a70
[root@docker1 ~]# curl 192.168.1.31 验证访问
hello world
[root@docker1 ~]# docker exec -it 82d768 bash
[root@82d768cd301e /]# rm -rf /var/www/html/* 删除网页内容
[root@82d768cd301e /]#  按ctrl + p +q进入后台运行
[root@docker1 ~]# curl 192.168.1.31
<mark>Apache HTTP Server Test Page powered by CentOS</mark>   apache测试页面
[root@docker1 ~]# docker stop 82d768cd301e 关闭httpd服务
82d768cd301e
[root@docker1 ~]# docker run -itd -p 80:80 docker.io/nginx:latest 将宿主机的80端口绑定到nginx容器的80端口,使宿主机执行nginx服务
4a77cf42ccfe8449e8460c4eab27d915cb766489c06e1b2a84c066e2affd3922
[root@docker1 ~]# curl 192.168.1.31

==Welcome to nginx!== ==nginx测试页面==
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值