Centos7 docker基础6-Dockerfile

Dockerfile是一个文本格式的配置文件,用户可以使用Dockerfile快速创建自定义的镜像

Dockerfile 由一行行命令语句组成,并且支持以#开头的注释

Dockerfile 一般分为四部分:

  • 基础镜像信息
  • 维护者信息
  • 镜像操指令
  • 容器启动执行指令

简单例子:
新建一个“simple_Dockerfile”目录,下面放Dockerfile文件与一个temp.tar.gz文件

[root@centos7 simple_Dockerfile]# ll
总用量 12
-rw-r--r-- 1 root root 1918 7月   1 18:30 11更换域名公告.html   #这个temp.tar.gz的内容
-rw-r--r-- 1 root root  244 7月  22 08:55 Dockerfile
-rw-r--r-- 1 root root 1132 7月  22 08:55 temp.tar.gz

Dockerfile内容如下

FROM ubuntu
MAINTAINER vector4wang xxxx@qq.com

WORKDIR /usr/local/docker
ADD temp.tar.gz ./add/
COPY temp.tar.gz ./copy/
EXPOSE 22
RUN groupadd -r vector4wang && useradd -r -g vector4wang vector4wang
#USER 切换用户
USER vector4wang

ENTRYPOINT ["/bin/bash"]

开始构建: docker build -t docker/simple .

#-t docker/simple 设置tag

#. 最后一点表示当前目录
运行结果:

[root@centos7 simple_Dockerfile]# docker build -t docker/simple .
Sending build context to Docker daemon  7.68 kB
Step 1/9 : FROM ubuntu
 ---> 4c108a37151f
Step 2/9 : MAINTAINER vector4wang xxxx@qq.com
 ---> Using cache
 ---> 23b5384b6a7c
Step 3/9 : WORKDIR /usr/local/docker
 ---> Using cache
 ---> e5bf240e4584
Step 4/9 : ADD temp.tar.gz ./add/
 ---> 27a3a2c66608
Removing intermediate container e02486f557ab
Step 5/9 : COPY temp.tar.gz ./copy/
 ---> 09d9e65e06c1
Removing intermediate container 7642555ce325
Step 6/9 : EXPOSE 22
 ---> Running in f02fb012c2c6
 ---> 53aa5253d4f6
Removing intermediate container f02fb012c2c6
Step 7/9 : RUN groupadd -r vector4wang && useradd -r -g vector4wang vector4wang
 ---> Running in 0f80f819c4b0

 ---> 58b2f283140a
Removing intermediate container 0f80f819c4b0
Step 8/9 : USER vector4wang
 ---> Running in 9d740c1880ef
 ---> 79fa54b4a6ce
Removing intermediate container 9d740c1880ef
Step 9/9 : ENTRYPOINT /bin/bash
 ---> Running in 7bf38f7a0bdf
 ---> b4869dbea5b2
Removing intermediate container 7bf38f7a0bdf
Successfully built b4869dbea5b2

查看生成的镜像

[root@centos7 simple_Dockerfile]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
docker/simple                 latest              b4869dbea5b2        7 seconds ago       64.6 MB #这个是生成
<none>                        <none>              ab9934bcdc0f        7 minutes ago       64.6 MB #感觉这个也是生成的
centos1                       v1.0                f1420ae8ab97        2 days ago          202 MB
ubuntu                        14.04               40446eeefc1a        2 days ago          215 MB

进容器中查看相应的操作是否成功

[root@centos7 simple_Dockerfile]# docker run -it b4869dbea5b2
vector4wang@3acb26c35c57:/usr/local/docker$ pwd   
 #当前操作确实为/usr/local/docker  
/usr/local/docker
vector4wang@3acb26c35c57:/usr/local/docker$ ls
#自动生成了不存在的路径
add  copy
vector4wang@3acb26c35c57:/usr/local/docker$ cd add/
vector4wang@3acb26c35c57:/usr/local/docker/add$ ls
#ADD 自动帮你解压的.tar.gz文件
'11'$'\346\233\264\346\215\242\345\237\237\345\220\215\345\205\254\345\221\212''.html'
vector4wang@3acb26c35c57:/usr/local/docker/add$ cd ../copy/
vector4wang@3acb26c35c57:/usr/local/docker/copy$ ls
#COPY 只是复制而己
temp.tar.gz
vector4wang@3acb26c35c57:/usr/local/docker/copy$ exit
exit

二、体验下使用docker build Dockerfile以阿里中间件大赛给的debian-jdk8镜像

Dockerfile文件如下:

FROM debian:stretch

