Docker镜像-基于DockerFile制作yum版nginx镜像

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 本文介绍了如何使用Dockerfile制作一个基于CentOS 7.6.1810的yum版nginx镜像,并提供了详细的步骤和命令。

作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。

  DockerFile可以说是一种能被Docker程序解释的脚本,DockerFile是一条条指令组成的,每一条指令对应Linux下面的一条命令,Docker程序将这些DockerFile指令再翻译成真正的Linux命令,其有自己的书写方式和支持的命令。

  Docker程序读取DockerFile并根据指令生成对应的Docker镜像,相比手动生成镜像的方式(可参考:https://www.cnblogs.com/yinzhengjie/p/12190111.html),DockerFile更能直观的展示镜像是怎么产生的,有了DockerFile,当后期有额外的需求时,只要在之前的DockerFile添加或修改响应的命令即可重新生成新的Docker镜像,避免了重复手动执行镜像的麻烦。

一.下载centos镜像并初始化系统

1>.下载CentOS指定版本镜像(默认下载最新的版本,而我指定的是CentOS 7.6主流版本)

[root@docker201.yinzhengjie.org.cn ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker image pull centos:centos7.6.1810
centos7.6.1810: Pulling from library/centos
ac9208207ada: Pull complete 
Digest: sha256:62d9e1c2daa91166139b51577fe4f4f6b4cc41a3a2c7fc36bd895e2a17a3e4e6
Status: Downloaded newer image for centos:centos7.6.1810
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              centos7.6.1810      f1cb7c7d58b7        10 months ago       202MB
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]#

2>.在宿主机上创建存放DockerFile的存储目录(目录结构按照业务类型或者系统类型等方式划分,方便后期镜像比较多的时候进行分类)

[root@docker201.yinzhengjie.org.cn ~]# mkdir /yinzhengjie/softwares/dockerfile/{web/{apache,nginx,tomcat,jdk},system/{centos,ubantu,redhat,suse,debain}} -pv
mkdir: created directory ‘/yinzhengjie’
mkdir: created directory ‘/yinzhengjie/softwares’
mkdir: created directory ‘/yinzhengjie/softwares/dockerfile’
mkdir: created directory ‘/yinzhengjie/softwares/dockerfile/web’
mkdir: created directory ‘/yinzhengjie/softwares/dockerfile/web/apache’
mkdir: created directory ‘/yinzhengjie/softwares/dockerfile/web/nginx’
mkdir: created directory ‘/yinzhengjie/softwares/dockerfile/web/tomcat’
mkdir: created directory ‘/yinzhengjie/softwares/dockerfile/web/jdk’
mkdir: created directory ‘/yinzhengjie/softwares/dockerfile/system’
mkdir: created directory ‘/yinzhengjie/softwares/dockerfile/system/centos’
mkdir: created directory ‘/yinzhengjie/softwares/dockerfile/system/ubantu’
mkdir: created directory ‘/yinzhengjie/softwares/dockerfile/system/redhat’
mkdir: created directory ‘/yinzhengjie/softwares/dockerfile/system/suse’
mkdir: created directory ‘/yinzhengjie/softwares/dockerfile/system/debain’
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]#

二.准备源码包与配置文件

1>.模拟打包生产环境代码

[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# ll
total 20
-rwxr-xr-x. 1 root root  462 Jan 18 20:25 docker-build.sh
-rw-r--r--. 1 root root 1758 Jan 19 01:38 Dockerfile
-rw-r--r--. 1 root root   45 Jan 18 19:54 index2020.html
-rw-r--r--. 1 root root   40 Jan 18 17:05 index.html
-rw-r--r--. 1 root root 1711 Jan 19 01:55 nginx.conf
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# 
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# 
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# cat index.html 
<h1>YinZhengjie's Nginx Web Server</h1>
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# 
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# cat index2020.html 
<h1>YinZhengjie's Nginx Web Server 2020</h1>
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# 
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# tar zcvf code.tar.gz index.html index2020.html 
index.html
index2020.html
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# 
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# ll
total 24
-rw-r--r--. 1 root root  199 Jan 19 02:06 code.tar.gz
-rwxr-xr-x. 1 root root  462 Jan 18 20:25 docker-build.sh
-rw-r--r--. 1 root root 1758 Jan 19 01:38 Dockerfile
-rw-r--r--. 1 root root   45 Jan 18 19:54 index2020.html
-rw-r--r--. 1 root root   40 Jan 18 17:05 index.html
-rw-r--r--. 1 root root 1711 Jan 19 01:55 nginx.conf
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# 
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]#

