一、鏡像制作的方法
1.本地導入導出鏡像
請參考:Docker 架構原理及簡單使用
導出:docker save Nginx >/tmp/nginx.tar.gz 導入:docker load </tmp/nginx.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 <image> 例子:FROM centos FROM指定構建鏡像的基礎源鏡像,如果本地沒有指定的鏡像,則會自動從Docker的公共庫pull鏡像下來。 FROM必須是Dockerfile中非注釋行的第一個指令,即一個Dockerfile從FROM語句開始 FROM可以在一個DOCKERfile中出現多次,如果有需求在一個Dockerfile中創建多個鏡像 2.MAINTAINER <name> 例子:MAINTAINER zxg zhutoyearn@163.com 指定創建鏡像的用戶 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 <port>[<port>...] 告訴docker服務端容器對外映射的本地端口,需要在docker run的使用使用-p或者-P選項生效 例子:EXPOSE 80 6.ENV ENV <key> <value> ENV <key>=<value>。。。 指定一個環節變量,會被后續RUN指令使用,并在容器運行時保留 例子:ENV myname zxg ENV myhome beijing ENV myname="zxg" myhome=beijing 7.ADD ADD <src>...<dest> 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 <src>...<dest> COPY復制新文件或者目錄并且添加到容器指定路徑中,用法和ADD相同,唯一區別時不能指定遠程文件URLS。 9.ENTRYPOINT 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 zxg <victor@docker.com> RUN 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 <victor@docker.com> ---> 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) <epel@fedoraproject.org>" 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 ~]#