Nginx是一个高性能的Web和反向代理服务器,它具有很多非常优越的特性:
1、作为Web服务器。
2、作为负载均衡服务器。
3、作为邮件代理服务器。
4、安装及配置简单。
接下来我们介绍在docker构建nginx镜像:
Docker镜像构建分为两种方式:

  1. 手动构建
  2. Dockerfile(自动构建)

一、Docker镜像手动构建实例

基于centos镜像进行构建nginx镜像
#下载镜像
[root@compute ~]# docker pull centos
[root@compute ~]# docker pull nginx
[root@compute ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest ff426288ea90 13 days ago 207.2 MB
docker.io/nginx latest 3f8a4339aadd 3 weeks ago 108.5 MB
#先创建centos镜像
[root@compute ~]# docker run --name nginxdocker -ti centos
[root@10859f0ffd78 /]# yum install -y wget
[root@10859f0ffd78 /]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
#安装nginx
[root@10859f0ffd78 /]# yum install -y nginx
[root@10859f0ffd78 /]# sed -i '3a daemon off;' /etc/nginx/nginx.conf
[root@10859f0ffd78 /]# exit
[root@compute ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
10859f0ffd78 centos "/bin/bash" 7 minutes ago Exited (0) 19 seconds ago nginxdocker
#构建镜像
[root@compute ~]# docker commit -m "test Nginx" 10859f0ffd78 nginxdocker/nginxdocker:v1
sha256:616e9d624b9a1db8e23491db331228ca56dd60c04356da14ab6d7e4cf821d415
You have new mail in /var/spool/mail/root
[root@compute ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginxdocker/nginxdocker v1 616e9d624b9a 10 seconds ago 383.9 MB
#启动容器
[root@compute ~]# docker run --name nginxserver -d -p 82:80 nginxdocker/nginxdocker:v1 nginx
c2ded9ce8af76c3b4862ca54478d39429879c856a2751957c9287df077dfcf17
[root@compute ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2ded9ce8af7 nginxdocker/nginxdocker:v1 "nginx" 21 seconds ago Up 19 seconds 0.0.0.0:82->80/tcp nginxserver
#测试
[root@compute ~]# curl -I 192.168.128.165:82
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Mon, 22 Jan 2018 08:15:39 GMT
Content-Type: text/html
Content-Length: 3700
Last-Modified: Wed, 18 Oct 2017 08:08:18 GMT
Connection: keep-alive
ETag: "59e70bf2-e74"
Accept-Ranges: bytes

二、Docker镜像自动构建实例

Dockerfile是一个文本格式的配置文件,用户可以使用Dockerfile快速创建自定义的镜像。
Dockerfile由一行行命令语句组成,#号注释。分为:基础镜像信息、维护者信息、镜像操作指令和容器启动时执行指令。

#创建目录
[root@compute ~]# mkdir /dokerfile
[root@compute ~]# cd /dokerfile/
[root@compute dokerfile]# mkdir nginx
[root@compute dokerfile]# cd nginx/
#添加Dockerfile文件注意D大写
[root@compute nginx]# vim Dockerfile
#This dockerfile uses the centos image
#VERSION 2 - EDITION 1
#Author:jianlaihe
#Command format:
# 指定基于的基础镜像
FROM centos #维护者信息
MAINTAINER jianlaihe hejianlai@dnion.com #镜像的操作指令
RUN yum install -y wget
RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/re
po/epel-7.repo
RUN yum install -y nginx
RUN echo "daemon off;" >>/etc/nginx/nginx.conf
#添加文件需存在当前目录下
ADD index.html /usr/share/nginx/html/index.html
EXPOSE 80
#容器启动时执行命令
CMD /usr/sbin/nginx [root@compute nginx]# echo "hello docker" >index.html
#docker build命令构建镜像名为nginx版本v2
[root@compute nginx]# docker build -t nginx:v2 .
Complete!
---> 5e37422db8b2
Removing intermediate container 87fa82c1173a
Step 6 : RUN echo "daemon off;" >>/etc/nginx/nginx.conf
---> Running in a03da9a96436
---> 236590c11b39
Removing intermediate container a03da9a96436
Step 7 : ADD index.html /usr/share/nginx/html/index.html
---> ac28fc354cad
Removing intermediate container 5c2d9944e6f3
Step 8 : EXPOSE 80
---> Running in 0b598a0680b8
---> d4addb2c20ba
Removing intermediate container 0b598a0680b8
Step 9 : CMD /usr/sbin/nginx
---> Running in d1feaede849f
---> 4905d9869aa7
Removing intermediate container d1feaede849f
Successfully built 4905d9869aa7
[root@compute nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v2 4905d9869aa7 53 seconds ago 404.1 MB
#启动容器
[root@compute nginx]# docker run --name testnginx -d -p 83:80 nginx:v2
b2cd72936465464bb8a88e9b3c5df0015241c7d17cb74062433ef79582c58908
#测试
[root@compute nginx]# curl 192.168.128.165:83
hello docker

Docker学习之4——构建NGINX镜像的更多相关文章

  1. 用Docker构建Nginx镜像

    1构建Nginx镜像 1建立工作目录 [root@localhost ]# mkdir 1nginx [root@localhost 1nginx]# cd 1nginx/ [root@localho ...

  2. 使用dockerfile构建nginx镜像

    使用dockerfile构建nginx镜像 docker构建镜像的方法:   commit.dockerfile 1.使用commit来构建镜像: commit是基于原有镜像基础上构建的镜像,使用此方 ...

  3. 使用dockerfile构建nginx镜像 转

      docker构建镜像的方法:   commit.dockerfile 1.使用commit来构建镜像: commit是基于原有镜像基础上构建的镜像,使用此方法构建镜像的目的:保存镜像里的一些配置信 ...

  4. Dockerfile构建nginx镜像

    Dockerfile构建nginx镜像 [root@bogon ~]# mkdir /opt/docker-file [root@bogon ~]# cd /opt/docker-file/ [roo ...

  5. docker学习笔记(3)- 镜像

    简介 在docker学习笔记(1)- 架构概述一节中可以看到镜像是docker三大组件之一,可以将Docker镜像类比为虚拟机的模版. 镜像由多个层组成,每层叠加之后从外部看就像一个独立的对象,镜像的 ...

  6. Docker学习总结(二)—— 镜像,容器

    1.Docker镜像  1.1相关概念:registry :用于保存Docker镜像,包括镜像层次结构和镜像元数据,类似于git仓库之类的实体. repository:某个Docker镜像所有迭代版本 ...

  7. docker学习7-Dockerfile制作自己的镜像文件

    前言 如果你是一个python自动化测试人员,某天你在公司终于完成了一个项目的接口自动化脚本工作,在你自己常用的本机或者服务器上调试完成了脚本,稳稳地没问题. 可是晚上下班回家,你自己找了个linux ...

  8. Docker 学习之部署php + nginx(一)

    博主电脑系统是window 10 专业版的,所以在此记录下docker的基本使用方法. 参考地址: https://www.runoob.com/docker/docker-install-php.h ...

  9. Docker学习笔记 - 创建私有的镜像仓库

    一.查找镜像仓库 https://hub.docker.com/ 二.下载镜像仓库 docker pull registry:2.6.2 三.安装镜像仓库 docker run -d -p 6000: ...

随机推荐

  1. odoo研究学习:刷新本地模块列表都干了什么事?

    模块信息存储在ir.module.module 数据表中 平时在开发过程中经常会刷新本地模块列表,例如:新增了模块.更新了模块基础信息.更换了模块图标等等,在点击‘更新’按钮的时候odoo平台到底干了 ...

  2. Swift 反射机制,命名空间

    1. 知道 Swift 中有命名空间        - 在同一命名空间下,全局共享!        - 第三方框架使用 Swift 如果直接拖拽到项目中,从属同一个命名空间,很有可能冲突!       ...

  3. PHP-自定义数组-预定义数组-自定义函数-预定义函数

    (1)自定义数组 —— 项目中的重点 (2)PHP预定义数组 —— 重点&难点 (3)自定义函数 —— 了解 (4)PHP预定义函数 —— 项目中的重点 1.自定义数组 数组:array,一个 ...

  4. spring-boot json数据交互

    SpringBoot学习之Json数据交互 最近在弄监控主机项目,对javaweb又再努力学习.实际的项目场景中,前后分离几乎是所以项目的标配,全栈的时代的逐渐远去,后端负责业务逻辑处理,前端负责数据 ...

  5. Linux学习---条件预处理的应用

    预处理的使用: ⑴包含头文件 #include ⑵宏定义 #define    替换,不进行语法检查 ①常量宏定义:#define 宏名 (宏体) (加括号为防止不进行语法检查而出现的错误) eg:# ...

  6. C++获取工程路径、exe路径

    编码过程中有时候会用到获取工程所在路径或者exe所在的路径信息,这里稍微记录下. 获取工程路径 char pBuf[MAX_PATH]; //存放路径的变量 GetCurrentDirectory(M ...

  7. C++与C语言在结构体上的区别

    用Nios 实现逻辑上很清楚,只是C++用switch语句后,写的很麻烦,主要是Switch语句很长吧. 另外要记录下:struct在C++中,在a文件中定义在b文件中定义变量是可以的,但在C语言中, ...

  8. Wordpress“固定链接”页面出现404原因及解决方法

    编辑配置文件:/etc/apache2/apache2.conf(非常靠后的位置),将里面的AllowOverride选项由None设置为All. <Directory /> Option ...

  9. 《mysql必知必会》学习_第14章_20180806_欢

    第14章:使用子查询. 子查询是镶嵌在其他查询里面,相当其他的select查询的条件来. P91 select order_num from where prod_id='tnt2';   #检索条件 ...

  10. Swift学习目录

    本学习基于苹果官方Swift学习材料,保留了原版90%左右的内容(一些项目开发中基本不用的知识点没有整理),并根据理解进行整理.如对原版感兴趣,可以直接单击链接阅读和学习. 第一部分 基础篇 1.基本 ...