ARG DEBIAN_FRONTEND=noninteractive
ARG JAVA_VERSION=8
ARG JAVA_UPDATE=172
ARG JAVA_BUILD=11
ARG JAVA_PACKAGE=jdk
ARG JAVA_HASH=a58eab1ec242421181065cdc37240b08

ENV LANG C.UTF-8
ENV JAVA_HOME=/opt/jdk
ENV PATH=${PATH}:${JAVA_HOME}/bin

RUN set -ex \
 && apt-get update \
 && apt-get -y install ca-certificates wget unzip \
 && wget -q --header "Cookie: oraclelicense=accept-securebackup-cookie" \
         -O /tmp/java.tar.gz \
         http://download.oracle.com/otn-pub/java/jdk/${JAVA_VERSION}u${JAVA_UPDATE}-b${JAVA_BUILD}/${JAVA_HASH}/${JAVA_PACKAGE}-${JAVA_VERSION}u${JAVA_UPDATE}-linux-x64.tar.gz \
 && CHECKSUM=$(wget -q -O - https://www.oracle.com/webfolder/s/digest/${JAVA_VERSION}u${JAVA_UPDATE}checksum.html | grep -E "${JAVA_PACKAGE}-${JAVA_VERSION}u${JAVA_UPDATE}-linux-x64\.tar\.gz" | grep -Eo '(sha256: )[^<]+' | cut -d: -f2 | xargs) \
 && echo "${CHECKSUM}  /tmp/java.tar.gz" > /tmp/java.tar.gz.sha256 \
 && sha256sum -c /tmp/java.tar.gz.sha256 \
 && mkdir ${JAVA_HOME} \
 && tar -xzf /tmp/java.tar.gz -C ${JAVA_HOME} --strip-components=1 \
 && wget -q --header "Cookie: oraclelicense=accept-securebackup-cookie;" \
         -O /tmp/jce_policy.zip \
         http://download.oracle.com/otn-pub/java/jce/${JAVA_VERSION}/jce_policy-${JAVA_VERSION}.zip \
 && unzip -jo -d ${JAVA_HOME}/jre/lib/security /tmp/jce_policy.zip \
 && rm -rf ${JAVA_HOME}/jar/lib/security/README.txt \
       /var/lib/apt/lists/* \
       /tmp/* \
       /root/.wget-hsts

新建一个空目录,假设就是~/debian-jdk8吧,cd进这个目录,新建一个Dockerfile,然后把上面的内容copy进去,然后执行下面的命令:

docker build -t debian-jdk8:v1.0 .

其中-t debian-jdk8:v1.0表示打包的镜像名为debian-jdk,tag为v1.0(前面说过,tag是可以任意命名的,不一定要是这种格式),注意命令的最后有一个.,这个表示打包的上下文(其实就是Dockerfile所在目录)是在当前目录,然后目录下的Dockerfile就会被编译执行。

[root@centos7 denbian-jdk8]# docker build -t debian-jdk8:v1.0 .
Sending build context to Docker daemon 3.072 kB
Step 1/11 : FROM debian:stretch
Trying to pull repository docker.io/library/debian ... 
stretch: Pulling from docker.io/library/debian
a4d8138d0f6b: Pull complete 
Digest: sha256:397b2157a9ea8d7f16c613aded70284292106e8b813fb1ed5de8a8785310a26a
Status: Downloaded newer image for docker.io/debian:stretch
 ---> 5a6d49d5e833
Step 2/11 : ARG DEBIAN_FRONTEND=noninteractive
 ---> Running in 3b695e63be44
 ---> cf75b9120562
Removing intermediate container 3b695e63be44
Step 3/11 : ARG JAVA_VERSION=8
 ---> Running in ffb25ed30a7b
 ---> cb294b73f54a
Removing intermediate container ffb25ed30a7b
Step 4/11 : ARG JAVA_UPDATE=172
 ---> Running in b02c932f307e
 ---> 3754d68d4615
Removing intermediate container b02c932f307e
Step 5/11 : ARG JAVA_BUILD=11
 ---> Running in d334c9c11b6d
 ---> 938457a64582
Removing intermediate container d334c9c11b6d
Step 6/11 : ARG JAVA_PACKAGE=jdk
 ---> Running in 0af0b1849907
 ---> 9636e9d4b039
Removing intermediate container 0af0b1849907
Step 7/11 : ARG JAVA_HASH=a58eab1ec242421181065cdc37240b08
 ---> Running in f5f26fc293e7
 ---> 2e8dbf64318a
Removing intermediate container f5f26fc293e7
Step 8/11 : ENV LANG C.UTF-8
 ---> Running in 32b46fc56ecc
 ---> ae275b3b5e84
Removing intermediate container 32b46fc56ecc
Step 9/11 : ENV JAVA_HOME /opt/jdk
 ---> Running in 79a42efd0802
 ---> 212b7ebcf149
Removing intermediate container 79a42efd0802
Step 10/11 : ENV PATH ${PATH}:${JAVA_HOME}/bin
 ---> Running in 6eaab787feac
 ---> c19adf77374c
Removing intermediate container 6eaab787feac
Step 11/11 : RUN set -ex  && apt-get update  && apt-get -y install ca-certificates wget unzip  && wget -q --header "Cookie: oraclelicense=accept-securebackup-cookie"          -O /tmp/java.tar.gz          http://download.oracle.com/otn-pub/java/jdk/${JAVA_VERSION}u${JAVA_UPDATE}-b${JAVA_BUILD}/${JAVA_HASH}/${JAVA_PACKAGE}-${JAVA_VERSION}u${JAVA_UPDATE}-linux-x64.tar.gz  && CHECKSUM=$(wget -q -O - https://www.oracle.com/webfolder/s/digest/${JAVA_VERSION}u${JAVA_UPDATE}checksum.html | grep -E "${JAVA_PACKAGE}-${JAVA_VERSION}u${JAVA_UPDATE}-linux-x64\.tar\.gz" | grep -Eo '(sha256: )[^<]+' | cut -d: -f2 | xargs)  && echo "${CHECKSUM}  /tmp/java.tar.gz" > /tmp/java.tar.gz.sha256  && sha256sum -c /tmp/java.tar.gz.sha256  && mkdir ${JAVA_HOME}  && tar -xzf /tmp/java.tar.gz -C ${JAVA_HOME} --strip-components=1  && wget -q --header "Cookie: oraclelicense=accept-securebackup-cookie;"          -O /tmp/jce_policy.zip          http://download.oracle.com/otn-pub/java/jce/${JAVA_VERSION}/jce_policy-${JAVA_VERSION}.zip  && unzip -jo -d ${JAVA_HOME}/jre/lib/security /tmp/jce_policy.zip  && rm -rf ${JAVA_HOME}/jar/lib/security/README.txt        /var/lib/apt/lists/*        /tmp/*        /root/.wget-hsts
 ---> Running in 87cc3bb1f8d5

+ apt-get update
Get:1 http://security-cdn.debian.org/debian-security stretch/updates InRelease [94.3 kB]
Ign:2 http://cdn-fastly.deb.debian.org/debian stretch InRelease
Get:3 http://cdn-fastly.deb.debian.org/debian stretch-updates InRelease [91.0 kB]
Get:5 http://security-cdn.debian.org/debian-security stretch/updates/main amd64 Packages [499 kB]
Get:4 http://cdn-fastly.deb.debian.org/debian stretch Release [118 kB]
Get:6 http://cdn-fastly.deb.debian.org/debian stretch-updates/main amd64 Packages [27.4 kB]
Get:7 http://cdn-fastly.deb.debian.org/debian stretch Release.gpg [2434 B]
Get:8 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 Packages [7082 kB]
Fetched 7915 kB in 4min 4s (32.4 kB/s)
Reading package lists...
+ apt-get -y install ca-certificates wget unzip
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  libffi6 libgmp10 libgnutls30 libhogweed4 libidn2-0 libp11-kit0 libpsl5
  libssl1.1 libtasn1-6 libunistring0 openssl publicsuffix
Suggested packages:
  gnutls-bin zip
The following NEW packages will be installed:
  ca-certificates libffi6 libgmp10 libgnutls30 libhogweed4 libidn2-0
  libp11-kit0 libpsl5 libssl1.1 libtasn1-6 libunistring0 openssl publicsuffix
  unzip wget
0 upgraded, 15 newly installed, 0 to remove and 1 not upgraded.
Need to get 5212 kB of archives.
After this operation, 14.4 MB of additional disk space will be used.
Get:1 http://security-cdn.debian.org/debian-security stretch/updates/main amd64 libssl1.1 amd64 1.1.0k-1~deb9u1 [1356 kB]
Get:3 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libgmp10 amd64 2:6.1.2+dfsg-1 [253 kB]
Get:4 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libhogweed4 amd64 3.3-1+b2 [136 kB]
Get:5 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libffi6 amd64 3.2.1-6 [20.4 kB]
Get:6 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libp11-kit0 amd64 0.23.3-2 [111 kB]
Get:7 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libtasn1-6 amd64 4.10-1.1+deb9u1 [50.6 kB]
Get:8 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libgnutls30 amd64 3.5.8-5+deb9u4 [896 kB]
Get:9 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libunistring0 amd64 0.9.6+really0.9.3-0.1 [279 kB]
Get:10 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libidn2-0 amd64 0.16-1+deb9u1 [60.7 kB]
Get:11 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libpsl5 amd64 0.17.0-3 [41.8 kB]
Get:12 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 wget amd64 1.18-5+deb9u3 [800 kB]
Get:13 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 ca-certificates all 20161130+nmu1+deb9u1 [182 kB]
Get:2 http://security-cdn.debian.org/debian-security stretch/updates/main amd64 openssl amd64 1.1.0k-1~deb9u1 [747 kB]
Get:14 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 unzip amd64 6.0-21+deb9u1 [170 kB]
Get:15 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 publicsuffix all 20190415.1030-0+deb9u1 [108 kB]
The command '/bin/sh -c set -ex  && apt-get update  && apt-get -y install ca-certificates wget unzip  && wget -q --header "Cookie: oraclelicense=accept-securebackup-cookie"          -O /tmp/java.tar.gz          http://download.oracle.com/otn-pub/java/jdk/${JAVA_VERSION}u${JAVA_UPDATE}-b${JAVA_BUILD}/${JAVA_HASH}/${JAVA_PACKAGE}-${JAVA_VERSION}u${JAVA_UPDATE}-linux-x64.tar.gz  && CHECKSUM=$(wget -q -O - https://www.oracle.com/webfolder/s/digest/${JAVA_VERSION}u${JAVA_UPDATE}checksum.html | grep -E "${JAVA_PACKAGE}-${JAVA_VERSION}u${JAVA_UPDATE}-linux-x64\.tar\.gz" | grep -Eo '(sha256: )[^<]+' | cut -d: -f2 | xargs)  && echo "${CHECKSUM}  /tmp/java.tar.gz" > /tmp/java.tar.gz.sha256  && sha256sum -c /tmp/java.tar.gz.sha256  && mkdir ${JAVA_HOME}  && tar -xzf /tmp/java.tar.gz -C ${JAVA_HOME} --strip-components=1  && wget -q --header "Cookie: oraclelicense=accept-securebackup-cookie;"          -O /tmp/jce_policy.zip          http://download.oracle.com/otn-pub/java/jce/${JAVA_VERSION}/jce_policy-${JAVA_VERSION}.zip  && unzip -jo -d ${JAVA_HOME}/jre/lib/security /tmp/jce_policy.zip  && rm -rf ${JAVA_HOME}/jar/lib/security/README.txt        /var/lib/apt/lists/*        /tmp/*        /root/.wget-hsts' returned a non-zero code: 137
[root@centos7 denbian-jdk8]#

执行完毕后运行docker images就会发现多了一个debian-jdk8镜像。

下面来解释一下Dockerfile的结构,那些字母全部大写的每行第一个单词都是Dockerfile的指令,可以看出这个Dockefile中包括的指令有FROM、ARG、ENV、RUN,下面的表格中我对其含义进行了解释:

指令 含义解释
WORKDIR 在构建时,指定镜像工作目录
EXPOSE 暴露镜像的端口供主机做映像
COPY 复制文件到容器 不做解压操作
ADD 复制文件到容器 且做解压操作   
ADD会对压缩文件(tar, gzip, bzip2, etc)做提取和解压操作。
USER 指定该镜像以什么样的用户去执行
CMD 容器启动时需要执行的命令

当有多个CMD的时候,只有最后一个生效。 会给docker run覆盖

ENTRYPOINT :容器启动时需要执行的命令 不会给docker run覆盖

FROM FROM debian:stretch表示以debian:stretch作为基础镜像进行构建

RUN 可以看出RUN后面跟的其实就是一些shell命令,通过&&将这些脚本连接在了一行执行,这么做的原因是为了减少镜像的层数,每多一行RUN都会给镜像增加一层,所以这里选择将所有命令联结在一起执行以减少层数

ARG 特地将这个指令放在RUN之后讲解,这个指令可以进行一些宏定义,比如我定义ENV JAVA_HOME=/opt/jdk,之后RUN后面的shell命令中的${JAVA_HOME}都会被/opt/jdk代替 ENV 可以看出这个指令的作用是在shell中设置一些环境变量(其实就是export)

参考文献:https://blog.csdn.net/qq_33256688/article/details/80319673
教程参考于:http://www.bdkyr.com

# docker
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×