失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Dockerfile镜像构建

Dockerfile镜像构建

时间:2020-11-15 10:59:08

相关推荐

Dockerfile镜像构建

Dockerfile

文章目录

DockerfileDocker镜像构建Docker commmit基于本地模板导入Dockerfile基本概念文件格式镜像缓存dockerfile 语法实例 (一)实例 (二)一、扫雷案例二、可道云案例三、使用dockerfile,alpine作为基础镜像,部署nginx。1、apk 安装nginx2、源代码编译安装nginx

在本章中,主要学习如何构建Docker镜像,其实在Docker公共仓库中已经有大量的镜像,但是并不一定满足我们对镜像的需求,所以我们需要学习如何的构建自己所需要的镜像。

Docker镜像构建

Docker镜像可以通过Docker hub或者阿里云等仓库中获取,这些镜像是由官方或者社区人员提供的,对于Docker用户来说并不能满足我们的需求,但是从无开始构建镜像成本大。常用的数据库、中间件、应用软件等都有现成的Docker官方镜像或社区创建的镜像,我们只需要稍作配置就可以直接使用。

使用现成镜像的好处除了省去自己做镜像的工作量外,更重要的是可以利用前人的经验。特别是使用那些官方镜像,因为Docker的工程师知道如何更好的在容器中运行软件。

当然,某些情况下我们也不得不自己构建镜像,比如找不到现成的镜像,比如自己开发的应用程序,需要在镜像中加入特定的功能。

Docker提供了三种构建镜像的方法:

docker commit命令基于本地模板导入Dockerfile构建文件

Docker commmit

docker commit命令可以基于容器创建镜像,创建过程大致分为三步,先创建容器,在容器中安装我们所需要的内容,再使用docker commit将容器打包为镜像即可。

下面展示一个示例:在centos的base镜像中安装vim-common并保存为新镜像。

1、先基于centos7运行容器,容器名为axi,并使用-it生成终端进入容器。[root@localhost ~]# docker run -it --name axi centos:72、在容器中安装vim-common。[root@194e67bda157 /]# dnf -y install vim-commonLoaded plugins: fastestmirror, ovlDetermining fastest mirrors* base: mirrors.* extras: mirrors.* updates: mirrors.base | 3.6 kB 00:00:00extras| 2.9 kB 00:00:00updates | 2.9 kB 00:00:00(1/4): base/7/x86_64/group_gz | 153 kB 00:00:00(2/4): extras/7/x86_64/primary_db | 247 kB 00:00:00(3/4): base/7/x86_64/primary_db| 6.1 MB 00:00:13(4/4): updates/7/x86_64/primary_db | 16 MB 00:00:22Resolving Dependencies--> Running transaction check---> Package vim-common.x86_64 2:7.4.629-8.el7_9 will be installed--> Processing Dependency: vim-filesystem for package: 2:vim-common-7.4.629-8.el7_9.x86_64--> Running transaction check---> Package vim-filesystem.x86_64 2:7.4.629-8.el7_9 will be installed--> Finished Dependency ResolutionDependencies Resolved========================================================================================================PackageArchVersion Repository Size========================================================================================================Installing:vim-common x86_64 2:7.4.629-8.el7_9 updates 5.9 MInstalling for dependencies:vim-filesystem x86_64 2:7.4.629-8.el7_9 updates 11 kTransaction Summary========================================================================================================Install 1 Package (+1 Dependent package)Total download size: 5.9 MInstalled size: 21 MDownloading packages:warning: /var/cache/yum/x86_64/7/updates/packages/vim-filesystem-7.4.629-8.el7_9.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEYPublic key for vim-filesystem-7.4.629-8.el7_9.x86_64.rpm is not installed(1/2): vim-filesystem-7.4.629-8.el7_9.x86_64.rpm | 11 kB 00:00:00(2/2): vim-common-7.4.629-8.el7_9.x86_64.rpm | 5.9 MB 00:00:27--------------------------------------------------------------------------------------------------------Total 221 kB/s | 5.9 MB 00:00:27Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7Importing GPG key 0xF4A80EB5:Userid: "CentOS-7 Key (CentOS 7 Official Signing Key) <security@>"Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5Package : centos-release-7-9..0.el7.centos.x86_64 (@CentOS)From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7Running transaction checkRunning transaction testTransaction test succeededRunning transactionInstalling : 2:vim-filesystem-7.4.629-8.el7_9.x86_641/2 Installing : 2:vim-common-7.4.629-8.el7_9.x86_642/2 Verifying : 2:vim-common-7.4.629-8.el7_9.x86_641/2 Verifying : 2:vim-filesystem-7.4.629-8.el7_9.x86_642/2 Installed:vim-common.x86_64 2:7.4.629-8.el7_9 Dependency Installed:vim-filesystem.x86_64 2:7.4.629-8.el7_9 Complete![root@194e67bda157 /]# 3、退出容器后,使用docker commit将axi容器打包为镜像,新镜像名为centosac:7[root@docker ~]# docker commit axi centosac:7sha256:282e38de865e6063499df3829e1a6b3854a178b03976472ef3e2b8ffd40a0008[root@docker ~]# docker images | grep centoscentosac7 282e38de865e 24 seconds ago 391MBcentos axi 5279aaca23bc 25 minutes ago 204MBcentos 7 6ac490b1c4a0 9 months ago204MB[root@docker ~]# docker run --name ac -it centosac:7 [root@30f05c39a0c7 /]# rpm -q vim-commonvim-common-7.4.629-8.el7_9.x86_64[root@30f05c39a0c7 /]#