2>.编写nginx的配置文件

[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# cat nginx.conf 
user nginx;

worker_processes auto;

error_log /var/log/nginx/error.log;

pid /run/nginx.pid;

#Docker最终运行Nginx建议大家把后台进程关闭,默认是"on".
daemon off;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    #自定义Nginx的日志格式
    log_format my_access_json '{"@timestamp":"$time_iso8601",' 
                               '"host":"$server_addr",' 
                          '"clientip":"$remote_addr",' 
                           '"size":$body_bytes_sent,' 
                          '"responsetime":$request_time,' 
                          '"upstreamtime":"$upstream_response_time",' 
                          '"upstreamhost":"$upstream_addr",' 
                          '"http_host":"$host",' 
                          '"uri":"$uri",' 
                          '"domain":"$host",' 
                          '"xff":"$http_x_forwarded_for",' 
                          '"referer":"$http_referer",' 
                          '"tcp_xff":"$proxy_protocol_addr",' 
                          '"http_user_agent":"$http_user_agent",' 
                          '"status":"$status"}';

    access_log /var/log/nginx/access_json.log my_access_json;

    sendfile            on;
    keepalive_timeout   65;
    include       mime.types;
    default_type  text/html;
    charset utf-8;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        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 {
        }
    }
}
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]#

三.编写Dockerfile**

