Docker学习之4——构建NGINX镜像
Nginx是一个高性能的Web和反向代理服务器,它具有很多非常优越的特性:
1、作为Web服务器。
2、作为负载均衡服务器。
3、作为邮件代理服务器。
4、安装及配置简单。
接下来我们介绍在docker构建nginx镜像:
Docker镜像构建分为两种方式:
- 手动构建
- Dockerfile(自动构建)
一、Docker镜像手动构建实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | 基于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 ff 426288 ea 90 13 days ago 207.2 MB docker.io/nginx latest 3 f 8 a 4339 aadd 3 weeks ago 108.5 MB #先创建centos镜像 [root@compute ~]# docker run --name nginxdocker -ti centos [root@ 10859 f 0 ffd 78 /]# yum install -y wget [root@ 10859 f 0 ffd 78 /]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel -7 .repo #安装nginx [root@ 10859 f 0 ffd 78 /]# yum install -y nginx [root@ 10859 f 0 ffd 78 /]# sed -i '3a daemon off;' /etc/nginx/nginx.conf [root@ 10859 f 0 ffd 78 /]# exit [root@compute ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 10859 f 0 ffd 78 centos "/bin/bash" 7 minutes ago Exited ( 0 ) 19 seconds ago nginxdocker #构建镜像 [root@compute ~]# docker commit -m "test Nginx" 10859 f 0 ffd 78 nginxdocker/nginxdocker:v 1 sha 256: 616 e 9 d 624 b 9 a 1 db 8 e 23491 db 331228 ca 56 dd 60 c 04356 da 14 ab 6 d 7 e 4 cf 821 d 415 You have new mail in /var/spool/mail/root [root@compute ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginxdocker/nginxdocker v 1 616 e 9 d 624 b 9 a 10 seconds ago 383.9 MB #启动容器 [root@compute ~]# docker run --name nginxserver -d -p 82: 80 nginxdocker/nginxdocker:v 1 nginx c 2 ded 9 ce 8 af 76 c 3 b 4862 ca 54478 d 39429879 c 856 a 2751957 c 9287 df 077 dfcf 17 [root@compute ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c 2 ded 9 ce 8 af 7 nginxdocker/nginxdocker:v 1 "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由一行行命令语句组成,#号注释。分为:基础镜像信息、维护者信息、镜像操作指令和容器启动时执行指令。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #创建目录 [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版本v 2 [root@compute nginx]# docker build -t nginx:v 2 . Complete! ---> 5 e 37422 db 8 b 2 Removing intermediate container 87 fa 82 c 1173 a Step 6 : RUN echo "daemon off;" >>/etc/nginx/nginx.conf ---> Running in a 03 da 9 a 96436 ---> 236590 c 11 b 39 Removing intermediate container a 03 da 9 a 96436 Step 7 : ADD index.html /usr/share/nginx/html/index.html ---> ac 28 fc 354 cad Removing intermediate container 5 c 2 d 9944 e 6 f 3 Step 8 : EXPOSE 80 ---> Running in 0 b 598 a 0680 b 8 ---> d 4 addb 2 c 20 ba Removing intermediate container 0 b 598 a 0680 b 8 Step 9 : CMD /usr/sbin/nginx ---> Running in d 1 feaede 849 f ---> 4905 d 9869 aa 7 Removing intermediate container d 1 feaede 849 f Successfully built 4905 d 9869 aa 7 [root@compute nginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx v 2 4905 d 9869 aa 7 53 seconds ago 404.1 MB #启动容器 [root@compute nginx]# docker run --name testnginx -d -p 83: 80 nginx:v 2 b 2 cd 72936465464 bb 8 a 88 e 9 b 3 c 5 df 0015241 c 7 d 17 cb 74062433 ef 79582 c 58908 #测试 [root@compute nginx]# curl 192.168 . 128.165: 83 hello docker |
如对您有帮助,支持下呗!
微信

支付宝

分类:
Docker
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类