task4 docker网络
docker 基础网络
1.外部访问docker
1.1 docker container ls查看主机和容器端口映射情况
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c3cc4e993ef nginx:alpine "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp epic_dirac
1.2docker logs +容器id查看访问记录
~$ docker logs 5c3cc4e993ef
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
1.3 docker run -d -p可以指定映射端口,一个端口只可以绑定一个容器。支持的格式有:
- ip:hostPort:containerPort指定地址指定端口
- ip::containerPort指定地址的任意端口
- hostPort:containerPort映射所有接口地址
eg:
docker run -d -p 80:80 nginx:alpine
docker run -d -p 127.0.0.1:80:80 nginx:alpine
1.4docker port 容器id 80 查看端口映射的地址
$ docker port 5c3cc4e993ef 80
0.0.0.0:80
2.容器互联
1.创建一个新的docker网络:
docker network create -d bridge my-net
where,
-d指定docker网络类型(bridge overlay)
docker有4种网络模式(https://www.jianshu.com/p/22a7032bb7bd):
- host mode
- container mode
- none mode
- bridge mode(default)
$ docker network create -d bridge my-net
e977cf3db68d4c75f7bd362324961f31bf5c26c4315d9c3deedca90ff0a48e23
2.容器连接网络
docker run -it --rm --name busybox1 --network my-net busybox sh
(退出是CTRL+D)
3.从容器快照文件导入为镜像
cat ubuntu.tar| docker import - test/ubuntu:v1.0
3.配置DNS
1.查看挂载信息mount
~$ mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
2.配置全部容器的DNS,可在/etc/docker/daemon.json文件种增加。
3.展示网桥信息:
先安装 yum install -y bridge-utils
再查看brctl show
~$ brctl show
bridge name bridge id STP enabled interfaces
br-e977cf3db68d 8000.0242dbf1f764 no
docker0 8000.0242939af06d no veth0287721
4.配置docker0网桥
docker服务默认创建一个docker0网桥。
上面有一个docker0内部接口。
作用:在内核层连通所有物理、虚拟网卡,实现容器和主机在同一物理网络。