Docker镜像-基于DockerFile制作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 lsREPOSITORY TAG IMAGE ID CREATED SIZE

[root@docker201.yinzhengjie.org.cn~]#

[root@docker201.yinzhengjie.org.cn~]# docker image pull centos:centos7.6.1810centos7.6.1810: Pulling from library/centos

ac9208207ada: Pull complete

Digest: sha256:62d9e1c2daa91166139b51577fe4f4f6b4cc41a3a2c7fc36bd895e2a17a3e4e6

Status: Downloaded newer imagefor centos:centos7.6.1810[root@docker201.yinzhengjie.org.cn~]#

[root@docker201.yinzhengjie.org.cn~]# docker image lsREPOSITORY TAG IMAGE ID CREATED SIZE

centos centos7.6.1810 f1cb7c7d58b7 10months 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}} -pvmkdir: 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

total20

-rwxr-xr-x. 1 root root 462 Jan 18 20:25 docker-build.sh

-rw-r--r--. 1 root root 1758 Jan 19 01:38Dockerfile-rw-r--r--. 1 root root 45 Jan 18 19:54index2020.html-rw-r--r--. 1 root root 40 Jan 18 17:05 index.html

-rw-r--r--. 1 root root 1711 Jan 19 01:55nginx.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]# catindex.html

YinZhengjie's Nginx Web Server

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

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

YinZhengjie's Nginx Web Server 2020

[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

total24

-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:38Dockerfile-rw-r--r--. 1 root root 45 Jan 18 19:54index2020.html-rw-r--r--. 1 root root 40 Jan 18 17:05index.html-rw-r--r--. 1 root root 1711 Jan 19 01:55nginx.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]# catnginx.confuser 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]# catDockerfile

#********************************************************************#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),

#类似于这样的安装命令(或者经常改动相对较小的命令)应该尽量往前写,这样在多次编译时就不会重复执行了(因为默认会使用缓存),从而提升编译效率.

RUNyum -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"需要使用此端向外端映射.

EXPOSE80/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):2020All rights reserved

#********************************************************************TAG=$1docker 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 lsREPOSITORY TAG IMAGE ID CREATED SIZE

nginx v0.2-20200118-1750 ef094745c27f 16minutes ago 448MB

jason/centos7-nginx v0.1.20200114 7372d16c99bc 31hours ago 448MB

jason/centos7-nginx latest c4e0980a825a 32hours ago 448MB

mysql5.6.44 c30095c52827 6months ago 256MB

centos centos7.6.1810 f1cb7c7d58b7 10months 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        #不难发现,当前的镜像版本号已经存在啦~执行后之前的镜像文件的版本号将被重置为""Sending build context to Docker daemon10.24kB

Step1/7 : FROM centos:centos7.6.1810

--->f1cb7c7d58b7

Step2/7: MAINTAINER Jason.Yin y1053419035@qq.com--->Using cache--->edd0bcfc5908

Step3/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

Step4/7 : ADD nginx.conf /etc/nginx/

--->adaaeace9a08

Step5/7 : ADD code.tar.gz /usr/share/nginx/html--->415004a0dc4d

Step6/7 : EXPOSE 80/tcp 443/tcp---> Running in4e9071f1c399

Removing intermediate container 4e9071f1c399--->5c91af75787c

Step7/7 : CMD ["nginx"]---> Running inc0cbd8d86a50

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 lsREPOSITORY TAG IMAGE ID CREATED SIZE

nginx v0.2-20200118-1750 abb7704b09d7 2 seconds ago 448MB

ef094745c27f 17minutes ago 448MB

jason/centos7-nginx v0.1.20200114 7372d16c99bc 31hours ago 448MB

jason/centos7-nginx latest c4e0980a825a 32hours ago 448MB

mysql5.6.44 c30095c52827 6months ago 256MB

centos centos7.6.1810 f1cb7c7d58b7 10months 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 lsREPOSITORY TAG IMAGE ID CREATED SIZE

nginx v0.2-20200118-1750 abb7704b09d7 8minutes ago 448MB ef094745c27f 25minutes ago 448MB

jason/centos7-nginx v0.1.20200114 7372d16c99bc 32hours ago 448MB

jason/centos7-nginx latest c4e0980a825a 32hours ago 448MB

mysql5.6.44 c30095c52827 6months ago 256MB

centos centos7.6.1810 f1cb7c7d58b7 10months 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 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 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的镜像

dockerfile安装yum_Docker镜像-基于DockerFile制作yum版nginx镜像相关推荐

  1. 破解elasticsearch(制作破解版docker镜像)

    破解elasticsearch(制作破解版docker镜像) 本文仅用于学习交流,要使用elasticsearch的付费功能,请通过正规渠道购买platinum(白金)版. 一.部署纯净版elasti ...

  2. Linux制作绿色版nginx

    在docker化的部署方式中,时常需要一个轻量化的nginx系统,主要用于实现动静分离,实现路由转发,或部署静态资源(可类比web容器). 轻量化的nginx,可以与静态资源一起,或接口服务一起,方便 ...

  3. dockerfile安装oracle,docker通过Dockerfile安装oracle-12c数据库

    1. 克隆oracle的docker-imges仓库 git clone https://github.com/oracle/docker-images.git 2.从oracle官网下载orcale ...

  4. 【Docker制作镜像】Linux制作以python为基础镜像,融合Flask框架和Redis数据库

    步骤 1.编辑Dockerfile 2.编辑requirements.txt文件 3.编辑app.py文件 4.生成镜像文件 5.使用镜像,启动容器 6.访问容器的web服务 7.启动redis容器 ...

  5. Docker 制作Nginx镜像

    系列文章目录 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 例如:第一章 Python 机器学习入门之pandas的使用 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮 ...

  6. Docker学习之路04:创建定制Nginx镜像

    Docker学习之路04:创建定制Nginx镜像 Docker学习路线传送门: Docker学习之路01:Docker的安装 Docker学习之路02:阿里云镜像加速器 Docker学习之路03:Do ...

  7. 风林火山win11 64位官方版iso镜像v2021.07

    风林火山win11 64位官方版iso镜像v2021.07是一款精心设计过的优秀装机系统,支持多种安装方式,系统中的服务项.注册表等,都是经过全面优化,能够很好的兼容各种功能,系统能够专业化的匹配专业 ...

  8. 基于Dockerfile制作镜像

    <基于Dockerfile制作镜像> 目录... 2 一.Dockerfile简介及书写规则... 3 1.FROM(指定基础image)... 3 2.MAINTAINER(用来指定镜像 ...

  9. Docker 3.2.5:基于 Dockerfile 制作 Nginx 镜像

    3.2.5:基于 Dockerfile 制作 Nginx 镜像(编译版) 准备基础镜像 使用制作好的 centos-init 作为基础镜像: 编写 Dockerfile 进入相应的 Dockerfil ...

最新文章

  1. linux共享文件丢失,【干货分享】linux平台下数据文件被误删后,如何及时得知并进行恢复...
  2. Python 技术篇-用PIL库旋转图片、改变图像尺寸
  3. linux文件指令 例子,Linux 命令:文件目录操作与实例
  4. C++中transform的用法
  5. windows服务器下的ftp server搭建
  6. SQL Tree解法
  7. 乌班图配置mysql Java_Ubuntu准备+MySQL+Java
  8. linux怎么随机按行打散文件,linux shell 将文件按照行数以及顺序拆分成多个文件...
  9. arcpy 实现列举目录下的要素类与描述矢量数据要素类
  10. MySQL-快速入门(1)基本数据库、表操作语句
  11. 关于邮件模板的兼容性1
  12. DB2 create database报错SQL1036C,Could not init GLFH lock file
  13. [Android工具]音乐下载软件,MP3音乐无损音乐下载器
  14. 《Miss Talk》第04期:对话凯叔讲故事 曲艳颂
  15. 数据库设计(1)_概念结构设计
  16. zookeeper的重连思考
  17. 《C++标准库》学习笔记 — STL —流
  18. 论文精读|VRCNet:变分关联点云补全网络(CVPR2021)
  19. 前端:页面内容不够,始终把footer固定在底部
  20. 【ES6闯关】Promise堪比原生的自定义封装then、catch、resolve、reject...

热门文章

  1. 学生出勤率平时成绩java_《javaweb应用开发》课程标准.doc
  2. QPainter使用整理
  3. Linux性能监控命令——sar
  4. Gulp和webpack的区别
  5. 解决启动flanneld失败的方法
  6. IIS 7 及以上 IIS错误页“编辑功能设置...”提示“锁定冲突”
  7. bindService执行成功后,低概率出现onServiceConnected没有被调用
  8. 程序员每天到底可以写几行代码?
  9. Response.Write()方法响应导致页面内容变形的问题
  10. 常用正则表达式总结(js与C#对照)