这样一个新的镜像就构建完成了,centosac:7镜像是在centos:7镜像基础之上创建的,通过查看镜像属性,发现centoscy:7要比centos:7镜像大一些。

然而,Docker并不建议用户通过这种方式构建镜像。这是一种手工创建镜像的方式,容易出错,效率低且可重复性弱。更重要的,使用者并不知道镜像是如何创建出来的,里面是否有恶意程序。也就是说无法对镜像进行审计,存在安全隐患.

基于本地模板导入

用户可以直接从一个操作系统模板文件导入一个镜像,主要使用 docker [container] import 命令。命令 格式为 docker [image] import [OPTIONS] file|URL|-[REPOSITORY[:TAG]] ,要直接导入一个镜像,可以使用 OpenVZ 提供的模板来创建,或者用其他已导入的镜像模板来创建。OpenVZ 模板的下载地址为 /Download/templates/precreated。

如:下载了 ubuntu:12.04 的模板压缩包,之后使用以下命令导入即可:

[root@docker ~]# cat ubuntu-12.04-x86-minimal.tar.gz | docker import - ubuntu:12.04sha256:88216a4bfde304bde7e1195756880acab7921b368da20e3a8591e71afc81b11e[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEubuntu 12.0488216a4bfde3 5 seconds ago 146MB

Dockerfile

基本概念

Docker镜像是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的一些配置参数(如匿名卷、环境变量、用户等)。

镜像的定制实际上就是定制每一层所添加的配置、文件。如果我们可以把每一层修改、安装、构建、操作的命令都写入一个脚本,用这个脚本来构建、定制镜像,那么之前提及的无法重复的问题、镜像构建透明性的问题、体积的问题就都会解决。这个脚本就是Dockerfile。

Dockerfile是一个文本文件,其内包含了一条条的指令(Instruction),每一条指令构建一层,因此每一条指令的内容,就是描述该层应当如何构建。有了Dockerfile,当我们需要定制自己额外的需求时,只需在 Dockerfile上添加或者修改指令,重新生成镜像即可,省去了敲命令的麻烦。

文件格式

Dockerfile分为四部分:基础镜像信息、维护者信息、镜像操作指令、容器启动执行指令。一开始必须要指明所基于的镜像名称,接下来一般会说明维护者信息;后面则是镜像操作指令,例如ADD指令。每执行一条ADD指令,镜像添加新的一层,并提交;最后是CMD指令,来指明运行容器时的操作命令。

示例如下,我们构建一个httpd镜像。

[root@localhost ~]# lsDockerfile [root@loaclhost ~]# vim Dockerfile # 1、第一行必须指定,基础镜像信息FROM centos:7# 2、维护者信息MAINTAINER chenyu@# 3、镜像操作指令RUN yum install -y httpdEXPOSE 80# 4、容器启动执行指令CMD ["/bin/bash"]

把构建容器所需要的指令都存放在Dockerfile文件中,这个文件的名字是固定的,不能够更改,再使用docker build命令构建容器,使用-t定义新的镜像名,如果构建镜像的Dockerfile文件不在当前目录下可以使用-f指定Dockerfile文件路径,示例如下:

