Docker build创建指定容器镜像

Docker build: Build an image from a Dockerfile.

按照Dockerfile文件所定义内容创建临时性容器,把Docker中所定义的每行命令在临时容器中执行,然后生成镜像分层,所有命令执行结束后,生成一个新的镜像,中间过程为commit。

一行命令就是一个分层,一行命令多条命令就涉及到镜像优化。

一次一次的commit执行情况就是pause暂停打包、暂停打包、暂停打包……的过程。

Docker build执行的Dockerfile文件,执行的却是docker commit操作。

Dockerfile原理:在Dockerfile定义所要执行的命令,使用docker build创建镜像,过程中会按照Dockerfile所定义的内容打开临时性容器(使用docker commit容器),把Dockerfile文件中的命令全部执行完毕,就得到一个容器应用镜像。

执行的命令越多,最终得到的容器应用镜像越大,所以要做优化。

Dockerfile关键字:

1、FROM:指定基础镜像;

2、MAINTAINER: 用来指定镜像创建者;

3、RUN:运行命令;

4、CMD:设置container启动时执行的操作;(如果容器镜像有此命令,启动容器时不要手动再让容器执行其他的命令。)

5、ENTRYPOINT:设置Container启动时执行的操作;

6、USER:设置容器中的用户;

7、EXPOSE:指定容器需要映射到宿主机的端口;

8、ENV:设置环境变量;

9、ADD:从宿主机的src复制文件到container的dest路径;

10、VOLUME:指定挂载点;

11、WORKDIR: 在子镜像中执行工作路径切换;

12、ONBUILD:

这些关键字都是要理解的。

1、示例:

目的: 通过Dockerfile创建一个可以在启动容器时就直接启动httpd应用的镜像。

步骤:

1、创建一个文件夹(目录),用于存储Dockerfile所使用的文件;

2、在此目录中创建Dockerfile文件及制作镜像所使用的文件,比如网站的首页,启动apache启动的脚本;

3、在此目录中,使用docker build创建镜像,读取Dockerfile文件;

4、验证:使用创建的镜像启动容器。

过程:

第一步:创建test目录:

mkdir test

第二步:进入目录并创建用于启动httpd的脚本文件:

cd test

vim run-httpd.sh