[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# cat Dockerfile 
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-25
#Blog:             http://www.cnblogs.com/yinzhengjie
#Description:        YinZhengjie's Nginx Dockerfile
#Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
#********************************************************************

#第一行先定义基础镜像,表示当前镜像文件是基于哪个进行编辑的.
FROM centos:centos7.6.1810

#指定镜像维护者的信息.
MAINTAINER Jason.Yin y1053419035@qq.com

#定义执行的命令,将安装nginx的步骤执行一遍。建议大家把多条RUN命令使用"&&"符号写成一行,这样可以减少镜像的层数(Layer),
#类似于这样的安装命令(或者经常改动相对较小的命令)应该尽量往前写,这样在多次编译时就不会重复执行了(因为默认会使用缓存),从而提升编译效率.
RUN yum -y install epel-release && yum -y install nginx && yum -y install net-tools vim wget pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop

#将nginx的配置文件放在镜像的指定目录"/etc/nginx/"
ADD nginx.conf /etc/nginx/

#如果"code.tar.gz"是压缩文件则自动解压压缩包,并将解压的结果放在镜像的指定目录"/usr/share/nginx/html".
ADD code.tar.gz /usr/share/nginx/html

#定义向外暴露的端口号,多个端口用空格做间隔,启动容器的时候"-p"需要使用此端向外端映射.
EXPOSE 80/tcp 443/tcp

#定义前台运行的命令,每个Docker只能有一条,如果定义了多条"CMD"指令那么最后一条CMD指令会覆盖之前的(即只有最后一条CMD被执行).
CMD ["nginx"]

[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# 
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]#

四.执行镜像构建

1>.自定义简单的构建镜像的脚本(我这里抛砖引玉,你可以自行扩充)

[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# cat docker-build.sh 
#!/bin/bash
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2020-01-18
#FileName:        docker-build.sh
#URL:             http://www.cnblogs.com/yinzhengjie
#Description:        The test script
#Copyright (C):     2020 All rights reserved
#********************************************************************

TAG=$1

docker image build -t nginx:${TAG} ./

[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# 
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]#

2>.使用自定义的脚本构建镜像

[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# docker image ls
REPOSITORY            TAG                  IMAGE ID            CREATED             SIZE
nginx                 v0.2-20200118-1750   ef094745c27f        16 minutes ago      448MB
jason/centos7-nginx   v0.1.20200114        7372d16c99bc        31 hours ago        448MB
jason/centos7-nginx   latest               c4e0980a825a        32 hours ago        448MB
mysql                 5.6.44               c30095c52827        6 months ago        256MB
centos                centos7.6.1810       f1cb7c7d58b7        10 months ago       202MB
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# 
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# ./docker-build.sh v0.2-20200118-1750        #不难发现,当前的镜像版本号已经存在啦~执行后之前的镜像文件的版本号将被重置为"<none>"
Sending build context to Docker daemon  10.24kB
Step 1/7 : FROM centos:centos7.6.1810
 ---> f1cb7c7d58b7
Step 2/7 : MAINTAINER Jason.Yin y1053419035@qq.com
 ---> Using cache
 ---> edd0bcfc5908
Step 3/7 : RUN yum -y install epel-release && yum -y install nginx && yum -y install net-tools vim wget pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
 ---> Using cache
 ---> 779cda1b20a1
Step 4/7 : ADD nginx.conf /etc/nginx/
 ---> adaaeace9a08
Step 5/7 : ADD code.tar.gz /usr/share/nginx/html
 ---> 415004a0dc4d
Step 6/7 : EXPOSE 80/tcp 443/tcp
 ---> Running in 4e9071f1c399
Removing intermediate container 4e9071f1c399
 ---> 5c91af75787c
Step 7/7 : CMD ["nginx"]
 ---> Running in c0cbd8d86a50
Removing intermediate container c0cbd8d86a50
 ---> abb7704b09d7
Successfully built abb7704b09d7
Successfully tagged nginx:v0.2-20200118-1750
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# 
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# docker image ls
REPOSITORY            TAG                  IMAGE ID            CREATED             SIZE
nginx                 v0.2-20200118-1750   abb7704b09d7        2 seconds ago       448MB
<none>                <none>               ef094745c27f        17 minutes ago      448MB
jason/centos7-nginx   v0.1.20200114        7372d16c99bc        31 hours ago        448MB
jason/centos7-nginx   latest               c4e0980a825a        32 hours ago        448MB
mysql                 5.6.44               c30095c52827        6 months ago        256MB
centos                centos7.6.1810       f1cb7c7d58b7        10 months ago       202MB
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]# 
[root@docker201.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/nginx]#

五.使用咱们自己的镜像测试配置是否生效

1>.基于咱们自制的镜像启动容器需要做端口映射

[root@docker201.yinzhengjie.org.cn ~]# docker container  ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
d1197bf0e557        mysql:5.6.44        "docker-entrypoint.s…"   4 hours ago         Up 4 hours          0.0.0.0:3306->3306/tcp   vibrant_rosalind
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker image ls
REPOSITORY            TAG                  IMAGE ID            CREATED             SIZE
nginx                 v0.2-20200118-1750   abb7704b09d7        8 minutes ago       448MB
<none>                <none>               ef094745c27f        25 minutes ago      448MB
jason/centos7-nginx   v0.1.20200114        7372d16c99bc        32 hours ago        448MB
jason/centos7-nginx   latest               c4e0980a825a        32 hours ago        448MB
mysql                 5.6.44               c30095c52827        6 months ago        256MB
centos                centos7.6.1810       f1cb7c7d58b7        10 months ago       202MB
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker run -it --rm -p 80:80 --name myNginx nginx:v0.2-20200118-1750             #启动成功后会在当前终端阻塞。

2>.使用exec命令连接新生成的容器并查看容器内部的数据是否正确

[root@docker201.yinzhengjie.org.cn ~]# docker container exec -it myNginx bash
[root@2902fadc453a /]# 
[root@2902fadc453a /]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 7  bytes 586 (586.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@2902fadc453a /]# 
[root@2902fadc453a /]# cat /etc/nginx/nginx.conf
user nginx;

worker_processes auto;

error_log /var/log/nginx/error.log;

pid /run/nginx.pid;

#Docker最终运行Nginx建议大家把后台进程关闭,默认是"on".
daemon off;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    #自定义Nginx的日志格式
    log_format my_access_json '{"@timestamp":"$time_iso8601",' 
                               '"host":"$server_addr",' 
                   '"clientip":"$remote_addr",' 
                   '"size":$body_bytes_sent,' 
                   '"responsetime":$request_time,' 
                   '"upstreamtime":"$upstream_response_time",' 
                   '"upstreamhost":"$upstream_addr",' 
                   '"http_host":"$host",' 
                   '"uri":"$uri",' 
                   '"domain":"$host",' 
                   '"xff":"$http_x_forwarded_for",' 
                   '"referer":"$http_referer",' 
                   '"tcp_xff":"$proxy_protocol_addr",' 
                   '"http_user_agent":"$http_user_agent",' 
                   '"status":"$status"}';

    access_log /var/log/nginx/access_json.log my_access_json;

    sendfile            on;
    keepalive_timeout   65;
    include       mime.types;
    default_type  text/html;
    charset utf-8;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        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 {
        }
    }
}
[root@2902fadc453a /]# 
[root@2902fadc453a /]# ss -ntl
State      Recv-Q Send-Q                                                                                          Local Address:Port                                                                                                         Peer Address:Port              
LISTEN     0      128                                                                                                         *:80                                                                                                                      *:*                  
LISTEN     0      128                                                                                                      [::]:80                                                                                                                   [::]:*                  
[root@2902fadc453a /]# 
[root@2902fadc453a /]# cd /var/log/nginx/
[root@2902fadc453a nginx]# 
[root@2902fadc453a nginx]# ll
total 0
-rw-r--r--. 1 root root 0 Jan 18 18:23 access_json.log
-rw-r--r--. 1 root root 0 Jan 18 18:23 error.log
[root@2902fadc453a nginx]# 
[root@2902fadc453a nginx]#