[root@localhost ~]# docker build -t httpd:aaa /root/Sending build context to Docker daemon 152MBStep 1/5 : FROM centos:7---> eeb6ee3f44bdStep 2/5 : MAINTAINER acong@---> Running in d41c5db5b3ecRemoving intermediate container d41c5db5b3ec---> a9c67ae8f88eStep 3/5 : RUN yum -y install httpd---> Running in e4777ad252f6Loaded plugins: fastestmirror, ovlDetermining fastest mirrors* base: mirrors.* extras: mirrors.* updates: mirrors.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 ResolutionDependencies Resolved================================================================================Package Arch Version Repository Size================================================================================Installing:httpd x86_642.4.6-97.el7.centos.5 updates2.7 MInstalling for dependencies:aprx86_641.4.8-7.el7 base 104 kapr-utilx86_641.5.2-6.el7 base92 kcentos-logosnoarch70.0.6-3.el7.centos base21 Mhttpd-tools x86_642.4.6-97.el7.centos.5 updates 94 kmailcap noarch2.1.41-2.el7 base31 kTransaction Summary================================================================================Install 1 Package (+5 Dependent packages)Total download size: 24 MInstalled size: 32 MDownloading packages:warning: /var/cache/yum/x86_64/7/base/packages/apr-util-1.5.2-6.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEYPublic key for apr-util-1.5.2-6.el7.x86_64.rpm is not installedPublic key for httpd-tools-2.4.6-97.el7.centos.5.x86_64.rpm is not installed--------------------------------------------------------------------------------Total237 kB/s | 24 MB 01:44Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7Importing GPG key 0xF4A80EB5:Userid: "CentOS-7 Key (CentOS 7 Official Signing Key) <security@>"Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5Package : centos-release-7-9..0.el7.centos.x86_64 (@CentOS)From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7Running transaction checkRunning transaction testTransaction test succeededRunning transactionInstalling : 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.5mailcap.noarch 0:2.1.41-2.el7 Complete!Removing intermediate container e4777ad252f6---> e7e8c35f9b15Step 4/5 : EXPOSE 80---> Running in 56b747392c13Removing intermediate container 56b747392c13---> 65c05b81e846Step 5/5 : CMD ["/bin/bash"]---> Running in 6f6b7c08c6abRemoving intermediate container 6f6b7c08c6ab---> b44f420b6834Successfully built b44f420b6834Successfully tagged httpd:aaa

通过以上镜像的构建过程可以看出,Dockerfile文件内的指令会逐一运行,构建过程如下:

1、下载centos7镜像。

2、添加镜像构建者信息

3、基于centos7镜像启动容器,安装httpd软件,安装完毕后将容器打包为镜像。

4、基于上一步生成的镜像启动容器,将80端口打开,打开后将容器打包为镜像。

5、基于上一步生成的镜像启动容器,添加容器启动后需要执行的指令,再打包为镜像。

也就说一条指令就是一层镜像,还可以通过docker history查看镜像的构建过程,这样我们构建的镜像就呈现出透明化,整个构建的过程都可以看到。

[root@localhost ~]# docker history httpd:aaa IMAGECREATEDCREATED BY SIZECOMMENTb44f420b6834 10 minutes ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B 65c05b81e846 10 minutes ago /bin/sh -c #(nop) EXPOSE 800B e7e8c35f9b15 10 minutes ago /bin/sh -c yum -y install httpd 203MBa9c67ae8f88e 13 minutes ago /bin/sh -c #(nop) MAINTAINER acong@example… 0B eeb6ee3f44bd 9 months ago/bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing>9 months ago/bin/sh -c #(nop) LABEL org.label-schema.sc… 0B<missing>9 months ago/bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB

镜像缓存

使用DockerFile文件构建完镜像以后,Docker会把构建过程中的每一层临时镜像进行缓存。在构建新镜像时,可以直接使用之前缓存的镜像层,这样能加速镜像的构建。镜像缓存示例如下:

[root@localhost ~]# vim Dockerfile # 1、第一行必须指定,基础镜像信息FROM centos:7# 2、维护者信息MAINTAINER acong@# 3、镜像操作指令RUN mkdir /axi

修改之前的Dockerfile文件,然后我们再构建新的镜像,构建过程如下,通过构建过程可以得知,DockerFile文件里面共三条指令,前两条指令都是用之前构建镜像的缓存,只有第三个指令才重新构建了缓存层。如果希望在构建镜像时不使用缓存,可以在docker build命令中加上–no-cache参数。

[root@docker ~]# docker build -t accentos:7 .Sending build context to Docker daemon 152MBStep 1/4 : FROM centos:7---> eeb6ee3f44bdStep 2/4 : MAINTAINER acong@---> Using cache---> a9c67ae8f88eStep 3/4 : RUN mkdir /galaxy---> Running in 0f8daaa6860aRemoving intermediate container 0f8daaa6860a---> f9dec0e749cdStep 4/4 : CMD ["/bin/bash"]---> Running in 15d1e564b622Removing intermediate container 15d1e564b622---> 446ed6d636eeSuccessfully built 446ed6d636eeSuccessfully tagged cycentos:7