#!/bin/bash
rm -rf /run/httpd/*
exec /usr/sbin/httpd -D FOREGROUND

第三步:创建用于测试httpd是否可用的index.html:

vim index.html

It's work!

第四步:创建Dockerfile:

FROM centos:7
MAINTAINER "[email protected]"
RUN yum clean all && yum install -y httpd
ADD run-httpd.sh /run-httpd.sh
RUN chmod -v +x /run-httpd.sh
ADD index.html /var/www/html
EXPOSE 80
WORKDIR /
CMD ["/bin/bash","run-httpd.sh"]

说明:centos:8已经停服。

第五步:使用docker build创建容器应用镜像,镜像名称为centos-base-httpd,版本为v1。

[root@vm1 test]# docker build -t centos-base-httpd:v1 .
Sending build context to Docker daemon  4.096kB
Step 1/9 : FROM centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
 ---> eeb6ee3f44bd
Step 2/9 : MAINTAINER "[email protected]"
 ---> Running in c26b1ca650a4
Removing intermediate container c26b1ca650a4
 ---> e796b74765c8
Step 3/9 : RUN yum clean all && yum install -y httpd
 ---> Running in 13bb29923568
Loaded plugins: fastestmirror, ovl
Cleaning repos: base extras updates
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-97.el7.centos.5 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-97.el7.centos.5 for package: httpd-2.4.6-97.el7.centos.5.x86_64
--> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-97.el7.centos.5.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-97.el7.centos.5.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-97.el7.centos.5.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-97.el7.centos.5.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-7.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed
---> Package httpd-tools.x86_64 0:2.4.6-97.el7.centos.5 will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package           Arch        Version                       Repository    Size
================================================================================
Installing:
 httpd             x86_64      2.4.6-97.el7.centos.5         updates      2.7 M
Installing for dependencies:
 apr               x86_64      1.4.8-7.el7                   base         104 k
 apr-util          x86_64      1.5.2-6.el7                   base          92 k
 centos-logos      noarch      70.0.6-3.el7.centos           base          21 M
 httpd-tools       x86_64      2.4.6-97.el7.centos.5         updates       94 k
 mailcap           noarch      2.1.41-2.el7                  base          31 k

Transaction Summary
================================================================================
Install  1 Package (+5 Dependent packages)

Total download size: 24 M
Installed size: 32 M
Downloading packages:
Public key for apr-1.4.8-7.el7.x86_64.rpm is not installed
warning: /var/cache/yum/x86_64/7/base/packages/apr-1.4.8-7.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for httpd-tools-2.4.6-97.el7.centos.5.x86_64.rpm is not installed
--------------------------------------------------------------------------------
Total                                              1.7 MB/s |  24 MB  00:14
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) "
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-9.2009.0.el7.centos.x86_64 (@CentOS)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-7.el7.x86_64                                       1/6
  Installing : apr-util-1.5.2-6.el7.x86_64                                  2/6
  Installing : httpd-tools-2.4.6-97.el7.centos.5.x86_64                     3/6
  Installing : centos-logos-70.0.6-3.el7.centos.noarch                      4/6
  Installing : mailcap-2.1.41-2.el7.noarch                                  5/6
  Installing : httpd-2.4.6-97.el7.centos.5.x86_64                           6/6
  Verifying  : httpd-tools-2.4.6-97.el7.centos.5.x86_64                     1/6
  Verifying  : mailcap-2.1.41-2.el7.noarch                                  2/6
  Verifying  : apr-1.4.8-7.el7.x86_64                                       3/6
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                  4/6
  Verifying  : httpd-2.4.6-97.el7.centos.5.x86_64                           5/6
  Verifying  : centos-logos-70.0.6-3.el7.centos.noarch                      6/6

Installed:
  httpd.x86_64 0:2.4.6-97.el7.centos.5

Dependency Installed:
  apr.x86_64 0:1.4.8-7.el7
  apr-util.x86_64 0:1.5.2-6.el7
  centos-logos.noarch 0:70.0.6-3.el7.centos
  httpd-tools.x86_64 0:2.4.6-97.el7.centos.5
  mailcap.noarch 0:2.1.41-2.el7

Complete!
Removing intermediate container 13bb29923568
 ---> c1b6b195b759
Step 4/9 : ADD run-httpd.sh /run-httpd.sh
 ---> 1f85b533d455
Step 5/9 : RUN chmod -v +x /run-httpd.sh
 ---> Running in bd3ef63efe8a
mode of '/run-httpd.sh' changed from 0644 (rw-r--r--) to 0755 (rwxr-xr-x)
Removing intermediate container bd3ef63efe8a
 ---> 938f5dcddad5
Step 6/9 : ADD index.html /var/www/html
 ---> 026c49eb2266
Step 7/9 : EXPOSE 80
 ---> Running in bb9b4ff22bf7
Removing intermediate container bb9b4ff22bf7
 ---> 50c117e5c73b
Step 8/9 : WORKDIR /
 ---> Running in ef989d3ca39b
Removing intermediate container ef989d3ca39b
 ---> e12ce12352d9
Step 9/9 : CMD ["/bin/bash","run-httpd.sh"]
 ---> Running in 94f40a65dbe4
Removing intermediate container 94f40a65dbe4
 ---> 62581b44dd05
Successfully built 62581b44dd05
Successfully tagged centos-base-httpd:v1

第六步:验证,使用上述创建的镜像启动容器:

docker run -d centos-base-httpd:v1

使用docker run --help 查看-d命令的意思:

-d:Run container in background and print container ID。

另外需要注意的是后面不再需要如/bin/bash的命令了,因为/bin/bash的命令比CMD命令优先级要高。

验证容器及httpd是否可用:

docker inspect 6a6 # 查看容器的IP地址

curl http://172.17.0.11
It's working!

案例二:把nginx容器化:

1)要求通过基础镜像做nginx应用镜像;

2)使用nginx应用镜像启动容器的时候,nginx要求启动;

3)验证nginx服务是否启动。

第一步:创建一个目录:

mkdir nginxtest

cd nginxtest

第二步:创建测试文件:

[root@localhost nginxtest]# echo 'nginx s running!!!' >> index.html
[root@localhost nginxtest]# ls
index.html

第三步:创建Dockerfile文件:

FROM centos:7

MAINTAINER "[email protected]"

RUN yum clean all && yum install -y yum-plugin-ovl && yum -y install epel-release

RUN yum -y install nginx

ADD index.html /usr/share/nginx/html/

RUN echo "daemon off;" >> /etc/nginx/nginx.conf

EXPOSE 80

CMD /usr/sbin/nginx

第四步:使用docker build命令创建nginx镜像:

docker build -t centos-nginx:v1 .

你可能感兴趣的:(Linux系统,Docker容器,docker,java,容器)