docker之NGINX镜像构建
Nginx是一个高性能的Web和反向代理服务器,它具有很多非常优越的特性:
1、作为Web服务器。
2、作为负载均衡服务器。
3、作为邮件代理服务器。
4、安装及配置简单。
接下来我们介绍在docker构建nginx镜像:
Docker镜像构建分为两种方式:
- 手动构建
- 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之NGINX镜像构建的更多相关文章
- Docker namespace,cgroup,镜像构建,数据持久化及Harbor安装、高可用配置
1.Docker namespace 1.1 namespace介绍 namespace是Linux提供的用于分离进程树.网络接口.挂载点以及进程间通信等资源的方法.可以使运行在同一台机器上的不同服务 ...
- 7.云原生之Docker容器Dockerfile镜像构建浅析与实践
转载自:https://www.bilibili.com/read/cv15220707/?from=readlist Dockerfile 镜像构建浅析与实践 描述:Dockerfile是一个文本格 ...
- docker创建nginx镜像
注意:此处不是用的dockerfile创建的镜像,只是用来搞一搞 首先你的系统里面要安装docker,这里就不重复介绍了,可以看之前的文章: 然后再搞一个基础镜像 docker pull regist ...
- Docker容器 关于镜像构建的安全问题
写在前面 确保容器中服务与应用安全是容器化演进的关键点.容器安全涉及到应用开发与维护的整个生命周期,本文主要从镜像构建的视角来看docker容器的一些安全问题及应对措施. 一.权限管理 1.避免以容器 ...
- Docker 制作Nginx镜像
参考文章:https://www.jianshu.com/p/dc4cd0547d1e 镜像的制作方式有两种,一种是下载别人的镜像之后再制作成自己的镜像,一种是从头开始制作自己的镜像 第一种,下载别人 ...
- Docker教程:镜像构建和自动镜像构建dockerfile
http://blog.csdn.net/pipisorry/article/details/50805379 Docker透过Dockerfile来记录建立Container映象文件的每一个步骤,可 ...
- docker学习之路-nginx镜像(翻译)
本篇来自https://hub.docker.com/_/nginx/?tab=description 它是docker hub上nginx的官方网站,上面有关于nginx的使用描述等.从这里你可以找 ...
- Docker 拉取Nginx镜像 和运行
Docker 镜像拉取 docker pull [OPTIONS] NAME[:TAG|@DIGEST] 镜像拉取命令 OPTIONS说明: -a :拉取所有 tagged 镜像 --disable- ...
- Docker——理解好镜像和容器的关系
关注公众号,大家可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料. 镜像也是 docker 的核心组件之一,镜像时容器运行的基础,容器是镜像运行后的形态.前面我们介绍了 ...
随机推荐
- JDK源码 - ArrayList
/** * ArrayList源码分析 * @author liyong * */ public class Util { @SuppressWarnings("unchecked" ...
- Linux各种版本
1.1 Fedora Core和Fedora 一开始总搞不清楚 Fedora Core和Fedora有什么关系?有什么不同,现在终于明白了,自从Fedora Core 第七版开始以后就开始称作F ...
- H5+Ajax+WebApi实现文件下载(进度条,多文件)
前言 踩过的坑 1.WebAPI跨域 2.Jquery ajax低版本不支持XHR 2功能 3.Jquery ajax不支持Deferred的process事件 4.IE下文件名乱码问题 功能实现 & ...
- Java与算法之(1) - 冒泡排序
冒泡排序法的原理是,每次比较相邻的两个元素,如果它们的顺序错误就把它们交换过来. 例如对4 3 6 2 7 1 5这7个数字进行从小到大的排序,从最左侧开始,首先比较4和3 因为是从小到大排序,4和3 ...
- windows下apache配置虚拟主机
因为有多个laravel项目需要配置根目录到public下面,所以要配置多个虚拟主机 方法一:添加端口号 第一步:进入apache的目录 Apache24\conf 找到 httpd.conf 文件, ...
- Codeforces Round #345(Div. 2)-651A.水题 651B.。。。 651C.去重操作 真是让人头大
A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- JS URI Encode
javascript中存在几种对URL字符串进行编码的方法:escape/encodeURI/encodeURIComponent.这几种编码所起的作用各不相同. escape 采用ISO Latin ...
- Oracle_子查询
Oracle_子查询 子查询 --如何查得所有比"CLARK"工资高的员工的信息 select ename, sal from emp where ename = 'CLARK ...
- 一篇文章让你深透理解cookie和session,附带分布式WEB系统redis共享session方案
cookie和session有什么区别?这是一个很基础的知识点,大家可能都知道一个大概:cookie是存在客户端的,session是存储在服务端,cookie和session用来验证识别用户的登录状态 ...
- Nginx负载均衡配置简单配置方法
http://www.jb51.net/article/121235.htm Nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至不同的Web服务器.下面通过 ...