dockerfile 语法

Dockerfile是由一系列指令和参数构成的脚本,一个Dockerfile里面包含了构建整个镜像的完整指令。Docker通过docker build执行Dockerfile中的一系列指令自动构建镜像,常用的Dockerfile指令有以下几种。

1、FROM指令必须为Dockerfile文件开篇的第一个非注释行,用于指定构建镜像所使用的基础镜像,后续的指令运行都要依靠此基础镜像所提供的的环境。实际使用中,如果没有指定仓库,docker build会先从本机查找是否有此基础镜像,如果没有会默认去Docker Hub Registry上拉取,再找不到就会报错,格式如下。

FROM <Repository>[:<Tag>]FROM <Repository>@<Digest>#Digest:镜像的哈希码,防止镜像被冒名顶替。

2、MAINTAINER指令用于让Dockerfile的作者提供个人的信息,Dockerfile并不限制MAINTAINER指令的位置,但是建议放在FROM指令之后,在较新的Docker版本中,已经被LABEL替代,格式如下。

MAINTAINER "acong@"

3、LABEL指令用于让用户为镜像指定各种元数据(键值对的格式),格式如下。

LABEL <key>=<value> <key>=<value>

4、COPY指令用于复制宿主机上的文件到目标镜像中,格式如下。

COPY <src>... <dest>COPY ["<src>",... "<dest>"]

:要复制的源文件或者目录,支持通配符

:目标路径,即正创建的镜像的文件系统路径,建议使用绝对路径,否则,COPY指令会以WORKDIR为其起始路径。如果路径中如果包含空白字符,建议使用第二种格式用引号引起来,否则会被当成两个文件。

5、ADD指令跟COPY类似,不过它还支持使用tar文件和URL路径。当拷贝的源文件是tar文件时,会自动展开为一个目录并拷贝进新的镜像中;然而通过URL获取到的tar文件不会自动展开。主机可以再联网的情况下,docker build可以将网络上的某文件引用下载并打包到新的镜像中,格式如下。

ADD <src>... <dest>ADD ["<src>",... "<dest>"]

6、WORKDIR指令用于指定工作目录,可以指多个,每个WORKDIR只影响他下面的指令,直到遇见下一个WORKDIR为止。WORKDIR也可以调用由ENV指令定义的变量。,格式如下。

WORKDIR 相对路径或者绝对路径

7、VOLUME指令用于在镜像中创建一个挂载点目录。Volume有两种类型:绑定挂载卷和docker管理的卷。在Dockerfile中只支持Docker管理的卷,也就是说只能指定容器内的路径,不能指定宿主机的路径,格式如下。

VOLUME <mountpoint>VOLUME ["<mountpoint>"]

8、EXPOSE指令用于指定容器中待暴露的端口。比如容器提供的是一个https服务且需要对外提供访问,那就需要指定待暴露443端口,然后在使用此镜像启动容器时搭配-P的参数才能将待暴露的状态转换为真正暴露的状态,转换的同时443也会转换成一个随机端口,跟-p :443一个意思。EXPOSE指令可以一次指定多个端口,例如:EXPOSE 11111/udp 11112/tcp,格式如下。

EXPOSE <port>[/<protocol>] [<port>[/<protocol>] ...]

9、ENV指令用于为镜像定义所需的环境变量,并可被ENV指令后面的其它指令所调用。调用格式为 $variable_name或者 ${variable_name},使用docker run启动容器的时候加上-e的参数为variable_name赋值,可以覆盖Dockerfile中ENV指令指定的此variable_name的值。但是不会影响到Dockerfile中已经引用过此变量的文件名,格式如下。

ENV <key> <value>ENV <key>=<value> ...

10、RUN指令运行于docker build过程中运行的程序,可以是任何命令。RUN指令后所执行的命令必须在FROM指令后的基础镜像中存在才行,格式如下。

RUN <command> RUN ["executable", "param1", "param2"]

通常是一个shell命令,系统默认会把后面的命令作为shell的子进程来运行,以"/bin/sh -c"来运行它。

第二种格式的参数是一个JSON格式的数组,其中"executable"为要运行的命令,后面的"paramN"为传递给命令的选项或参数。

11、CMD指令用于用户指定启动容器的默认要运行的程序,也就是PID为1的进程命令,且其运行结束后容器也会终止。如果不指定,默认是bash。CMD指令指定的默认程序会被docker run命令行指定的参数所覆盖。Dockerfile中可以存在多个CMD指令,但仅最后一个生效。因为一个Docker容器只能运行一个PID为1的进程。类似于RUN指令,也可以运行任意命令或程序,但是两者的运行时间点不同。RUN指令运行在docker build的过程中,而CMD指令运行在基于新镜像启动容器时,格式如下。

CMD command param1 param2CMD ["executable","param1","param2"]CMD ["param1","param2"]

前两种语法格式同RUN指令。第一种用法对于CMD指令基本没有意义,因为它运行的程序PID不为1。

第三种则需要结合ENTRYPOINT指令使用,CMD指令后面的命令作为ENTRYPOINT指令的默认参数。如果docker run命令行结尾有参数指定,那CMD后面的参数不生效。

12、ENTRYPOINT指令类似CMD指令的功能,用于为容器指定默认运行程序。Dockerfile中可以存在多个ENTRYPOINT指令,但仅最后一个生效,与CMD区别在于,由ENTRYPOINT启动的程序不会被docker run命令行指定的参数所覆盖,而且这些命令行参数会被当做参数传递给ENTRYPOINT指令指定的程序,格式如下。

ENTRYPOINT command param1 param2ENTRYPOINT ["executable", "param1", "param2"]

不过,docker run的–entrypoint选项的参数可覆盖ENTRYPOINT指定的默认程序。

13、USER用于指定docker build过程中任何RUN、CMD等指令的用户名或者UID。默认情况下容器的运行用户为root,格式如下。

USER <user>[:<group>]USER <UID>[:<GID>]

实例 (一)

1、基于基础镜像centos:7

2、维护者信息

3、添加Centos-7仓库

4、添加epel-7仓库

5、安装nginx软件包

6、暴露指定端口80

7、构建镜像,镜像名称为nginx:v1

8、运行容器,容器名称为自己名字的全拼,映射端口88:80

9、进入容器,删除默认网页,写入新的默认网页,网页内容为你们自己名字的全拼

10、通过浏览器,能够正常访问nginx

[root@localhost ~]# vim DockerfileFROM centos:7MAINTAINER AXI@ADD Centos-7.repo /etc/yum/repos.d/ADD epel-7.repo /etc/yum/repos.d/RUN yum -y install nginxEXPOSE 80CMD ["/usr/sbin/nginx","-g","daemon off;"][root@localhost ~]# docker build -t nginx:v1 .Sending build context to Docker daemon 24.36MBStep 1/7 : FROM centos:7---> eeb6ee3f44bdStep 2/7 : MAINTAINER AXI@---> Using cache---> ddb7b29ee73dStep 3/7 : ADD Centos-7.repo /etc/yum/repos.d/---> Using cache---> 293034cdfc1aStep 4/7 : ADD epel-7.repo /etc/yum/repos.d/---> Using cache---> 06db9b47d55bStep 5/7 : RUN yum -y install nginx---> Using cache---> 1927e4a7aa2aStep 6/7 : EXPOSE 80---> Running in 1dd97c7f9677Removing intermediate container 1dd97c7f9677---> a3c064bfc3ebStep 7/7 : CMD ["/usr/sbin/nginx","-g","daemon off;"]---> Running in e305eadc35dbRemoving intermediate container e305eadc35db---> c26bd2c76044Successfully built c26bd2c76044Successfully tagged nginx:v1[root@localhost ~]#[root@localhost ~]# docker run -dit --name liucong -p 88:80 nginx:v1ae7ed1e26682685b9e828d5e06e5f5be0aac5d1869178e56eca08798ce964ef2[root@localhost ~]# docker psCONTAINER ID IMAGECOMMAND CREATEDSTATUSPORTS NAMESae7ed1e26682 nginx:v1 "/usr/sbin/nginx -g …" 18 seconds ago Up 17 seconds 0.0.0.0:88->80/tcp, :::88->80/tcp liucong[root@localhost ~]# docker exec -it liucong /bin/bash[root@ae7ed1e26682 /]# cd /usr/share/nginx/html/[root@ae7ed1e26682 html]# ls404.html 50x.html en-US icons img index.html nginx-logo.png poweredby.png[root@ae7ed1e26682 html]# rm -rf index.html[root@ae7ed1e26682 html]# echo "liucong" > index.html[root@ae7ed1e26682 html]# cat index.htmlliucong[root@ae7ed1e26682 html]#

实例 (二)

一、扫雷案例

1、所需软件包和环境

Centos-7.repo epel-7.repo saolei.zip

server1:部署docker扫雷案例

