一、镜像制作的方法
1、本地导入导出镜像
请参考:Docker 架构原理及简单使用
导出:docker save nginx >/tmp/nginx.tar.gz 导入:docker load tar.gz
2、docker commit 命令创建镜像副本
请参考:Docker docker commit方法镜像制作
3、docker file
前面两种方法已经介绍过了,这里介绍docker file,生成环境推荐使用这种方法
二、docker file方法制作镜像
1、什么是docker file
用来全自动构建镜像文件,命名为Dockerfile
2、Dockerfile 文件编写指令及语法
1)指令
FROM
MAINTAINER
RUN
CMD
EXPOSE
ENV
ADD
COPY
ENTRYPOINT
VOLUME
USER
WORKDIR
ONBUILD
2)语法
引用了docker中文文档:http://www.docker.org.cn/dockerppt/114.html
1、FROM
例子:FROM centos
FROM指定构建镜像的基础源镜像,如果本地没有指定的镜像,则会自动从Docker的公共库pull镜像下来。
FROM必须是Dockerfile中非注释行的第一个指令,即一个Dockerfile从FROM语句开始
FROM可以在一个DOCKERfile中出现多次,如果有需求在一个Dockerfile中创建多个镜像
2、MAINTAINER
例子:MAINTAINER zxg [email protected]
指定创建镜像的用户
3、RUN <"executable","parm1","param2">
两种使用方式:
- RUN
- RUN "executable", "param1", "param2"
例子:RUN yum install wget -y
每条RUN指令将在当前镜像基础上执行指定命令,并提交为新的镜像,后续的RUN都在之前RUN提交后的镜像为基础,镜像是分层的,可以通过一个镜像的任何一个历史提交点来创建,类似源码的 版本控制 。
exec 方式会被解析为一个 JSON 数组,所以必须使用双引号而不是单引号。exec 方式不会调用一个命令 shell,所以也就不会继承相应的变量,如:
RUN [ "echo", "$HOME" ] #错误,这个个方法不会输出HOME变量,下面为正确方式
RUN [ "sh", "-c", "echo", "$HOME" ]
RUN 产生的缓存在下一次构建的适合是不会失效的,会被重用,可以使用--no-cache选择,即docker build-no-cache,如此便不会缓存
4、CMD <"executable",>
三种使用方式:
- CMD "executable","param1","param2"
- CMD "param1","param2"
- CMD command param1 param2 (shell form)
例子:CMD["nginx"]
CMD指定在 Dockerfile 中只能使用一次,如果有多个,则只有最后一个会生效。
CMD的目的是为了在启动容器时提供一个默认的命令执行选项。如果用户启动容器时指定了运行的命令,则会覆盖掉CMD指定的命令。
CMD会在启动容器的时候执行,build 时不执行,而RUN只是在构建镜像的时候执行,后续镜像构建完成之后,启动容器就与RUN无关了,这个初学者容易弄混这个概念,这里简单注解一下。
5、EXPOSE[ ...]
告诉docker服务端容器对外映射的本地端口,需要在docker run的使用使用-p或者-P选项生效
例子:EXPOSE 80
6、ENV
ENV7、ADD
ENV= 。。。
指定一个环节变量,会被后续RUN指令使用,并在容器运行时保留
例子:ENV myname zxg
ENV myhome beijing
ENV myname="zxg" myhome=beijing
ADD...
ADD复制本地主机文件、目录或者远程文件URLS从并且添加到容器指定路径中
支持通过Go的正则模式匹配,具体规则可参见Go filepath.Match
例子:ADD hom* /mydir/ #adds all files starting with ”hom“
ADD hom?.txt /mydir/ #?is replaced with any single character
ADD index.html /usr/share/nginx/html/index.html
注意如下:
- 路径必须是绝对路径,如果 不存在,会自动创建对应目录
- 路径必须是 Dockerfile 所在路径的相对路径
- 如果是一个目录,只会复制目录下的内容,而目录本身则不会被复制
8、COPY
COPY... 9、ENTRYPOINT
COPY复制新文件或者目录并且添加到容器指定路径中,用法和ADD相同,唯一区别时不能指定远程文件URLS。
ENTRYPOINT "executable","param1","param2"
ENTRYPOINT command param1 param2(shell form)
配置容器启动后执行的命令,并且不可被 docker run 提供的参数覆盖,而CMD是可以被覆盖的。如果需要覆盖,则可以使用docker run --entrypoint选项。
每个 Dockerfile 中只能有一个ENTRYPOINT,当指定多个时,只有最后一个生效。
Exec form ENTRYPOINT 例子
通过ENTRYPOINT使用 exec form 方式设置稳定的默认命令和选项,而使用CMD添加默认之外经常被改动的选项。
FROM ubuntu ENTRYPOINT ["top", "-b"] CMD ["-c"]
通过 Dockerfile 使用ENTRYPOINT展示前台运行 Apache 服务
FROM debian:stable RUN apt-get update && apt-get install -y --force-yes apache2 EXPOSE 80 443 VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"] ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
Shell form ENTRYPOINT 例子
这种方式会在/bin/sh -c中执行,会忽略任何CMD或者docker run命令行选项,为了确保docker stop能够停止长时间运行ENTRYPOINT的容器,确保执行的时候使用exec选项。
FROM ubuntu ENTRYPOINT exec top -b
如果在ENTRYPOINT忘记使用exec选项,则可以使用CMD补上:
FROM ubuntu ENTRYPOINT top -b CMD --ignored-param1 # --ignored-param2 ... --ignored-param3 ... 依此类推
10、VOLUME
VOLUME ["/data"]
创建一个可以从本地主机或其他容器挂载的挂载点,后续具体介绍
11、USER
USER daemon
指定运行容器时的用户名或UID,后续RUN、CMD、ENTERPOINT也会使用指定用户
12、WORKDIR
WORKDIR /path/to/workdir
为后续的RUN、CMD、ENTRYPOINT指令配置工作目录。可以使用多个WORKDIR指令,后续命令如果参数是相对路径,则会基于之前命令指定的路径
WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd
最终路径是/a/b/c
WORKDIR指令可以在ENV设置变量之后调用环境变量:
ENV DIRPATH /path
WORKDIR $DIRPATH/$DIRNAME
最终路径则为 /path/$DIRNAME
13、ONBUILD
ONBUILD [INSTRUCTION]
配置当所创建的镜像作为其他新创建镜像的基础镜像时,所执行的操作指令
例如:Dockerfile 使用如下的内容创建了镜像image-A:
[...]
ONBUILD ADD . /app/src
ONBUILD RUN /usr/local/bin/python-build --dri /app/src
[...]
3、例子examples,做一个centos安装了nginx及wget的镜像
[root@web1 docker]# vim Dockerfile #this is docker file for centos-nginx FROM centos MAINTAINER zxgRUN yum install wget -y RUN rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm N yum install nginx -y ▽DD index.html /usr/share/nginx/html/index.html RUN echo "daemon off;">>/etc/nginx/nginx.conf EXPOSE 80 CMD ["nginx"]
4、使用build命令制作镜像
1)build命令说明
$ docker build --help Usage: docker build [OPTIONS] PATH | URL | - Build a new image from the source code at PATH --force-rm=false Always remove intermediate containers, even after unsuccessful builds # 移除过渡容器,即使构建失败 --no-cache=false Do not use cache when building the image # 不实用 cache -q, --quiet=false Suppress the verbose output generated by the containers --rm=true Remove intermediate containers after a successful build # 构建成功后移除过渡层容器 -t, --tag="" Repository name (and optionally a tag) to be applied to the resulting image in case of success
2)制作镜像
docker build -t zxg/nginx1 .
整个过程还挺长,所以折叠了,可以点开看一下
[root@web1 docker]# docker build -t zxg/nginx1 . Sending build context to Docker daemon 3.072 kB Step 1/9 : FROM centos ---> 9f38484d220f Step 2/9 : MAINTAINER zxg---> Using cache ---> 35983aa687ed Step 3/9 : RUN yum install wget -y ---> Using cache ---> efb8205fc8d2 Step 4/9 : RUN rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm ---> Running in 56c5812e494f warning: /var/tmp/rpm-tmp.pS5KO3: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY Retrieving https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm Preparing... ######################################## Updating / installing... epel-release-7-11 ######################################## ---> cf40357c60bb Removing intermediate container 56c5812e494f Step 5/9 : RUN yum install nginx -y ---> Running in 36ebe7b47657 Loaded plugins: fastestmirror, ovl Loading mirror speeds from cached hostfile * base: mirrors.tuna.tsinghua.edu.cn * epel: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.aliyun.com * updates: mirrors.huaweicloud.com https://hkg.mirror.rackspace.com/epel/7/x86_64/repodata/192b605ef6d0f773ad6162832c60509e1bc0817e28662ea77340078287b3b4d8-updateinfo.xml.bz2: [Errno 14] HTTPS Error 404 - Not Found Trying other mirror. To address this issue please refer to the below wiki article https://wiki.centos.org/yum-errors If above article doesn't help to resolve this issue please use https://bugs.centos.org/. https://mirrors.njupt.edu.cn/epel/7/x86_64/repodata/dc10637460e531277a1cd82574de2dde2637fd3a9e90cd0282e9436d3a95b91d-primary.sqlite.bz2: [Errno 14] HTTPS Error 404 - Not Found Trying other mirror. http://fedora.cs.nctu.edu.tw/epel/7/x86_64/repodata/dc10637460e531277a1cd82574de2dde2637fd3a9e90cd0282e9436d3a95b91d-primary.sqlite.bz2: [Errno 14] HTTP Error 404 - Not Found Trying other mirror. Resolving Dependencies --> Running transaction check ---> Package nginx.x86_64 1:1.12.2-3.el7 will be installed --> Processing Dependency: nginx-all-modules = 1:1.12.2-3.el7 for package: 1:nginx-1.12.2-3.el7.x86_64 --> Processing Dependency: nginx-filesystem = 1:1.12.2-3.el7 for package: 1:nginx-1.12.2-3.el7.x86_64 --> Processing Dependency: nginx-filesystem for package: 1:nginx-1.12.2-3.el7.x86_64 --> Processing Dependency: openssl for package: 1:nginx-1.12.2-3.el7.x86_64 --> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.12.2-3.el7.x86_64 --> Running transaction check ---> Package gperftools-libs.x86_64 0:2.6.1-1.el7 will be installed ---> Package nginx-all-modules.noarch 1:1.12.2-3.el7 will be installed --> Processing Dependency: nginx-mod-http-geoip = 1:1.12.2-3.el7 for package: 1:nginx-all-modules-1.12.2-3.el7.noarch --> Processing Dependency: nginx-mod-http-image-filter = 1:1.12.2-3.el7 for package: 1:nginx-all-modules-1.12.2-3.el7.noarch --> Processing Dependency: nginx-mod-http-perl = 1:1.12.2-3.el7 for package: 1:nginx-all-modules-1.12.2-3.el7.noarch --> Processing Dependency: nginx-mod-http-xslt-filter = 1:1.12.2-3.el7 for package: 1:nginx-all-modules-1.12.2-3.el7.noarch --> Processing Dependency: nginx-mod-mail = 1:1.12.2-3.el7 for package: 1:nginx-all-modules-1.12.2-3.el7.noarch --> Processing Dependency: nginx-mod-stream = 1:1.12.2-3.el7 for package: 1:nginx-all-modules-1.12.2-3.el7.noarch ---> Package nginx-filesystem.noarch 1:1.12.2-3.el7 will be installed ---> Package openssl.x86_64 1:1.0.2k-16.el7_6.1 will be installed --> Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-16.el7_6.1 for package: 1:openssl-1.0.2k-16.el7_6.1.x86_64 --> Processing Dependency: make for package: 1:openssl-1.0.2k-16.el7_6.1.x86_64 --> Running transaction check ---> Package make.x86_64 1:3.82-23.el7 will be installed ---> Package nginx-mod-http-geoip.x86_64 1:1.12.2-3.el7 will be installed --> Processing Dependency: GeoIP for package: 1:nginx-mod-http-geoip-1.12.2-3.el7.x86_64 --> Processing Dependency: libGeoIP.so.1()(64bit) for package: 1:nginx-mod-http-geoip-1.12.2-3.el7.x86_64 ---> Package nginx-mod-http-image-filter.x86_64 1:1.12.2-3.el7 will be installed --> Processing Dependency: gd for package: 1:nginx-mod-http-image-filter-1.12.2-3.el7.x86_64 --> Processing Dependency: libgd.so.2()(64bit) for package: 1:nginx-mod-http-image-filter-1.12.2-3.el7.x86_64 ---> Package nginx-mod-http-perl.x86_64 1:1.12.2-3.el7 will be installed --> Processing Dependency: perl >= 5.006001 for package: 1:nginx-mod-http-perl-1.12.2-3.el7.x86_64 --> Processing Dependency: perl(:MODULE_COMPAT_5.16.3) for package: 1:nginx-mod-http-perl-1.12.2-3.el7.x86_64 --> Processing Dependency: perl(Exporter) for package: 1:nginx-mod-http-perl-1.12.2-3.el7.x86_64 --> Processing Dependency: perl(XSLoader) for package: 1:nginx-mod-http-perl-1.12.2-3.el7.x86_64 --> Processing Dependency: perl(constant) for package: 1:nginx-mod-http-perl-1.12.2-3.el7.x86_64 --> Processing Dependency: perl(strict) for package: 1:nginx-mod-http-perl-1.12.2-3.el7.x86_64 --> Processing Dependency: perl(warnings) for package: 1:nginx-mod-http-perl-1.12.2-3.el7.x86_64 --> Processing Dependency: libperl.so()(64bit) for package: 1:nginx-mod-http-perl-1.12.2-3.el7.x86_64 ---> Package nginx-mod-http-xslt-filter.x86_64 1:1.12.2-3.el7 will be installed --> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-3.el7.x86_64 --> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-3.el7.x86_64 --> Processing Dependency: libexslt.so.0()(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-3.el7.x86_64 --> Processing Dependency: libxslt.so.1()(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-3.el7.x86_64 ---> Package nginx-mod-mail.x86_64 1:1.12.2-3.el7 will be installed ---> Package nginx-mod-stream.x86_64 1:1.12.2-3.el7 will be installed ---> Package openssl-libs.x86_64 1:1.0.2k-16.el7 will be updated ---> Package openssl-libs.x86_64 1:1.0.2k-16.el7_6.1 will be an update --> Running transaction check ---> Package GeoIP.x86_64 0:1.5.0-13.el7 will be installed ---> Package gd.x86_64 0:2.0.35-26.el7 will be installed --> Processing Dependency: libpng15.so.15(PNG15_0)(64bit) for package: gd-2.0.35-26.el7.x86_64 --> Processing Dependency: libjpeg.so.62(LIBJPEG_6.2)(64bit) for package: gd-2.0.35-26.el7.x86_64 --> Processing Dependency: libpng15.so.15()(64bit) for package: gd-2.0.35-26.el7.x86_64 --> Processing Dependency: libjpeg.so.62()(64bit) for package: gd-2.0.35-26.el7.x86_64 --> Processing Dependency: libfreetype.so.6()(64bit) for package: gd-2.0.35-26.el7.x86_64 --> Processing Dependency: libfontconfig.so.1()(64bit) for package: gd-2.0.35-26.el7.x86_64 --> Processing Dependency: libXpm.so.4()(64bit) for package: gd-2.0.35-26.el7.x86_64 --> Processing Dependency: libX11.so.6()(64bit) for package: gd-2.0.35-26.el7.x86_64 ---> Package libxslt.x86_64 0:1.1.28-5.el7 will be installed ---> Package perl.x86_64 4:5.16.3-294.el7_6 will be installed --> Processing Dependency: perl(Socket) >= 1.3 for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(Scalar::Util) >= 1.10 for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl-macros for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(threads::shared) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(threads) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(Time::Local) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(Time::HiRes) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(Storable) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(Socket) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(Scalar::Util) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(Pod::Simple::XHTML) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(Pod::Simple::Search) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(Getopt::Long) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(Filter::Util::Call) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(File::Temp) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(File::Spec::Unix) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(File::Spec::Functions) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(File::Spec) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(File::Path) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(Cwd) for package: 4:perl-5.16.3-294.el7_6.x86_64 --> Processing Dependency: perl(Carp) for package: 4:perl-5.16.3-294.el7_6.x86_64 ---> Package perl-Exporter.noarch 0:5.68-3.el7 will be installed ---> Package perl-constant.noarch 0:1.27-2.el7 will be installed ---> Package perl-libs.x86_64 4:5.16.3-294.el7_6 will be installed --> Running transaction check ---> Package fontconfig.x86_64 0:2.13.0-4.3.el7 will be installed --> Processing Dependency: fontpackages-filesystem for package: fontconfig-2.13.0-4.3.el7.x86_64 --> Processing Dependency: dejavu-sans-fonts for package: fontconfig-2.13.0-4.3.el7.x86_64 ---> Package freetype.x86_64 0:2.8-12.el7_6.1 will be installed ---> Package libX11.x86_64 0:1.6.5-2.el7 will be installed --> Processing Dependency: libX11-common >= 1.6.5-2.el7 for package: libX11-1.6.5-2.el7.x86_64 --> Processing Dependency: libxcb.so.1()(64bit) for package: libX11-1.6.5-2.el7.x86_64 ---> Package libXpm.x86_64 0:3.5.12-1.el7 will be installed ---> Package libjpeg-turbo.x86_64 0:1.2.90-6.el7 will be installed ---> Package libpng.x86_64 2:1.5.13-7.el7_2 will be installed ---> Package perl-Carp.noarch 0:1.26-244.el7 will be installed ---> Package perl-File-Path.noarch 0:2.09-2.el7 will be installed ---> Package perl-File-Temp.noarch 0:0.23.01-3.el7 will be installed ---> Package perl-Filter.x86_64 0:1.49-3.el7 will be installed ---> Package perl-Getopt-Long.noarch 0:2.40-3.el7 will be installed --> Processing Dependency: perl(Pod::Usage) >= 1.14 for package: perl-Getopt-Long-2.40-3.el7.noarch --> Processing Dependency: perl(Text::ParseWords) for package: perl-Getopt-Long-2.40-3.el7.noarch ---> Package perl-PathTools.x86_64 0:3.40-5.el7 will be installed ---> Package perl-Pod-Simple.noarch 1:3.28-4.el7 will be installed --> Processing Dependency: perl(Pod::Escapes) >= 1.04 for package: 1:perl-Pod-Simple-3.28-4.el7.noarch --> Processing Dependency: perl(Encode) for package: 1:perl-Pod-Simple-3.28-4.el7.noarch ---> Package perl-Scalar-List-Utils.x86_64 0:1.27-248.el7 will be installed ---> Package perl-Socket.x86_64 0:2.010-4.el7 will be installed ---> Package perl-Storable.x86_64 0:2.45-3.el7 will be installed ---> Package perl-Time-HiRes.x86_64 4:1.9725-3.el7 will be installed ---> Package perl-Time-Local.noarch 0:1.2300-2.el7 will be installed ---> Package perl-macros.x86_64 4:5.16.3-294.el7_6 will be installed ---> Package perl-threads.x86_64 0:1.87-4.el7 will be installed ---> Package perl-threads-shared.x86_64 0:1.43-6.el7 will be installed --> Running transaction check ---> Package dejavu-sans-fonts.noarch 0:2.33-6.el7 will be installed --> Processing Dependency: dejavu-fonts-common = 2.33-6.el7 for package: dejavu-sans-fonts-2.33-6.el7.noarch ---> Package fontpackages-filesystem.noarch 0:1.44-8.el7 will be installed ---> Package libX11-common.noarch 0:1.6.5-2.el7 will be installed ---> Package libxcb.x86_64 0:1.13-1.el7 will be installed --> Processing Dependency: libXau.so.6()(64bit) for package: libxcb-1.13-1.el7.x86_64 ---> Package perl-Encode.x86_64 0:2.51-7.el7 will be installed ---> Package perl-Pod-Escapes.noarch 1:1.04-294.el7_6 will be installed ---> Package perl-Pod-Usage.noarch 0:1.63-3.el7 will be installed --> Processing Dependency: perl(Pod::Text) >= 3.15 for package: perl-Pod-Usage-1.63-3.el7.noarch --> Processing Dependency: perl-Pod-Perldoc for package: perl-Pod-Usage-1.63-3.el7.noarch ---> Package perl-Text-ParseWords.noarch 0:3.29-4.el7 will be installed --> Running transaction check ---> Package dejavu-fonts-common.noarch 0:2.33-6.el7 will be installed ---> Package libXau.x86_64 0:1.0.8-2.1.el7 will be installed ---> Package perl-Pod-Perldoc.noarch 0:3.20-4.el7 will be installed --> Processing Dependency: perl(parent) for package: perl-Pod-Perldoc-3.20-4.el7.noarch --> Processing Dependency: perl(HTTP::Tiny) for package: perl-Pod-Perldoc-3.20-4.el7.noarch --> Processing Dependency: groff-base for package: perl-Pod-Perldoc-3.20-4.el7.noarch ---> Package perl-podlators.noarch 0:2.5.1-3.el7 will be installed --> Running transaction check ---> Package groff-base.x86_64 0:1.22.2-8.el7 will be installed ---> Package perl-HTTP-Tiny.noarch 0:0.033-3.el7 will be installed ---> Package perl-parent.noarch 1:0.225-244.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: nginx x86_64 1:1.12.2-3.el7 epel 531 k Installing for dependencies: GeoIP x86_64 1.5.0-13.el7 base 1.5 M dejavu-fonts-common noarch 2.33-6.el7 base 64 k dejavu-sans-fonts noarch 2.33-6.el7 base 1.4 M fontconfig x86_64 2.13.0-4.3.el7 base 254 k fontpackages-filesystem noarch 1.44-8.el7 base 9.9 k freetype x86_64 2.8-12.el7_6.1 updates 380 k gd x86_64 2.0.35-26.el7 base 146 k gperftools-libs x86_64 2.6.1-1.el7 base 272 k groff-base x86_64 1.22.2-8.el7 base 942 k libX11 x86_64 1.6.5-2.el7 base 606 k libX11-common noarch 1.6.5-2.el7 base 164 k libXau x86_64 1.0.8-2.1.el7 base 29 k libXpm x86_64 3.5.12-1.el7 base 55 k libjpeg-turbo x86_64 1.2.90-6.el7 base 134 k libpng x86_64 2:1.5.13-7.el7_2 base 213 k libxcb x86_64 1.13-1.el7 base 214 k libxslt x86_64 1.1.28-5.el7 base 242 k make x86_64 1:3.82-23.el7 base 420 k nginx-all-modules noarch 1:1.12.2-3.el7 epel 16 k nginx-filesystem noarch 1:1.12.2-3.el7 epel 17 k nginx-mod-http-geoip x86_64 1:1.12.2-3.el7 epel 23 k nginx-mod-http-image-filter x86_64 1:1.12.2-3.el7 epel 27 k nginx-mod-http-perl x86_64 1:1.12.2-3.el7 epel 36 k nginx-mod-http-xslt-filter x86_64 1:1.12.2-3.el7 epel 26 k nginx-mod-mail x86_64 1:1.12.2-3.el7 epel 54 k nginx-mod-stream x86_64 1:1.12.2-3.el7 epel 76 k openssl x86_64 1:1.0.2k-16.el7_6.1 updates 493 k perl x86_64 4:5.16.3-294.el7_6 updates 8.0 M perl-Carp noarch 1.26-244.el7 base 19 k perl-Encode x86_64 2.51-7.el7 base 1.5 M perl-Exporter noarch 5.68-3.el7 base 28 k perl-File-Path noarch 2.09-2.el7 base 26 k perl-File-Temp noarch 0.23.01-3.el7 base 56 k perl-Filter x86_64 1.49-3.el7 base 76 k perl-Getopt-Long noarch 2.40-3.el7 base 56 k perl-HTTP-Tiny noarch 0.033-3.el7 base 38 k perl-PathTools x86_64 3.40-5.el7 base 82 k perl-Pod-Escapes noarch 1:1.04-294.el7_6 updates 51 k perl-Pod-Perldoc noarch 3.20-4.el7 base 87 k perl-Pod-Simple noarch 1:3.28-4.el7 base 216 k perl-Pod-Usage noarch 1.63-3.el7 base 27 k perl-Scalar-List-Utils x86_64 1.27-248.el7 base 36 k perl-Socket x86_64 2.010-4.el7 base 49 k perl-Storable x86_64 2.45-3.el7 base 77 k perl-Text-ParseWords noarch 3.29-4.el7 base 14 k perl-Time-HiRes x86_64 4:1.9725-3.el7 base 45 k perl-Time-Local noarch 1.2300-2.el7 base 24 k perl-constant noarch 1.27-2.el7 base 19 k perl-libs x86_64 4:5.16.3-294.el7_6 updates 688 k perl-macros x86_64 4:5.16.3-294.el7_6 updates 44 k perl-parent noarch 1:0.225-244.el7 base 12 k perl-podlators noarch 2.5.1-3.el7 base 112 k perl-threads x86_64 1.87-4.el7 base 49 k perl-threads-shared x86_64 1.43-6.el7 base 39 k Updating for dependencies: openssl-libs x86_64 1:1.0.2k-16.el7_6.1 updates 1.2 M Transaction Summary ================================================================================ Install 1 Package (+54 Dependent packages) Upgrade ( 1 Dependent package) Total download size: 21 M Downloading packages: Delta RPMs disabled because /usr/bin/applydeltarpm not installed. warning: /var/cache/yum/x86_64/7/epel/packages/nginx-1.12.2-3.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY Public key for nginx-1.12.2-3.el7.x86_64.rpm is not installed -------------------------------------------------------------------------------- Total 868 kB/s | 21 MB 00:24 Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 Importing GPG key 0x352C64E5: Userid : "Fedora EPEL (7) " Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5 Package : epel-release-7-11.noarch (installed) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 Running transaction check Running transaction test Transaction test succeeded Running transaction Warning: RPMDB altered outside of yum. Updating : 1:openssl-libs-1.0.2k-16.el7_6.1.x86_64 1/57 Installing : fontpackages-filesystem-1.44-8.el7.noarch 2/57 Installing : 2:libpng-1.5.13-7.el7_2.x86_64 3/57 Installing : freetype-2.8-12.el7_6.1.x86_64 4/57 Installing : dejavu-fonts-common-2.33-6.el7.noarch 5/57 Installing : dejavu-sans-fonts-2.33-6.el7.noarch 6/57 Installing : fontconfig-2.13.0-4.3.el7.x86_64 7/57 Installing : 1:nginx-filesystem-1.12.2-3.el7.noarch 8/57 Installing : libX11-common-1.6.5-2.el7.noarch 9/57 Installing : gperftools-libs-2.6.1-1.el7.x86_64 10/57 Installing : libXau-1.0.8-2.1.el7.x86_64 11/57 Installing : libxcb-1.13-1.el7.x86_64 12/57 Installing : libX11-1.6.5-2.el7.x86_64 13/57 Installing : libXpm-3.5.12-1.el7.x86_64 14/57 Installing : libxslt-1.1.28-5.el7.x86_64 15/57 Installing : groff-base-1.22.2-8.el7.x86_64 16/57 Installing : 1:perl-parent-0.225-244.el7.noarch 17/57 Installing : perl-HTTP-Tiny-0.033-3.el7.noarch 18/57 Installing : perl-podlators-2.5.1-3.el7.noarch 19/57 Installing : perl-Pod-Perldoc-3.20-4.el7.noarch 20/57 Installing : 1:perl-Pod-Escapes-1.04-294.el7_6.noarch 21/57 Installing : perl-Text-ParseWords-3.29-4.el7.noarch 22/57 Installing : perl-Encode-2.51-7.el7.x86_64 23/57 Installing : perl-Pod-Usage-1.63-3.el7.noarch 24/57 Installing : 4:perl-libs-5.16.3-294.el7_6.x86_64 25/57 Installing : 4:perl-macros-5.16.3-294.el7_6.x86_64 26/57 Installing : 4:perl-Time-HiRes-1.9725-3.el7.x86_64 27/57 Installing : perl-Exporter-5.68-3.el7.noarch 28/57 Installing : perl-constant-1.27-2.el7.noarch 29/57 Installing : perl-Time-Local-1.2300-2.el7.noarch 30/57 Installing : perl-Carp-1.26-244.el7.noarch 31/57 Installing : perl-Storable-2.45-3.el7.x86_64 32/57 Installing : perl-PathTools-3.40-5.el7.x86_64 33/57 Installing : perl-Scalar-List-Utils-1.27-248.el7.x86_64 34/57 Installing : perl-File-Temp-0.23.01-3.el7.noarch 35/57 Installing : perl-File-Path-2.09-2.el7.noarch 36/57 Installing : perl-threads-shared-1.43-6.el7.x86_64 37/57 Installing : perl-threads-1.87-4.el7.x86_64 38/57 Installing : perl-Filter-1.49-3.el7.x86_64 39/57 Installing : perl-Socket-2.010-4.el7.x86_64 40/57 Installing : 1:perl-Pod-Simple-3.28-4.el7.noarch 41/57 Installing : perl-Getopt-Long-2.40-3.el7.noarch 42/57 Installing : 4:perl-5.16.3-294.el7_6.x86_64 43/57 Installing : GeoIP-1.5.0-13.el7.x86_64 44/57 Installing : libjpeg-turbo-1.2.90-6.el7.x86_64 45/57 Installing : gd-2.0.35-26.el7.x86_64 46/57 Installing : 1:make-3.82-23.el7.x86_64 47/57 Installing : 1:openssl-1.0.2k-16.el7_6.1.x86_64 48/57 Installing : 1:nginx-mod-http-image-filter-1.12.2-3.el7.x86_64 49/57 Installing : 1:nginx-mod-mail-1.12.2-3.el7.x86_64 50/57 Installing : 1:nginx-mod-stream-1.12.2-3.el7.x86_64 51/57 Installing : 1:nginx-mod-http-xslt-filter-1.12.2-3.el7.x86_64 52/57 Installing : 1:nginx-mod-http-perl-1.12.2-3.el7.x86_64 53/57 Installing : 1:nginx-mod-http-geoip-1.12.2-3.el7.x86_64 54/57 Installing : 1:nginx-all-modules-1.12.2-3.el7.noarch 55/57 Installing : 1:nginx-1.12.2-3.el7.x86_64 56/57 Cleanup : 1:openssl-libs-1.0.2k-16.el7.x86_64 57/57 Verifying : 1:nginx-all-modules-1.12.2-3.el7.noarch 1/57 Verifying : fontconfig-2.13.0-4.3.el7.x86_64 2/57 Verifying : perl-HTTP-Tiny-0.033-3.el7.noarch 3/57 Verifying : 1:nginx-mod-http-image-filter-1.12.2-3.el7.x86_64 4/57 Verifying : perl-threads-shared-1.43-6.el7.x86_64 5/57 Verifying : 4:perl-Time-HiRes-1.9725-3.el7.x86_64 6/57 Verifying : 1:perl-Pod-Escapes-1.04-294.el7_6.noarch 7/57 Verifying : 1:make-3.82-23.el7.x86_64 8/57 Verifying : perl-Exporter-5.68-3.el7.noarch 9/57 Verifying : perl-constant-1.27-2.el7.noarch 10/57 Verifying : perl-PathTools-3.40-5.el7.x86_64 11/57 Verifying : 1:nginx-1.12.2-3.el7.x86_64 12/57 Verifying : 2:libpng-1.5.13-7.el7_2.x86_64 13/57 Verifying : 1:nginx-mod-mail-1.12.2-3.el7.x86_64 14/57 Verifying : 1:nginx-mod-stream-1.12.2-3.el7.x86_64 15/57 Verifying : dejavu-fonts-common-2.33-6.el7.noarch 16/57 Verifying : fontpackages-filesystem-1.44-8.el7.noarch 17/57 Verifying : libjpeg-turbo-1.2.90-6.el7.x86_64 18/57 Verifying : 1:perl-parent-0.225-244.el7.noarch 19/57 Verifying : 1:nginx-mod-http-xslt-filter-1.12.2-3.el7.x86_64 20/57 Verifying : GeoIP-1.5.0-13.el7.x86_64 21/57 Verifying : 4:perl-libs-5.16.3-294.el7_6.x86_64 22/57 Verifying : groff-base-1.22.2-8.el7.x86_64 23/57 Verifying : perl-File-Temp-0.23.01-3.el7.noarch 24/57 Verifying : 1:perl-Pod-Simple-3.28-4.el7.noarch 25/57 Verifying : perl-Getopt-Long-2.40-3.el7.noarch 26/57 Verifying : perl-Time-Local-1.2300-2.el7.noarch 27/57 Verifying : libxcb-1.13-1.el7.x86_64 28/57 Verifying : 4:perl-macros-5.16.3-294.el7_6.x86_64 29/57 Verifying : 4:perl-5.16.3-294.el7_6.x86_64 30/57 Verifying : libXpm-3.5.12-1.el7.x86_64 31/57 Verifying : 1:nginx-mod-http-perl-1.12.2-3.el7.x86_64 32/57 Verifying : 1:openssl-1.0.2k-16.el7_6.1.x86_64 33/57 Verifying : perl-Carp-1.26-244.el7.noarch 34/57 Verifying : libxslt-1.1.28-5.el7.x86_64 35/57 Verifying : libX11-1.6.5-2.el7.x86_64 36/57 Verifying : perl-Storable-2.45-3.el7.x86_64 37/57 Verifying : dejavu-sans-fonts-2.33-6.el7.noarch 38/57 Verifying : perl-Scalar-List-Utils-1.27-248.el7.x86_64 39/57 Verifying : gd-2.0.35-26.el7.x86_64 40/57 Verifying : 1:nginx-mod-http-geoip-1.12.2-3.el7.x86_64 41/57 Verifying : perl-Pod-Usage-1.63-3.el7.noarch 42/57 Verifying : perl-Encode-2.51-7.el7.x86_64 43/57 Verifying : perl-Pod-Perldoc-3.20-4.el7.noarch 44/57 Verifying : perl-podlators-2.5.1-3.el7.noarch 45/57 Verifying : libXau-1.0.8-2.1.el7.x86_64 46/57 Verifying : perl-File-Path-2.09-2.el7.noarch 47/57 Verifying : perl-threads-1.87-4.el7.x86_64 48/57 Verifying : gperftools-libs-2.6.1-1.el7.x86_64 49/57 Verifying : libX11-common-1.6.5-2.el7.noarch 50/57 Verifying : perl-Filter-1.49-3.el7.x86_64 51/57 Verifying : freetype-2.8-12.el7_6.1.x86_64 52/57 Verifying : perl-Text-ParseWords-3.29-4.el7.noarch 53/57 Verifying : perl-Socket-2.010-4.el7.x86_64 54/57 Verifying : 1:nginx-filesystem-1.12.2-3.el7.noarch 55/57 Verifying : 1:openssl-libs-1.0.2k-16.el7_6.1.x86_64 56/57 Verifying : 1:openssl-libs-1.0.2k-16.el7.x86_64 57/57 Installed: nginx.x86_64 1:1.12.2-3.el7 Dependency Installed: GeoIP.x86_64 0:1.5.0-13.el7 dejavu-fonts-common.noarch 0:2.33-6.el7 dejavu-sans-fonts.noarch 0:2.33-6.el7 fontconfig.x86_64 0:2.13.0-4.3.el7 fontpackages-filesystem.noarch 0:1.44-8.el7 freetype.x86_64 0:2.8-12.el7_6.1 gd.x86_64 0:2.0.35-26.el7 gperftools-libs.x86_64 0:2.6.1-1.el7 groff-base.x86_64 0:1.22.2-8.el7 libX11.x86_64 0:1.6.5-2.el7 libX11-common.noarch 0:1.6.5-2.el7 libXau.x86_64 0:1.0.8-2.1.el7 libXpm.x86_64 0:3.5.12-1.el7 libjpeg-turbo.x86_64 0:1.2.90-6.el7 libpng.x86_64 2:1.5.13-7.el7_2 libxcb.x86_64 0:1.13-1.el7 libxslt.x86_64 0:1.1.28-5.el7 make.x86_64 1:3.82-23.el7 nginx-all-modules.noarch 1:1.12.2-3.el7 nginx-filesystem.noarch 1:1.12.2-3.el7 nginx-mod-http-geoip.x86_64 1:1.12.2-3.el7 nginx-mod-http-image-filter.x86_64 1:1.12.2-3.el7 nginx-mod-http-perl.x86_64 1:1.12.2-3.el7 nginx-mod-http-xslt-filter.x86_64 1:1.12.2-3.el7 nginx-mod-mail.x86_64 1:1.12.2-3.el7 nginx-mod-stream.x86_64 1:1.12.2-3.el7 openssl.x86_64 1:1.0.2k-16.el7_6.1 perl.x86_64 4:5.16.3-294.el7_6 perl-Carp.noarch 0:1.26-244.el7 perl-Encode.x86_64 0:2.51-7.el7 perl-Exporter.noarch 0:5.68-3.el7 perl-File-Path.noarch 0:2.09-2.el7 perl-File-Temp.noarch 0:0.23.01-3.el7 perl-Filter.x86_64 0:1.49-3.el7 perl-Getopt-Long.noarch 0:2.40-3.el7 perl-HTTP-Tiny.noarch 0:0.033-3.el7 perl-PathTools.x86_64 0:3.40-5.el7 perl-Pod-Escapes.noarch 1:1.04-294.el7_6 perl-Pod-Perldoc.noarch 0:3.20-4.el7 perl-Pod-Simple.noarch 1:3.28-4.el7 perl-Pod-Usage.noarch 0:1.63-3.el7 perl-Scalar-List-Utils.x86_64 0:1.27-248.el7 perl-Socket.x86_64 0:2.010-4.el7 perl-Storable.x86_64 0:2.45-3.el7 perl-Text-ParseWords.noarch 0:3.29-4.el7 perl-Time-HiRes.x86_64 4:1.9725-3.el7 perl-Time-Local.noarch 0:1.2300-2.el7 perl-constant.noarch 0:1.27-2.el7 perl-libs.x86_64 4:5.16.3-294.el7_6 perl-macros.x86_64 4:5.16.3-294.el7_6 perl-parent.noarch 1:0.225-244.el7 perl-podlators.noarch 0:2.5.1-3.el7 perl-threads.x86_64 0:1.87-4.el7 perl-threads-shared.x86_64 0:1.43-6.el7 Dependency Updated: openssl-libs.x86_64 1:1.0.2k-16.el7_6.1 Complete! ---> 996824f87698 Removing intermediate container 36ebe7b47657 Step 6/9 : ADD index.html /usr/share/nginx/html/index.html ---> a21387c003a7 Removing intermediate container 74dfd64941b1 Step 7/9 : RUN echo "daemon off;">>/etc/nginx/nginx.conf ---> Running in bd60f92d877e ---> 3b20cd6163a8 Removing intermediate container bd60f92d877e Step 8/9 : EXPOSE 80 ---> Running in 54e388a3fd21 ---> 2aeecbaba79b Removing intermediate container 54e388a3fd21 Step 9/9 : CMD nginx ---> Running in 32d4b23b050c ---> 3babdf3c6c6d Removing intermediate container 32d4b23b050c Successfully built 3babdf3c6c6d
5、验证一下,并用curl测试
[root@web1 docker]# docker run -d --name my_nginx1 zxg/nginx1 #后台运行整个容器 751a8f9dda48c034d40b8855b3dd6c7aaf785eeaa9ae404c5eb3c0271e434f50 [root@web1 docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 751a8f9dda48 zxg/nginx1 "nginx" About a minute ago Up About a minute 80/tcp my_nginx1 [root@web1 docker]# docker exec -it my_nginx1 bash #进入容器 [root@751a8f9dda48 /]# cat /usr/share/nginx/html/index.html #查看文件是否添加到容器 this is docker-centos7-nginx1 [root@751a8f9dda48 /]# cat /etc/nginx/nginx.conf # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } # Settings for a TLS enabled server. # # server { # listen 443 ssl http2 default_server; # listen [::]:443 ssl http2 default_server; # server_name _; # root /usr/share/nginx/html; # # ssl_certificate "/etc/pki/nginx/server.crt"; # ssl_certificate_key "/etc/pki/nginx/private/server.key"; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 10m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # # # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; # # location / { # } # # error_page 404 /404.html; # location = /40x.html { # } # # error_page 500 502 503 504 /50x.html; # location = /50x.html { # } # } } daemon off; [root@751a8f9dda48 /]# exit exit [root@web1 docker]#
[root@751a8f9dda48 html]# curl 127.0.0.1 this is docker-centos7-nginx1 [root@751a8f9dda48 html]#
[root@web1 ~]# docker inspect my_nginx1 |grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2", [root@web1 ~]# curl 172.17.0.2 this is docker-centos7-nginx1 [root@web1 ~]#
转载请注明出处:https://www.cnblogs.com/zhangxingeng/p/11394374.html