欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > 【第六节】docker应用系列篇: 使用Dockerfile构建nginx

【第六节】docker应用系列篇: 使用Dockerfile构建nginx

2025/11/9 6:51:50 来源:https://blog.csdn.net/spark_dev/article/details/144440573  浏览:    关键词:【第六节】docker应用系列篇: 使用Dockerfile构建nginx

系列文章目录

【第六节】docker应用系列篇: 使用Dockerfile构建nginx

  • 系列文章目录
  • 前言
  • 一、 准备dockerfile
    • 1. 简单dockerFile
    • 1.2 解决网络问题的dockerfile
  • 2. 构建结果:
  • 3. 构建过程:


前言


提示:以下是本篇文章正文内容,下面案例可供参考

一、 准备dockerfile

1. 简单dockerFile

[root@localhost ~]# mkdir nginxroot
[root@localhost ~]# cd nginxroot
[root@localhost nginxroot]#
[root@localhost nginxroot]# echo "nginx's running" >> index.html
[root@localhost nginxroot]# ls
index.html
[root@localhost nginxroot]# cat index.html
nginx's running
[root@localhost nginxroot]# vim Dockerfile
[root@localhost nginxroot]# cat Dockerfile
FROM centos:centos7MAINTAINER "www.kubemsb.com"RUN yum -y install wgetRUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repoRUN yum -y install nginxADD index.html /usr/share/nginx/html/RUN echo "daemon off;" >> /etc/nginx/nginx.confEXPOSE 80CMD /usr/sbin/nginx

1.2 解决网络问题的dockerfile

上面的dockerFile如果有网络问题,无法使用yum,可以更换yum源, 使用阿里的yum源