server2:部署apache,将saolei.zip软件包上传上去

2、编辑Dockerfile,使用基础镜像centos:7

3、构建镜像saolei:v1

4、运行容器,指定映射端口8081:8080,容器名为自己名字的全拼

5、使用浏览器访问http://自己docker服务器的ip/saolei.jsp,能够实现扫雷游戏的使用

server1 配置

[root@localhost ~]# cd /etc/yum.repos.d/[root@localhost yum.repos.d]# lsCentos-7.repo epel-7.repo[root@localhost ~]# yum -y install httpd[root@localhost ~]# cd /var/www/html/上传扫雷软件包[root@localhost html]# lssaolei.zip[root@localhost html]# systemctl start httpd[root@localhost html]# systemctl enable httpd[root@localhost html]# systemctl stop firewalld[root@localhost html]# setenforce 0

server2 配置

[root@localhost ~]# vim DockerfileFROM centos:7MAINTAINER axi@ADD Centos-7.repo /etc/yum.repos.d/ADD epel-7.repo /etc/yum.repos.d/RUN yum -y install tomcat unzip curlWORKDIR /var/lib/tomcat/webapps/RUN curl -O http://192.168.89.150/saolei.zip && \unzip saolei.zip && \mv saolei ROOTADD init.sh /init.shEXPOSE 8080CMD ["/bin/bash","/init.sh"][root@localhost ~]# vim init.sh#!/bin/bash/usr/libexec/tomcat/server start[root@localhost ~]#[root@localhost ~]# docker build -t saolei:v1 .Sending build context to Docker daemon 25.11MBStep 1/10 : FROM centos:7---> eeb6ee3f44bdStep 2/10 : MAINTAINER axi@---> Using cache---> cf7f3ea66247Step 3/10 : ADD Centos-7.repo /etc/yum.repos.d/---> Using cache---> 39dd92ab6818Step 4/10 : ADD epel-7.repo /etc/yum.repos.d/---> Using cache---> 929a18df2c47Step 5/10 : RUN yum -y install tomcat unzip curl---> Using cache---> d291d3d1f080Step 6/10 : WORKDIR /var/lib/tomcat/webapps/---> Using cache---> 643d2645808dStep 7/10 : RUN curl -O http://192.168.89.150/saolei.zip && unzip saolei.zip && mv saolei ROOT---> Running in c5df6d257011% Total % Received % Xferd Average Speed Time TimeTime CurrentDload Upload Total Spent Left Speed100 241k 100 241k 00 139M0 --:--:-- --:--:-- --:--:-- 235MArchive: saolei.zipcreating: saolei/creating: saolei/imgs/inflating: saolei/imgs/bai22.jpginflating: saolei/imgs/caidao.jpginflating: saolei/imgs/hei22.jpginflating: saolei/imgs/lei.jpginflating: saolei/imgs/notlei.jpginflating: saolei/imgs/paichu.jpginflating: saolei/imgs/paicuo.jpginflating: saolei/imgs/qipan.jpginflating: saolei/imgs/query.jpginflating: saolei/saolei.jspRemoving intermediate container c5df6d257011---> b677b123118bStep 8/10 : ADD init.sh /init.sh---> 7b5b3d5a0127Step 9/10 : EXPOSE 8080---> Running in 0a2fc9aa7e39Removing intermediate container 0a2fc9aa7e39---> 2d5b0409f873Step 10/10 : CMD ["/bin/bash","/init.sh"]---> Running in 28090a0b26c0Removing intermediate container 28090a0b26c0---> c6fc43bd8e9fSuccessfully built c6fc43bd8e9fSuccessfully tagged saolei:v1[root@localhost ~]# docker run -d --name liucong -p 8081:8080 saolei:v1496b9fa165ae8dcd4c2ce7a17344494c13a5a496e750e4ee0b017f4491c5cb27[root@localhost ~]# docker psCONTAINER ID IMAGE COMMANDCREATEDSTATUSPORTS NAMES496b9fa165ae saolei:v1 "/bin/bash /init.sh" 10 minutes ago Up 10 minutes 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp liucong

浏览器访问

二、可道云案例

1、所需软件包

Centos-7.repo epel-7.repo kodexplorer4.40.zip nginx.conf

2、创建/opt/dockerfile/kod目录,将所需的软件包和文件放置该目录下

3、在该目录下编辑dockerfile,使用基础镜像centos:7

4、构建镜像kod:v1

5、运行容器,指定映射端口80:80,容器名为自己名字的全拼

6、使用浏览器访问http://自己docker服务器的ip/index.php,能够实现可道云的使用

[root@localhost ~]# cd /etc/yum.repos.d/[root@localhost yum.repos.d]# lsCentos-7.repo epel-7.repo[root@localhost ~]# mkdir -p /opt/dockerfile/kod[root@localhost ~]# cd /opt/dockerfile/kod/[root@localhost kod]# lsCentos-7.repo Dockerfile epel-7.repo init.sh kodexplorer4.40.zip nginx.conf[root@localhost kod]# vim Dockerfile[root@localhost kod]# lsDockerfile kodexplorer4.40.zip nginx.conf[root@localhost kod]# cat DockerfileFROM centos:7ADD Centos-7.repo /etc/yum.repos.d/ADD epel-7.repo /etc/yum.repos.d/RUN yum -y install nginx php-fpm php-gd php-mbstring unzip && \sed -i '/^user/c user=nginx' /etc/php-fpm.d/www.conf && \sed -i '/^group/c group=nginx' /etc/php-fpm.d/www.confCOPY nginx.conf /etc/nginx/nginx.confRUN mkdir /codeWORKDIR /codeCOPY kodexplorer4.40.zip .RUN unzip kodexplorer4.40.zip && \chown -R nginx.nginx .ADD init.sh /init.shEXPOSE 80ENTRYPOINT ["/bin/bash","/init.sh"][root@localhost kod]# vim nginx.confworker_processes 1;events {worker_connections 1024;}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server {listen 80;server_name localhost;location / {root /code;index index.php index.html index.htm;}location ~ \.php$ {root /code;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /code$fastcgi_script_name;include fastcgi_params;}}}[root@localhost kod]#[root@localhost kod]# vim init.sh#!/bin/bashphp-fpm -Decho "$1" >> /etc/nginx/nginx.confnginx -g 'daemon off;'[root@localhost kod]#[root@localhost kod]# docker build -t kod:v1 .Sending build context to Docker daemon 13.9MBStep 1/12 : FROM centos:7---> eeb6ee3f44bdStep 2/12 : ADD Centos-7.repo /etc/yum.repos.d/---> Using cache---> 86f481a0839aStep 3/12 : ADD epel-7.repo /etc/yum.repos.d/---> Using cache---> ce7ba1fb7e2dStep 4/12 : RUN yum -y install nginx php-fpm php-gd php-mbstring unzip && sed -i '/^user/c user=nginx' /etc/php-fpm.d/www.conf && sed -i '/^group/c group=nginx' /etc/php-fpm.d/www.conf---> Using cache---> 1e7789d97c56Step 5/12 : COPY nginx.conf /etc/nginx/nginx.conf---> f1b9fbb718b6Step 6/12 : RUN mkdir /code---> Running in 002e39a82d72Removing intermediate container 002e39a82d72---> 3a4c569d0cebStep 7/12 : WORKDIR /code---> Running in 9182cff332a1Removing intermediate container 9182cff332a1---> c262a38f61e2Step 8/12 : COPY kodexplorer4.40.zip .---> 654860eabf13Step 9/12 : RUN unzip kodexplorer4.40.zip && chown -R nginx.nginx .此处省略...[root@localhost kod]# docker run -d --name liucong -p 80:80 kod:v1 '#testtest'007307fbee5277dafea5d621e8f5550430fbbd99c58dae2e5277845130968a25[root@localhost kod]# docker psCONTAINER ID IMAGECOMMAND CREATEDSTATUSPORTS NAMES007307fbee52 kod:v1 "/bin/bash /init.sh …" 47 seconds ago Up 46 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp liucong

浏览器访问

三、使用dockerfile,alpine作为基础镜像,部署nginx。

1、apk 安装nginx

[root@localhost ~]# docker pull alpineUsing default tag: latestlatest: Pulling from library/alpine59bf1c3509f3: Pull completeDigest: sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300Status: Downloaded newer image for alpine:latestdocker.io/library/alpine:latest[root@localhost ~]# vim DockerfileFROM alpineRUN echo "/alpine/v3.11/main/" > /etc/apk/repositories; \echo "/alpine/v3.11/community/" >> /etc/apk/repositories; \apk add nginx && mkdir /run/nginx/EXPOSE 80ENTRYPOINT [ "nginx", "-g", "daemon off;" ][root@localhost ~]# docker build -t nginx:v1 .Sending build context to Docker daemon 25.12MBStep 1/4 : FROM alpine---> c059bfaa849cStep 2/4 : RUN echo "/alpine/v3.11/main/" > /etc/apk/repositories; echo "/alpine/v3.11/community/" >> /etc/apk/repositories; apk add nginx && mkdir /run/nginx/---> Running in 7090f8aa64e2fetch /alpine/v3.11/main/x86_64/APKINDEX.tar.gzfetch /alpine/v3.11/community/x86_64/APKINDEX.tar.gz(1/2) Installing pcre (8.43-r1)(2/2) Installing nginx (1.16.1-r8)Executing nginx-1.16.1-r8.pre-installExecuting busybox-1.34.1-r3.triggerOK: 7 MiB in 16 packagesRemoving intermediate container 7090f8aa64e2---> 6255abc125abStep 3/4 : EXPOSE 80---> Running in fb4658eec742Removing intermediate container fb4658eec742---> a3d517cb14e1Step 4/4 : ENTRYPOINT [ "nginx", "-g", "daemon off;" ]---> Running in 0427427940e6Removing intermediate container 0427427940e6---> bd51526d6e4bSuccessfully built bd51526d6e4bSuccessfully tagged nginx:v1[root@localhost ~]#[root@localhost ~]# docker run -d -P nginx:v1c6cbe131f85101074d0b83c1f19868da4345d06195ec0ebb89ac6c86c49c9d7b[root@localhost ~]# docker psCONTAINER ID IMAGECOMMAND CREATED STATUS PORTS NAMESc6cbe131f851 nginx:v1 "nginx -g 'daemon of…" 3 seconds ago Up 3 seconds 0.0.0.0:49153->80/tcp, :::49153->80/tcp zealous_williams[root@localhost ~]#

此时去浏览器访问

无法访问到Nginx的网页

进入容器内修改配置文件[root@localhost ~]# docker exec -it c6cbe131f851 /bin/sh/ # cd /etc/nginx/conf.d//etc/nginx/conf.d # lsdefault.conf/etc/nginx/conf.d # vi default.conf# This is a default site configuration which will simply return 404, preventing# chance access to any other virtualhost.server {listen 80 default_server;listen [::]:80 default_server;# Everything is a 404location / {index index.html;//修改此行内容}# You may need this to prevent return 404 recursion.location = /404.html {internal;}}/etc/nginx/conf.d # nginx -s reload/etc/nginx/conf.d #

再次前往浏览器访问

2、源代码编译安装nginx

使用源代码编译部署

[root@localhost ~]# lspcre-8.44.tar.gz nginx-1.16.1.tar.gz[root@localhost ~]# vim DockerfileFROM alpine:latestADD nginx-1.16.1.tar.gz /home/ADD pcre-8.44.tar.gz /home/RUN echo /alpine/v3.10/main/ > /etc/apk/repositories && \echo /alpine/v3.10/community/ >> /etc/apk/repositories && \apk update && apk upgrade && \apk add gcc g++ make && \addgroup -S nginx && \adduser -DHS -s /sbin/nologin -G nginx nginx && \cd /home/nginx-1.16.1 && \./configure --prefix=/usr/local/nginx --with-pcre=/home/pcre-8.44 --without-http_gzip_module && \make && make install && \ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ && \mkdir -p /usr/local/nginx/conf/vhost/EXPOSE 80CMD ["/usr/sbin/nginx","-g","daemon off;"][root@localhost ~]# docker build -t nginx:v2 .Sending build context to Docker daemon 28.24MBStep 1/6 : FROM alpine:latest---> c059bfaa849cStep 2/6 : ADD nginx-1.16.1.tar.gz /home/---> 015442bdc2bfStep 3/6 : ADD pcre-8.44.tar.gz /home/---> 889676c60928编译安装省略........Removing intermediate container cb0e43ca0d6e---> 8b4c0b0006a6Step 5/6 : EXPOSE 80---> Running in d9d12481d882Removing intermediate container d9d12481d882---> 76163326c28aStep 6/6 : CMD ["/usr/sbin/nginx","-g","daemon off;"]---> Running in 9242698fb8c2Removing intermediate container 9242698fb8c2---> 6e10c5b19cb8Successfully built 6e10c5b19cb8Successfully tagged nginx:v2[root@localhost ~]#[root@localhost ~]# docker run -d -p 80:80 nginx:v253e27e344354316497b982550bb86254baf19c8d05d1eb939b3e2d1df8178a22[root@localhost ~]# docker psCONTAINER ID IMAGECOMMAND CREATEDSTATUSPORTS NAMES53e27e344354 nginx:v2 "/usr/sbin/nginx -g …" 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp crazy_aryabhata[root@localhost ~]#

前往浏览器访问 80 默认端口

如果觉得《Dockerfile镜像构建》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。