3>.使用客户端访问宿主机的映射端口,验证咱们自己nginx的镜像

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
13天前
|
应用服务中间件 nginx Docker
Docker镜像-基于DockerFile制作编译版nginx镜像
这篇文章介绍了如何基于Dockerfile制作一个编译版的nginx镜像,并提供了详细的步骤和命令。
91 17
Docker镜像-基于DockerFile制作编译版nginx镜像
|
12天前
|
NoSQL 关系型数据库 Redis
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
|
12天前
|
Docker 容器
Docker Hub镜像公共仓库使用
这篇文章介绍了如何使用Docker Hub公共仓库进行镜像的创建、上传、下载和管理。
204 8
|
27天前
|
Docker 容器
在Docker中,Dockerfile有哪些常见指令?
在Docker中,Dockerfile有哪些常见指令?
|
4月前
|
Linux Docker 容器
Docker Dockerfile 语法与指令
Docker Dockerfile 语法与指令
52 0
|
Linux Docker 容器
Docker Dockerfile 语法与指令
Docker Dockerfile 语法与指令
130 0
|
应用服务中间件 Linux Shell
Docker进阶 dockerfile指令构建docker镜像
Docker进阶 Dockerfile指令,编译dockerfile文件构建镜像,dockerfile常用指令,实操训练:Dockerfile构建Nginx镜像: FROM #指定基础镜像,一切从这里开始构建 MAINTAINER #镜像是谁写的,姓名+邮箱 RUN #镜像构建的时候需要运行的命令 ADD #步骤:tomcat镜像,这个tomcat压缩包!添加内容 WORKDIR #镜像的工作目录 VOLUME #挂载的目录 EXPOST #暴露端口配置
Docker进阶 dockerfile指令构建docker镜像
|
Java 应用服务中间件 Shell
【Docker】四 Dockerfile指令详解
需求 - 启动一个Nginx容器。 - 将Nginx容器的首⻚改为 `Welcome to my first docker class` 。 - 将容器保存下来。
181 0
|
存储 Shell 应用服务中间件
学习Docker就应该掌握的dockerfile语法与指令
在日常的工作中,常常需要制作自己的项目的镜像,一般通过以下两种方式制作镜像:Docker commit、Dockerfile。 ## Docker commit Docker commit一般用做从一个运行状态的容器来创建一个新的镜像。定制镜像应该使用Dockerfile来完成。 ``` docker commit 容器名 新镜像名:tag ``` 使用这种方式的缺点是:1.对外不
|
存储 JSON 监控
纯干货!Docker Dockerfile指令大全(下)
什么是 Dockerfile? Dockerfile 是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。使用docker build命令,用户可以创建基于基础镜像的自定义镜像。