[root@vm31 nginxroot-ali]# cat Dockerfile 
FROM centos:centos7
MAINTAINER "www.kuberick.com"RUN  rm -f /etc/yum.repos.d/* ADD ali.repo /etc/yum.repos.d/RUN  yum -y install wgetRUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repoRUN yum -y install nginxADD index.html /usr/share/nginx/html/RUN echo "daemon off;" >> /etc/nginx/nginx.confEXPOSE 80CMD /usr/sbin/nginx

ali.repo

[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

2. 构建结果:

[root@localhost nginxroot]# docker build -t centos7-nginx:v1 .
[root@vm31 ~]# docker images
REPOSITORY                        TAG       IMAGE ID       CREATED         SIZE
centos7-nginx                     v1        81440cec486b   7 minutes ago   757MB

3. 构建过程:

输出:
Sending build context to Docker daemon  3.072kB
第一步:下载基础镜像
Step 1/9 : FROM centos:centos7---> eeb6ee3f44bd
第二步:维护者信息
Step 2/9 : MAINTAINER "www.kubemsb.com"---> Using cache---> f978e524772c第三步:安装wget
Step 3/9 : RUN yum -y install wget---> Running in 4e0fc3854088
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors* base: mirrors.huaweicloud.com* extras: mirrors.tuna.tsinghua.edu.cn* updates: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-18.el7_6.1 will be installed
--> Finished Dependency ResolutionDependencies Resolved================================================================================Package        Arch             Version                   Repository      Size
================================================================================
Installing:wget           x86_64           1.14-18.el7_6.1           base           547 kTransaction Summary
================================================================================
Install  1 PackageTotal download size: 547 k
Installed size: 2.0 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/wget-1.14-18.el7_6.1.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for wget-1.14-18.el7_6.1.x86_64.rpm is not installed
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5Package    : centos-release-7-9.2009.0.el7.centos.x86_64 (@CentOS)From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transactionInstalling : wget-1.14-18.el7_6.1.x86_64                                  1/1
install-info: No such file or directory for /usr/share/info/wget.info.gzVerifying  : wget-1.14-18.el7_6.1.x86_64                                  1/1Installed:wget.x86_64 0:1.14-18.el7_6.1Complete!
Removing intermediate container 4e0fc3854088---> 369e33a2152a第四步:使用wget下载YUM源
Step 4/9 : RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo---> Running in 4bdfc0a1c844
--2022-02-10 06:18:07--  http://mirrors.aliyun.com/repo/epel-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 221.195.209.65, 221.195.209.64, 221.195.209.70, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|221.195.209.65|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 664 [application/octet-stream]
Saving to: '/etc/yum.repos.d/epel.repo'0K                                                       100%  158M=0s2022-02-10 06:18:07 (158 MB/s) - '/etc/yum.repos.d/epel.repo' saved [664/664]Removing intermediate container 4bdfc0a1c844---> 1d73faa62447第五步:安装Nginx
Step 5/9 : RUN yum -y install nginx---> Running in 51b50c2ce841
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile* base: mirrors.huaweicloud.com* extras: mirrors.tuna.tsinghua.edu.cn* updates: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.20.1-9.el7 will be installed
--> Processing Dependency: nginx-filesystem = 1:1.20.1-9.el7 for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: libcrypto.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_1)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: nginx-filesystem for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: openssl for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: redhat-indexhtml for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: system-logos for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: libcrypto.so.1.1()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: libssl.so.1.1()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
--> Running transaction check
---> Package centos-indexhtml.noarch 0:7-9.el7.centos will be installed
---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed
---> Package gperftools-libs.x86_64 0:2.6.1-1.el7 will be installed
---> Package nginx-filesystem.noarch 1:1.20.1-9.el7 will be installed
---> Package openssl.x86_64 1:1.0.2k-24.el7_9 will be installed
--> Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-24.el7_9 for package: 1:openssl-1.0.2k-24.el7_9.x86_64
--> Processing Dependency: make for package: 1:openssl-1.0.2k-24.el7_9.x86_64
---> Package openssl11-libs.x86_64 1:1.1.1k-2.el7 will be installed
--> Running transaction check
---> Package make.x86_64 1:3.82-24.el7 will be installed
---> Package openssl-libs.x86_64 1:1.0.2k-19.el7 will be updated
---> Package openssl-libs.x86_64 1:1.0.2k-24.el7_9 will be an update
--> Finished Dependency ResolutionDependencies Resolved================================================================================Package               Arch        Version                   Repository    Size
================================================================================
Installing:nginx                 x86_64      1:1.20.1-9.el7            epel         587 k
Installing for dependencies:centos-indexhtml      noarch      7-9.el7.centos            base          92 kcentos-logos          noarch      70.0.6-3.el7.centos       base          21 Mgperftools-libs       x86_64      2.6.1-1.el7               base         272 kmake                  x86_64      1:3.82-24.el7             base         421 knginx-filesystem      noarch      1:1.20.1-9.el7            epel          24 kopenssl               x86_64      1:1.0.2k-24.el7_9         updates      494 kopenssl11-libs        x86_64      1:1.1.1k-2.el7            epel         1.5 M
Updating for dependencies:openssl-libs          x86_64      1:1.0.2k-24.el7_9         updates      1.2 MTransaction Summary
================================================================================
Install  1 Package  (+7 Dependent packages)
Upgrade             ( 1 Dependent package)Total download size: 26 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
--------------------------------------------------------------------------------
Total                                              3.1 MB/s |  26 MB  00:08
Running transaction check
Running transaction test
Transaction test succeeded
Running transactionInstalling : centos-logos-70.0.6-3.el7.centos.noarch                     1/10Installing : centos-indexhtml-7-9.el7.centos.noarch                      2/10Installing : 1:make-3.82-24.el7.x86_64                                   3/10Installing : gperftools-libs-2.6.1-1.el7.x86_64                          4/10Installing : 1:openssl11-libs-1.1.1k-2.el7.x86_64                        5/10Updating   : 1:openssl-libs-1.0.2k-24.el7_9.x86_64                       6/10Installing : 1:openssl-1.0.2k-24.el7_9.x86_64                            7/10Installing : 1:nginx-filesystem-1.20.1-9.el7.noarch                      8/10Installing : 1:nginx-1.20.1-9.el7.x86_64                                 9/10Cleanup    : 1:openssl-libs-1.0.2k-19.el7.x86_64                        10/10Verifying  : 1:nginx-filesystem-1.20.1-9.el7.noarch                      1/10Verifying  : 1:nginx-1.20.1-9.el7.x86_64                                 2/10Verifying  : 1:openssl-libs-1.0.2k-24.el7_9.x86_64                       3/10Verifying  : 1:openssl11-libs-1.1.1k-2.el7.x86_64                        4/10Verifying  : gperftools-libs-2.6.1-1.el7.x86_64                          5/10Verifying  : 1:make-3.82-24.el7.x86_64                                   6/10Verifying  : 1:openssl-1.0.2k-24.el7_9.x86_64                            7/10Verifying  : centos-indexhtml-7-9.el7.centos.noarch                      8/10Verifying  : centos-logos-70.0.6-3.el7.centos.noarch                     9/10Verifying  : 1:openssl-libs-1.0.2k-19.el7.x86_64                        10/10Installed:nginx.x86_64 1:1.20.1-9.el7Dependency Installed:centos-indexhtml.noarch 0:7-9.el7.centoscentos-logos.noarch 0:70.0.6-3.el7.centosgperftools-libs.x86_64 0:2.6.1-1.el7make.x86_64 1:3.82-24.el7nginx-filesystem.noarch 1:1.20.1-9.el7openssl.x86_64 1:1.0.2k-24.el7_9openssl11-libs.x86_64 1:1.1.1k-2.el7Dependency Updated:openssl-libs.x86_64 1:1.0.2k-24.el7_9Complete!
Removing intermediate container 51b50c2ce841---> 88a7d7a2c522第六步:添加文件至容器
Step 6/9 : ADD index.html /usr/share/nginx/html/---> a2226a4d6720
第七步:设置nginx服务运行方式
Step 7/9 : RUN echo "daemon off;" >> /etc/nginx/nginx.conf---> Running in 01d623937807
Removing intermediate container 01d623937807---> 53fddea5b491第八步:暴露端口
Step 8/9 : EXPOSE 80---> Running in 9b73fcf7ee1b
Removing intermediate container 9b73fcf7ee1b---> 903377216b23
第九步:运行命令,执行nginx二进制文件
Step 9/9 : CMD /usr/sbin/nginx---> Running in 58037652952c
Removing intermediate container 58037652952c---> 944d27b80f1f生成镜像,并为镜像打标记:
Successfully built 944d27b80f1f
Successfully tagged centos7-nginx:v1
---

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词