Docker容器互联
查看所有网络信息
docker network ls
创建网络
docker network create my-net
-d 指定网络类型,默认使用的是bridge,也可以是overlay(需要swarm mode)
查看网络详情信息
docker inspect my-net
连接容器
[root@dokcer _data]# docker run -dit --name=app1 --network my-net centos:7
78c6bd50d9111a00b7865071a4be43fb9f16523c501f5120fd842e0b88d1a712
[root@dokcer _data]# docker run -dit --name=app2 --network my-net centos:7
bff16dc1c32a99f27dec4aa8867bfdb729d07785cf38b56147964fa6781df1af
[root@dokcer _data]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bff16dc1c32a centos:7 "/bin/bash" 8 seconds ago Up 6 seconds app2
78c6bd50d911 centos:7
通过查看app1的ip地址为172.18.0.2 app2的ip地址为172.18.0.3
[root@dokcer _data]# ping 172.18.0.2
PING 172.18.0.2 (172.18.0.2) 56(84) bytes of data.
64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.078 ms
64 bytes from 172.18.0.2: icmp_seq=2 ttl=64 time=0.059 ms
64 bytes from 172.18.0.2: icmp_seq=3 ttl=64 time=0.062 ms
64 bytes from 172.18.0.2: icmp_seq=4 ttl=64 time=0.055 ms
64 bytes from 172.18.0.2: icmp_seq=5 ttl=64 time=0.047 ms
^C
--- 172.18.0.2 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4125ms
rtt min/avg/max/mdev = 0.047/0.060/0.078/0.011 ms
[root@dokcer _data]# ping 172.18.0.3
PING 172.18.0.3 (172.18.0.3) 56(84) bytes of data.
64 bytes from 172.18.0.3: icmp_seq=1 ttl=64 time=0.050 ms
64 bytes from 172.18.0.3: icmp_seq=2 ttl=64 time=0.049 ms
^C
--- 172.18.0.3 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1010ms
rtt min/avg/max/mdev = 0.049/0.049/0.050/0.007 ms
[root@78c6bd50d911 /]# ping 172.18.0.1
PING 172.18.0.1 (172.18.0.1) 56(84) bytes of data.
64 bytes from 172.18.0.1: icmp_seq=1 ttl=64 time=0.119 ms
64 bytes from 172.18.0.1: icmp_seq=2 ttl=64 time=0.057 ms
^C
--- 172.18.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1038ms
rtt min/avg/max/mdev = 0.057/0.088/0.119/0.031 ms
[root@78c6bd50d911 /]# ping 172.18.0.3
PING 172.18.0.3 (172.18.0.3) 56(84) bytes of data.
64 bytes from 172.18.0.3: icmp_seq=1 ttl=64 time=0.375 ms
64 bytes from 172.18.0.3: icmp_seq=2 ttl=64 time=0.034 ms
64 bytes from 172.18.0.3: icmp_seq=3 ttl=64 time=0.091 ms
^C
--- 172.18.0.3 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2066ms
rtt min/avg/max/mdev = 0.034/0.166/0.375/0.149 ms
[root@78c6bd50d911 /]# ping app2
PING app2 (172.18.0.3) 56(84) bytes of data.
64 bytes from app2.my-net (172.18.0.3): icmp_seq=1 ttl=64 time=0.093 ms
64 bytes from app2.my-net (172.18.0.3): icmp_seq=2 ttl=64 time=0.035 ms
64 bytes from app2.my-net (172.18.0.3): icmp_seq=3 ttl=64 time=0.033 ms
^C
--- app2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2074ms
rtt min/avg/max/mdev = 0.033/0.053/0.093/0.029 ms
[root@78c6bd50d911 /]# exit
exit
[root@dokcer _data]# docker exec -it app2 bash
[root@bff16dc1c32a /]# ping app1
PING app1 (172.18.0.2) 56(84) bytes of data.
64 bytes from app1.my-net (172.18.0.2): icmp_seq=1 ttl=64 time=0.149 ms
64 bytes from app1.my-net (172.18.0.2): icmp_seq=2 ttl=64 time=0.035 ms
^C
--- app1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1016ms
rtt min/avg/max/mdev = 0.035/0.092/0.149/0.057 ms
[root@bff16dc1c32a /]# exit
exit
删除网络
docker network rm my-net
只能删除未被占用的网络
Dockerfile
镜像制作的脚本
以centos:7为基础,安装JDK和Tomcat
准备一个JDK9和Tomcat 8版本
JDK下载:https://repo.huaweicloud.com/openjdk/9/
Tomcat下载:https://mirrors.cnnic.cn/apache/tomcat/tomcat-8/v8.5.91/bin/
解压上面下载的压缩包的命令
tar -zxvf apache-tomcat-8.5.91.tar.gz
tar -zxvf openjdk-9_linux-x64_bin.tar.gz
Dockerfile内容
#以centos:7作为基础,安装JDK和Tomcat
FROM centos:7
#ADD命令将压缩包传入镜像中的指定的目录,并同时解压
ADD openjdk-9_linux-x64_bin.tar.gz /opt/
ADD apache-tomcat-8.5.91.tar.gz /usr/
#修改文件名
RUN mv /usr/apache-tomcat-8.5.91 /usr/tomcat
#设置环境变量
ENV JAVA_HOME=/opt/jdk-9 \
CATALINA_HOME=/usr/tomcat \
PATH=$PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
# 暴露端口
EXPOSE 8080
#设置tomcat启动容器时,自启
ENTRYPOINT /usr/tomcat/bin/startup.sh && tail -F /usr/tomcat/logs/catalina.out
FROM centos:7
ADD openjdk-9_linux-x64_bin.tar.gz /opt/
ADD apache-tomcat-8.5.91.tar.gz /usr/
RUN mv /usr/apache-tomcat-8.5.91 /usr/tomcat
ENV JAVA_HOME=/opt/jdk-9 \
CATALINA_HOME=/usr/tomcat \
PATH=$PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
EXPOSE 8080
ENTRYPOINT /usr/tomcat/bin/startup.sh && tail -F /usr/tomcat/logs/catalina.out
使用Dockerfile构建镜像
Dockerfile文件和openjdk-9_linux-x64_bin.tar.gz 以及apache-tomcat-8.5.91.tar.gz在同一目录
docker build -t tomcat:8 .
末尾的"."表示构建过程从当前目录寻找文件
运行tomcat:8
docker run -dit --name=tomcat8 tomcat:8 bash
查看运行过程中的日志输出
docker logs tomcat8
远程访问tomcat8
访问失败
网络链接没有问题、防火墙已经关闭
我们需要做端口映射
端口映射
docker run -dit --name=tomcat88 -p 8080:8080 tomcat:8 bash
-p 宿主机端口:容器端口
上一篇文章:Docker容器操作-CSDN博客https://blog.csdn.net/Z0412_J0103/article/details/143225306下一篇文章: