一、Docker镜像的创建方法
1.基于已有镜像创建
命令格式: docker commit 【选项】 容器ID/名称 仓库名称:标签
常用选项:
-m :说明信息
-a :作者信息
-p :生成过程中停止容器的运行
例如
[root@localhost tomcat]
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat v6 f7a49d73a02d 22 minutes ago 964MB
[root@localhost tomcat]
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]
sha256:f76c0cb8b7e00935964ea125be00b01f705ca78170116c62bb3cd48d1d8b0547
[root@localhost tomcat]
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]
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]
[root@localhost tomcat]
sha256:e06d12bf2b7a6757b94660853c9b939b174fea700c428c473fd26e373eeb963e
[root@localhost tomcat]
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 ~]
[root@localhost ~]
[root@localhost tomcat]
apache-tomcat-9.0.16.tar.gz jdk-8u91-linux-x64.tar.gz
[root@localhost tomcat]
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]
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]
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat v1 e5abb5176221 57 seconds ago 964MB
centos 7 8652b9f0cb4c 9 months ago 204MB
[root@localhost tomcat]
14264289e424e41923ad95e1844076c126d3f57eca4d02eb7bee72a40b2ad9cf
[root@localhost tomcat]
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]
优化
[root@localhost tomcat]
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]
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]
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]
86fe575915d67e8018bdabba986a55a46be18b14c431bb1270e1281b130334f5
[root@localhost tomcat]
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]
[root@86fe575915d6 /]
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]
08487b0d03be9fecedc0162e488991e9a8699bfc33f9ce4b272341e93ce7b5b6
[root@localhost tomcat]
[root@08487b0d03be /]
[root@08487b0d03be data1]
[root@08487b0d03be data1]
exit
[root@localhost tomcat]
1
[root@localhost tomcat]
2.数据卷容器
[root@localhost tomcat]
2a54095ea3eb5cc640019917ea0203d29c2e963efe9831fa3ed364cd1ceb3cac
[root@localhost tomcat]
[root@2a54095ea3eb /]
[root@2a54095ea3eb data1]
1
[root@2a54095ea3eb data1]
[root@2a54095ea3eb data1]
exit
[root@localhost tomcat]
[root@08487b0d03be /]
1 123
[root@08487b0d03be /]
exit
[root@localhost tomcat]
1 123
[root@localhost tomcat]
三、Docker网络通信
1.Docker网络模式
- Bridge
使用docker0作为网桥容器,桥接容器与宿主机网络 - Host
与宿主机共享网络名称空间/网络协议栈 - Container
多个容器之间共享一个网络名称空间/网络协议栈 - None
自闭空间
2.Docker自定义网络
1.查看网络列表
[root@localhost tomcat]
NETWORK ID NAME DRIVER SCOPE
27f9d1130e6c bridge bridge local
cfdd984859ec host host local
81ae3de486e7 none null local
2.自定义网络固定IP
[root@localhost tomcat]
f317eceed4f9005794371808ee796de15d73cb64e26ebf00f0d2f35434fff9af
[root@localhost tomcat]
ea0b00e62fdde8cb3ffdaf94701611e0db844645651a13d0b4d744641025b6d6
[root@localhost tomcat]
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAMConfig": {
"IPAddress": "172.18.0.100",
[root@localhost tomcat]
3.端口映射
[root@localhost tomcat]
71734dea1499308121abe4d975eb63145927604d5be37a4c140fcbf245f7f6fc
[root@localhost tomcat]
2a59bb35898ca65480463ef1bd24670a1907683ca3905681dc52862bea1fed05
[root@localhost tomcat]
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]
d160125c8e5373face71b29cb0dff715f6e2cbb5cd1a2f4f5acf374c28d8c09a
[root@localhost tomcat]
3fce1b17ec497ea776995b4bed527dca378d6f9f803b024cf1b4e925b46d84ed
[root@localhost tomcat]
[root@3fce1b17ec49 /]
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可以使用映射容器端口到宿主机和容器互联机制来实现网络访问
|