Docker學習之4——構建NGINX鏡像


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM