Docker学习之4——构建NGINX镜像
发布日期:2021-05-08 23:26:15 浏览次数:19 分类:博客文章

本文共 4298 字,大约阅读时间需要 14 分钟。

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 imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEdocker.io/centos    latest              ff426288ea90        13 days ago         207.2 MBdocker.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 -aCONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                      PORTS                NAMES10859f0ffd78        centos               "/bin/bash"              7 minutes ago       Exited (0) 19 seconds ago                        nginxdocker#构建镜像[root@compute ~]# docker commit -m "test Nginx" 10859f0ffd78 nginxdocker/nginxdocker:v1sha256:616e9d624b9a1db8e23491db331228ca56dd60c04356da14ab6d7e4cf821d415You have new mail in /var/spool/mail/root[root@compute ~]# docker imagesREPOSITORY                TAG                 IMAGE ID            CREATED             SIZEnginxdocker/nginxdocker   v1                  616e9d624b9a        10 seconds ago      383.9 MB#启动容器[root@compute ~]# docker run --name nginxserver -d -p 82:80 nginxdocker/nginxdocker:v1 nginxc2ded9ce8af76c3b4862ca54478d39429879c856a2751957c9287df077dfcf17[root@compute ~]# docker psCONTAINER ID        IMAGE                        COMMAND             CREATED             STATUS              PORTS                NAMESc2ded9ce8af7        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:82HTTP/1.1 200 OKServer: nginx/1.12.2Date: Mon, 22 Jan 2018 08:15:39 GMTContent-Type: text/htmlContent-Length: 3700Last-Modified: Wed, 18 Oct 2017 08:08:18 GMTConnection: keep-aliveETag: "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/repo/epel-7.repoRUN yum install -y nginxRUN echo "daemon off;" >>/etc/nginx/nginx.conf#添加文件需存在当前目录下ADD index.html /usr/share/nginx/html/index.htmlEXPOSE 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! ---> 5e37422db8b2Removing intermediate container 87fa82c1173aStep 6 : RUN echo "daemon off;" >>/etc/nginx/nginx.conf ---> Running in a03da9a96436 ---> 236590c11b39Removing intermediate container a03da9a96436Step 7 : ADD index.html /usr/share/nginx/html/index.html ---> ac28fc354cadRemoving intermediate container 5c2d9944e6f3Step 8 : EXPOSE 80 ---> Running in 0b598a0680b8 ---> d4addb2c20baRemoving intermediate container 0b598a0680b8Step 9 : CMD /usr/sbin/nginx ---> Running in d1feaede849f ---> 4905d9869aa7Removing intermediate container d1feaede849fSuccessfully built 4905d9869aa7[root@compute nginx]# docker imagesREPOSITORY                TAG                 IMAGE ID            CREATED             SIZEnginx                     v2                  4905d9869aa7        53 seconds ago      404.1 MB#启动容器[root@compute nginx]# docker run --name testnginx -d -p 83:80 nginx:v2b2cd72936465464bb8a88e9b3c5df0015241c7d17cb74062433ef79582c58908#测试[root@compute nginx]# curl  192.168.128.165:83  hello docker

 

上一篇:Failed to get D-Bus connection: Operation not permitted解决
下一篇:shell获取字符串长度

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年05月10日 23时10分18秒