Docker 数据管理与网络通信
Posted 还行少年
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker 数据管理与网络通信相关的知识,希望对你有一定的参考价值。
文章目录
一、Docker镜像的创建方法
1.基于已有镜像创建
命令格式: docker commit 【选项】 容器ID/名称 仓库名称:标签
常用选项:
-m :说明信息
-a :作者信息
-p :生成过程中停止容器的运行
例如
[root@localhost tomcat]# docker images //查看当前镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat v6 f7a49d73a02d 22 minutes ago 964MB
[root@localhost tomcat]# docker ps //查看当前运行容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f03d993ef837 tomcat:v6 "/bin/sh -c '/usr/lo…" 22 minutes ago Up 22 minutes 0.0.0.0:49169->8080/tcp, :::49169->8080/tcp nifty_lovelace
[root@localhost tomcat]# docker commit f03d993ef837 tomcat:v7 //基于已有容器创建新的镜像
sha256:f76c0cb8b7e00935964ea125be00b01f705ca78170116c62bb3cd48d1d8b0547
[root@localhost tomcat]# docker images //14s前创建的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat v7 f76c0cb8b7e0 14 seconds ago 964MB
tomcat v6 f7a49d73a02d 24 minutes ago 964MB
[root@localhost tomcat]#
2.基于本地模板创建
[root@localhost tomcat]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f03d993ef837 tomcat:v6 "/bin/sh -c '/usr/lo…" 39 minutes ago Up 39 minutes 0.0.0.0:49169->8080/tcp, :::49169->8080/tcp nifty_lovelace
[root@localhost tomcat]# docker export nifty_lovelace > tomcat.tar.gz
[root@localhost tomcat]# cat tomcat.tar.gz | docker import - tomcat:v8
sha256:e06d12bf2b7a6757b94660853c9b939b174fea700c428c473fd26e373eeb963e
[root@localhost tomcat]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat v8 e06d12bf2b7a About a minute ago 584MB
tomcat v7 f76c0cb8b7e0 About an hour ago 964MB
tomcat v6 f7a49d73a02d About an hour ago 964MB
3.基于Dockerfile创建
Dokcerfile操作指令
指令 | 含义 |
---|---|
FROM 镜像 | 指定新镜像所基于的镜像,第一条指令必须为FROM指令 |
MAINTAINER 名字 | 说明新镜像的维护人信息 |
RUN 命令 | 在所基于的镜像上执行命令,并提交到新的镜像中 |
CMD 【“要运行的程序”,“参数1”,“参数2”】 | 指定启动容器时要运行的命令或者脚本,Dockerfile只能有一条CMD命令,如果指定多条则只有一条被执行 |
EXPOSE 端口号 | 指定新镜像加载到Docker时要开启的端口 |
ENV 环境变量 变量值 | 设置一个环境变量的值,会被后面的RUN使用 |
ADD 源文件/目录 目标文件/目录 | 将源文件复制到目标文件,源文件要与Dockerfile位于相同目录中,或者是一个URL |
COPY 源文件/目录 目标文件/目录 | 将本地主机上的源文件/目录复制到目标地点,源文件/目录要与Dockerfile在相同的目录中 |
VOLUME 【“目录”】 | 在容器中创建一个挂载点 |
USER 用户名 /UID | 指定运行容器时的用户 |
WORKDIR 路径 | 为后续的RUN、CMD、ENTRYPOINT指定工作目录 |
ONBUILD命令 | 指定所生成的镜像作为一个基础镜像时所要运行的命令 |
例如:
[root@localhost ~]# mkdir tomcat //创建工作目录
[root@localhost ~]# cd tomcat/ //切换目录
[root@localhost tomcat]# ls //上传jdk。tomcat进入此目录
apache-tomcat-9.0.16.tar.gz jdk-8u91-linux-x64.tar.gz
[root@localhost tomcat]# vim Dockerfile //编写Dokcerfile文件
FROM centos:7 //基于基础镜像
MAINTAINER this is tomcat image //用户信息
ADD jdk-8u91-linux-x64.tar.gz /usr/local/ //复制到指定目录并解压
WORKDIR /usr/local/ //指定工作目录
RUN mv jdk1.8.0_91 /usr/local/java //改名
ENV JAVA_HOME /usr/local/java //添加环境变量
ENV JRE_HOME ${JAVA_HOME}/jre
ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
ENV PATH $JAVA_HOME/bin:$PATH
ADD apache-tomcat-9.0.16.tar.gz /usr/local/ //复制文件到指定目录并解压
RUN mv apache-tomcat-9.0.16 /usr/local/tomcat //解压
WORKDIR /usr/local/tomcat/bin //指定工作目录
EXPOSE 8080 //暴露端口
ENTRYPOINT /usr/local/tomcat/bin/startup.sh && tail -f /usr/local/tomcat/logs/catalina.out //运行tomcat并查看日志(因为执行完主进程后,容器就会退出,所以运行一个主进程使其可以以守护进程的形式在后台运行)
[root@localhost tomcat]# docker build -t tomcat:v1 . //创建镜像
Sending build context to Docker daemon 192.2MB
Step 1/14 : FROM centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
---> 8652b9f0cb4c
Step 2/14 : MAINTAINER this is tomcat image
---> Running in a5532074ed3b
Removing intermediate container a5532074ed3b
---> 71779be7bb6b
Step 3/14 : ADD jdk-8u91-linux-x64.tar.gz /usr/local/
---> 825dc697ac91
Step 4/14 : WORKDIR /usr/local/
---> Running in 55fa7448be7d
Removing intermediate container 55fa7448be7d
---> e5204e622950
Step 5/14 : RUN mv jdk1.8.0_91 /usr/local/java
---> Running in f75255a5f6de
Removing intermediate container f75255a5f6de
---> 688f36307cf6
Step 6/14 : ENV JAVA_HOME /usr/local/java
---> Running in 8e7836ed0469
Removing intermediate container 8e7836ed0469
---> d781f8b9970b
Step 7/14 : ENV JRE_HOME ${JAVA_HOME}/jre
---> Running in c287ef31da84
Removing intermediate container c287ef31da84
---> 2b91344cceea
Step 8/14 : ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
---> Running in 18a913acaaed
Removing intermediate container 18a913acaaed
---> 8d963d1baade
Step 9/14 : ENV PATH $JAVA_HOME/bin:$PATH
---> Running in a0835d791cbc
Removing intermediate container a0835d791cbc
---> 15bce63ddd4e
Step 10/14 : ADD apache-tomcat-9.0.16.tar.gz /usr/local/
---> 1ddb362b6286
Step 11/14 : RUN mv apache-tomcat-9.0.16 /usr/local/tomcat
---> Running in 4b2992f274c2
Removing intermediate container 4b2992f274c2
---> a6ba9f1468f7
Step 12/14 : WORKDIR /usr/local/tomcat/bin
---> Running in 238a58b3b96f
Removing intermediate container 238a58b3b96f
---> ddda3a7e220f
Step 13/14 : EXPOSE 8080
---> Running in b158cff2fa10
Removing intermediate container b158cff2fa10
---> 332ed893be0b
Step 14/14 : ENTRYPOINT /usr/local/tomcat/bin/startup.sh && tail -f /usr/local/tomcat/logs/catalina.out
---> Running in 6cc3df381c8f
Removing intermediate container 6cc3df381c8f
---> e5abb5176221
Successfully built e5abb5176221
Successfully tagged tomcat:v1
[root@localhost tomcat]#
[root@localhost tomcat]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat v1 e5abb5176221 57 seconds ago 964MB
centos 7 8652b9f0cb4c 9 months ago 204MB
[root@localhost tomcat]# docker run -d -P --name tomcat tomcat:v1 //基于镜像创建容器
14264289e424e41923ad95e1844076c126d3f57eca4d02eb7bee72a40b2ad9cf
[root@localhost tomcat]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
14264289e424 tomcat:v1 "/bin/sh -c '/usr/lo…" 5 seconds ago Up 4 seconds 0.0.0.0:49170->8080/tcp, :::49170->8080/tcp tomcat
[root@localhost tomcat]#
优化
- 减少RUN
[root@localhost tomcat]# vim Dockerfile
FROM centos:7
ADD jdk-8u91-linux-x64.tar.gz /usr/local/
ENV JAVA_HOME /usr/local/jdk1.8.0_91
ENV JRE_HOME ${JAVA_HOME}/jre
ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
ENV PATH $JAVA_HOME/bin:$PATH
ADD apache-tomcat-9.0.16.tar.gz /usr/local/
EXPOSE 8080
ENTRYPOINT /usr/local/apache-tomcat-9.0.16/bin/startup.sh && tail -f /usr/local/apache-tomcat-9.0.16/logs/catalina.out
[root@localhost tomcat]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat v2 92d523236981 5 minutes ago 584MB
tomcat v1 e5abb5176221 41 minutes ago 964MB
centos 7 8652b9f0cb4c 9 months ago 204MB
[root@localhost tomcat]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e8961371a414 tomcat:v2 "/bin/sh -c '/usr/lo…" 5 minutes ago Up 5 minutes 0.0.0.0:49174->8080/tcp, :::49174->8080/tcp tomcatv2
14264289e424 tomcat:v1 "/bin/sh -c '/usr/lo…" 39 minutes ago Up 39 minutes 0.0.0.0:49170->8080/tcp, :::49170->8080/tcp tomcat
二、Docker的数据管理
1.数据卷
1.1 创建数据卷
[root@localhost tomcat]# docker run -d -v /data1 --name tomcat3 tomcat:v2 //创建一个数据卷挂载到容器内
86fe575915d67e8018bdabba986a55a46be18b14c431bb1270e1281b130334f5
[root@localhost tomcat]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
86fe575915d6 tomcat:v2 "/bin/sh -c '/usr/lo…" 6 seconds ago Up 6 seconds 8080/tcp tomcat3
e8961371a414 tomcat:v2 "/bin/sh -c '/usr/lo…" 30 minutes ago Up 30 minutes 0.0.0.0:49174->8080/tcp, :::49174->8080/tcp tomcatv2
14264289e424 tomcat:v1 "/bin/sh -c '/usr/lo…" About an hour ago Up About an hour 0.0.0.0:49170->8080/tcp, :::49170->8080/tcp tomcat
[root@localhost tomcat]# docker exec -it tomcat3 /bin/bash
[root@86fe575915d6 /]# ls
anaconda-post.log bin data1 dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
1.2 挂载主机目录作为数据卷
[root@localhost tomcat]# docker run -d -v /data1:/data1 --name tomcat4 tomcat:v2 //实现宿主机与容器之间的数据迁移
08487b0d03be9fecedc0162e488991e9a8699bfc33f9ce4b272341e93ce7b5b6
[root@localhost tomcat]# docker exec -it tomcat4 /bin/bash
[root@08487b0d03be /]# cd data1/
[root@08487b0d03be data1]# touch 1
[root@08487b0d03be data1]# exit
exit
[root@localhost tomcat]# ls /data1
1
[root@localhost tomcat]#
2.数据卷容器
[root@localhost tomcat]# docker run -d --volumes-from tomcat4 --name tomcat5 tomcat:v2 //挂载tomcat4中的数据卷到tomcat5
2a54095ea3eb5cc640019917ea0203d29c2e963efe9831fa3ed364cd1ceb3cac
[root@localhost tomcat]# docker exec -it tomcat5 /bin/bash
[root@2a54095ea3eb /]# cd data1/
[root@2a54095ea3eb data1]# ls
1
[root@2a54095ea3eb data1]# touch 123
[root@2a54095ea3eb data1]# exit
exit
[root@localhost tomcat]# docker exec -it tomcat4 /bin/bash
[root@08487b0d03be /]# ls data1/
1 123
[root@08487b0d03be /]# exit
exit
[root@localhost tomcat]# ls /data1
1 123
[root@localhost tomcat]#
三、Docker网络通信
1.Docker网络模式
- Bridge
使用docker0作为网桥容器,桥接容器与宿主机网络 - Host
与宿主机共享网络名称空间/网络协议栈 - Container
多个容器之间共享一个网络名称空间/网络协议栈 - None
自闭空间
2.Docker自定义网络
1.查看网络列表
[root@localhost tomcat]# docker network ls
NETWORK ID NAME DRIVER SCOPE
27f9d1130e6c bridge bridge local
cfdd984859ec host host local
81ae3de486e7 none null local
2.自定义网络固定IP
[root@localhost tomcat]# docker network create --subnet=172.18.0.0/16 mynetwork //创建新的网段
f317eceed4f9005794371808ee796de15d73cb64e26ebf00f0d2f35434fff9af
[root@localhost tomcat]# docker run -d --name tomcat --net mynetwork --ip 172.18.0.100 tomcat:v2 //固定IP
ea0b00e62fdde8cb3ffdaf94701611e0db844645651a13d0b4d744641025b6d6
[root@localhost tomcat]# docker inspect tomcat | grep "IPA" //查看IP
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAMConfig": {
"IPAddress": "172.18.0.100",
[root@localhost tomcat]#
3.端口映射
[root@localhost tomcat]# docker run -d -P tomcat:v2 //映射随机端口
71734dea1499308121abe4d975eb63145927604d5be37a4c140fcbf245f7f6fc
[root@localhost tomcat]# docker run -d -p 8080:8080 tomcat:v2 //映射指定端口
2a59bb35898ca65480463ef1bd24670a1907683ca3905681dc52862bea1fed05
[root@localhost tomcat]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a59bb35898c tomcat:v2 "/bin/sh -c '/usr/lo…" 5 seconds ago Up 4 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp recursing_northcutt
71734dea1499 tomcat:v2 "/bin/sh -c '/usr/lo…" 20 seconds ago Up 19 seconds 0.0.0.0:49176->8080/tcp, :::49176->8080/tcp quizzical_yalow
4.容器互联
[root@localhost tomcat]# docker run -d -P --name tomcat tomcat:v2 //创建源容器
d160125c8e5373face71b29cb0dff715f6e2cbb5cd1a2f4f5acf374c28d8c09a
[root@localhost tomcat]# docker run -d -P --name tomcat2 --link tomcat:tomcat tomcat:v2 //创建接收容器
3fce1b17ec497ea776995b4bed527dca378d6f9f803b024cf1b4e925b46d84ed
[root@localhost tomcat]# docker exec -it tomcat2 /bin/bash //进入容器测试
[root@3fce1b17ec49 /]# ping tomcat
PING tomcat (172.17.0.2) 56(84) bytes of data.
64 bytes from tomcat (172.17.0.2): icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from tomcat (172.17.0.2): icmp_seq=2 ttl=64 time=0.101 ms
64 bytes from tomcat (172.17.0.2): icmp_seq=3 ttl=64 time=0.107 ms
^C
--- tomcat ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.079/0.095/0.107/0.016 ms
[root@3fce1b17ec49 /]#
小结
- 创建Docker镜像有三种方法:基于已有镜像创建,基于本地模板创建,基于Dockerfile文件创建
- 容器中管理数据有数据卷和数据卷容器两种方法
- Docker可以使用映射容器端口到宿主机和容器互联机制来实现网络访问
以上是关于Docker 数据管理与网络通信的主要内容,如果未能解决你的问题,请参考以下文章