目录
引言
一、Docker的网络模式
1、Docker的四种网络模式
2、Docker的自定义网络
二、Docker的数据卷
1、数据卷
2、数据卷容器
3、容器互联(使用centos镜像)
总结
引言
介绍关于Docker的四种网络模式以及实现容器之间的共享。
一、Docker的网络模式
1、Docker的四种网络模式
(1)Host:与宿主机共享网络名称空间/网络协议栈,IP共享和端口范围的共享;
(2)Container:多个容器之间共享一个network namespaces,多个容器共用一个IP和端口范围;
(3)None:自闭空间,无网卡和无需网络连接;
(4)Bridge:桥接是默认模式,在不指定网络模式的情况下创建容器,默认使用此模式,通过Veth对,连接容器与docker0网桥,网桥分配给容器IP,同事docker0作为“局域网”内容器的网关,最后和宿主机网卡进行通讯,同时,通过iptables规则,将容器IP/port映射出去,用于与宿主机网卡交互。
2、Docker的自定义网络
(1)查看网络列表
root@docker ~]# docker network ls
?(2)查看容器IP
docker inspect 镜像id
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 9 days ago 141MB
nginx lnmp ea335eea17ab 9 days ago 141MB
centos 7 eeb6ee3f44bd 2 months ago 204MB
[root@docker ~]# docker inspect ea335eea17ab
[
{
"Id": "sha256:ea335eea17ab984571cd4a3bcf90a0413773b559c75ef4cda07d0ce952b00291",
"RepoTags": [
"nginx:latest",
"nginx:lnmp"
],
"RepoDigests": [
"nginx@sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e"
],
"Parent": "",
"Comment": "",
"Created": "2021-11-17T10:38:14.652464384Z",
"Container": "8a038ff17987cf87d4b7d7e2c80cb83bd2474d66e2dd0719e2b4f7de2ad6d853",
"ContainerConfig": {
"Hostname": "8a038ff17987",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.21.4",
"NJS_VERSION=0.7.0",
"PKG_RELEASE=1~bullseye"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"nginx\" \"-g\" \"daemon off;\"]"
],
"Image": "sha256:2fb4060b053a39040c51ff7eadd30325de2c76650fc50aa42839070e16e8bdcb",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"DockerVersion": "20.10.7",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.21.4",
"NJS_VERSION=0.7.0",
"PKG_RELEASE=1~bullseye"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "sha256:2fb4060b053a39040c51ff7eadd30325de2c76650fc50aa42839070e16e8bdcb",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"Architecture": "amd64",
"Os": "linux",
"Size": 141490847,
"VirtualSize": 141490847,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/745f3000efcdb513b3dbac2b839941056038b7f6759ec9a2f1cd8a1aed465b8e/diff:/var/lib/docker/overlay2/9022910
ea297653435a20b75e9ca05e17337f3585aac3bf3ed3d12d60f6ff886/diff:/var/lib/docker/overlay2/cbc359dabe146feb99d59e076e525399610ec55df1dc17259776cd6c1b4c772e/diff:/var/lib/docker/overlay2/35706655061ed79100c98027d8e317c1d5befcff36bed2bdf1893e2687a314ac/diff:/var/lib/docker/overlay2/c98f1d427d324d8cd7f92b5262ca2bad03557a1db27bf8a52ac4a9a83924210e/diff", "MergedDir": "/var/lib/docker/overlay2/41129afe9dbf4c934fbf1a8855d2c6a11ea012848c0c50693442e0822e1b8a7f/merged",
"UpperDir": "/var/lib/docker/overlay2/41129afe9dbf4c934fbf1a8855d2c6a11ea012848c0c50693442e0822e1b8a7f/diff",
"WorkDir": "/var/lib/docker/overlay2/41129afe9dbf4c934fbf1a8855d2c6a11ea012848c0c50693442e0822e1b8a7f/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:e1bbcf243d0e7387fbfe5116a485426f90d3ddeb0b1738dca4e3502b6743b325",
"sha256:37380c5830feb5d6829188be41a4ea0654eb5c4632f03ef093ecc182acf40e8a",
"sha256:ff4c727794302b5a0ee4dadfaac8d1233950ce9a07d76eb3b498efa70b7517e4",
"sha256:49eeddd2150fbd14433ec1f01dbf6b23ea6cf581a50635554826ad93ce040b68",
"sha256:1e8ad06c81b6baf629988756d90fd27c14285da4d9bf57179570febddc492087",
"sha256:8525cde30b227bb5b03deb41bda41deb85d740b834be61a69ead59d840f07c13"
]
},
"Metadata": {
"LastTagTime": "2021-11-25T11:11:55.855864227+08:00"
}
}
]
(3)自定义网络固定IP
先自定义网络模式,创建172.18.0.0/16的网段
[root@docker ~]# docker network create --subnet=172.18.0.0/16 mynetwork
网络模式内加入指定的ip地址
[root@docker ~]# docker run -itd --name test1 --net mynetwork --ip 172.18.0.10 centos:latest /bin/bash
[root@docker ~]# docker inspect test1
(4)端口映射
docker run -d -P httpd:centos7
docker run -d -p 49280:80 httpd:centos7
二、Docker的数据卷
1、数据卷
将宿主机目录/var/www挂载到容器中的/data1上
docker run -v /var/www:/data1 --name test -it centos:7 /bin/bash
cd /data1/
touch test1111
#返回宿主机进行查看
ls /var/www/
2、数据卷容器
#数据卷容器
docker run --name juan -v /data1 -v /data2 -it centos:7 /bin/bash
#新容器挂载数据卷容器
docker run -it --volumes-from juan --name test2 centos:7 /bin/bash
3、容器互联(使用centos镜像)
#创建并运行容器取名web1,端口号自动映射
docker run -itd -P --name web1 centos:7 /bin/bash
#创建并运行容器取名web2,链接到web1和其通信进web2容器 ping web1
docker run -itd -P --name web2 --link web1:web1 centos:7 /bin/bash
总结
1、Docker的网络模式:Host模式,Container模式,None模式,Bridge模式;
2、数据卷容器:让两个容器实现数据共享。
|