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 的核心组件之一,镜像时容器运行的基础,容器是镜像运行后的形态.前面我们介绍了 ...
随机推荐
- I2C通讯协议
1.基本概念 主机 初始化发送,产生时钟信号和终止发送的器件 从机 被主机寻址的器件 发送器 发送数据到总线的器件 接收器 ...
- iframe兄弟间和iframe父子间的值传递问题
在网上查了资料.iframe的参数传递问题.有很多答案都是不可行的.现在将收集的资料整理一下.已经验证通过.以下如有问题请及时指正. 1. iframe兄弟之间值传递 举例说明:index页面中有两个 ...
- Generator函数语法解析
转载请注明出处: Generator函数语法解析 Generator函数是ES6提供的一种异步编程解决方案,语法与传统函数完全不同.以下会介绍一下Generator函数. 写下这篇文章的目的其实很简单 ...
- 【Java学习笔记之二十】final关键字在Java继承中的用法小结
谈到final关键字,想必很多人都不陌生,在使用匿名内部类的时候可能会经常用到final关键字.另外,Java中的String类就是一个final类,那么今天我们就来了解final这个关键字的用法. ...
- HDU 2682 Tree
题目: There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities A and ...
- 如何把域名解析到网站空间IP上?
建立网站首要就是要有一个域名和网站空间,怎么把这两者联系在一起呢?这就要通过域名解析,把域名指向空间的IP,让我们能够通过域名访问网站空间.通过域名解析把我们容易记住的域名转化成IP地址,由DNS服务 ...
- linux下yum命令出现Loaded plugins: fastestmirror
yum install的时候提示:Loaded plugins: fastestmirror fastestmirror是yum的一个加速插件,这里是插件提示信息是插件不能用了. 不能用就先别用呗,禁 ...
- 前端自动化构建工具Gulp简单入门
昨天听同事分享了Gulp的一些简单使用,决定自己也试一试. 一.安装 gulp是基于nodejs的,所以要先下载安装node(直接搜node,在官网下载就好了) 1.全局安装gulp npm inst ...
- 学习javascript数据结构(四)——树
前言 总括: 本文讲解了数据结构中的[树]的概念,尽可能通俗易懂的解释树这种数据结构的概念,使用javascript实现了树,如有纰漏,欢迎批评指正. 原文博客地址:学习javascript数据结构( ...
- Angular CLI: 发布到 GitHub Pages
发布 Angular 应用的简单方式是使用 GitHub Pages. 首先需要创建一个 GitHub 账号,随后,为您的项目创建一个仓库.记下 GitHub 中的用户名和项目名称. 例如,我的 Gi ...