1.6 docker 构建NGINX镜像
Nginx是一个高性能的Web和反向代理服务器,它具有很多非常优越的特性:
1、作为Web服务器。
2、作为负载均衡服务器。
3、作为邮件代理服务器。
4、安装及配置简单。
接下来我们介绍在docker构建nginx镜像:
Docker镜像构建分为两种方式:
- 手动构建
- Dockerfile(自动构建)
一、Docker镜像手动构建容器实例
[root@itpux nginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mynginx latest 904dd77518e4 About an hour ago 109.1 MB nginx latest dbfc48660aeb 9 days ago 109.1 MB hub.c.163.com/library/centos latest 328edcd84f1b 14 months ago 192.5 MB hub.c.163.com/library/mysql latest 9e64176cd8a2 18 months ago 407.1 MB mysql latest 9e64176cd8a2 18 months ago 407.1 MB [root@itpux nginx]# docker pull centos Using default tag: latest latest: Pulling from library/centos aeb7866da422: Pull complete Digest: sha256:67dad89757a55bfdfabec8abd0e22f8c7c12a1856514726470228063ed86593b Status: Downloaded newer image for centos:latest [root@itpux nginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mynginx latest 904dd77518e4 About an hour ago 109.1 MB nginx latest dbfc48660aeb 9 days ago 109.1 MB centos latest 75835a67d134 2 weeks ago 200.4 MB hub.c.163.com/library/centos latest 328edcd84f1b 14 months ago 192.5 MB hub.c.163.com/library/mysql latest 9e64176cd8a2 18 months ago 407.1 MB mysql latest 9e64176cd8a2 18 months ago 407.1 MB [root@itpux nginx]# docker run --name nginxdocker -p 8080:80 -ti centos
一)容量内安装wget
[root@f191a15ea06c /]# yum install -y wget Loaded plugins: fastestmirror, ovl Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com base | 3.6 kB 00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/4): base/7/x86_64/group_gz | 166 kB 00:00:00 (2/4): extras/7/x86_64/primary_db | 204 kB 00:00:00 (3/4): updates/7/x86_64/primary_db | 6.0 MB 00:00:01 (4/4): base/7/x86_64/primary_db | 5.9 MB 00:00:09 Resolving Dependencies --> Running transaction check ---> Package wget.x86_64 0:1.14-15.el7_4.1 will be installed --> Finished Dependency Resolution
二)容器内安装阿里云YUM 的配置文件epel-7.repo
[root@f191a15ea06c /]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo --2018-10-25 09:39:10-- http://mirrors.aliyun.com/repo/epel-7.repo Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 119.249.58.213, 119.249.58.217, 119.249.58.214, ... Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|119.249.58.213|:80... connected.
三)容器YUM安装nginx
[root@2e511a125865 /]# yum install -y nginx Loaded plugins: fastestmirror, ovl Loading mirror speeds from cached hostfile * base: mirrors.huaweicloud.com * extras: mirrors.huaweicloud.com * updates: mirrors.huaweicloud.com
四)在容器里将'3a daemon off;'加入到容器/etc/nginx/nginx.conf
[root@2e511a125865 /]#sed -i '3a daemon off;' /etc/nginx/nginx.conf
[root@2e511a125865 /]# ll total 20 -rw-r--r--. 1 root root 12030 Oct 6 19:15 anaconda-post.log lrwxrwxrwx. 1 root root 7 Oct 6 19:14 bin -> usr/bin drwxr-xr-x. 5 root root 380 Oct 25 11:11 dev drwxr-xr-x. 50 root root 4096 Oct 25 11:13 etc drwxr-xr-x. 2 root root 6 Apr 11 2018 home lrwxrwxrwx. 1 root root 7 Oct 6 19:14 lib -> usr/lib lrwxrwxrwx. 1 root root 9 Oct 6 19:14 lib64 -> usr/lib64 drwxr-xr-x. 2 root root 6 Apr 11 2018 media drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt drwxr-xr-x. 2 root root 6 Apr 11 2018 opt dr-xr-xr-x. 189 root root 0 Oct 25 11:11 proc dr-xr-x---. 2 root root 114 Oct 6 19:15 root drwxr-xr-x. 10 root root 130 Oct 25 11:13 run lrwxrwxrwx. 1 root root 8 Oct 6 19:14 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 Apr 11 2018 srv dr-xr-xr-x. 13 root root 0 Oct 25 04:46 sys drwxrwxrwt. 7 root root 132 Oct 25 11:13 tmp drwxr-xr-x. 13 root root 155 Oct 6 19:14 usr drwxr-xr-x. 18 root root 238 Oct 6 19:14 var [root@2e511a125865 /]#
五)构建容器镜像
docker commit -m "test Nginx" 2e511a125865aa49935e00a92f8c357632f0b6d31c2914318496726fdcce440d nginxdocker/nginxdocker:v1
镜像 容器名 容器ID 镜像名称 镜像版本号
[root@itpux nginx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2e511a125865 centos "/bin/bash" 8 minutes ago Up 4 seconds nginxdocker [root@itpux nginx]# docker stop 2e511a125865aa49935e00a92f8c357632f0b6d31c2914318496726fdcce440d 2e511a125865aa49935e00a92f8c357632f0b6d31c2914318496726fdcce440d [root@itpux nginx]# docker commit -m "test Nginx" 2e511a125865aa49935e00a92f8c357632f0b6d31c2914318496726fdcce440d nginxdocker/nginxdocker:v1 sha256:4700aa51860f94deeb4424379b7de9695e9e7c0708a09b9deb2ea52f5a695811 [root@itpux nginx]#
六)检查是否镜像完成和通过镜像创建容器nginxserver
[root@itpux nginx]# docker run --name nginxserver -d -p 8080:80 nginxdocker/nginxdocker:v1 nginx 34abed4c4e6fc648d7e0b39683205a14645c1f93ae76012b6167b850bc109c1e [root@itpux nginx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 34abed4c4e6f nginxdocker/nginxdocker:v1 "nginx" 21 seconds ago Up 19 seconds 0.0.0.0:8080->80/tcp nginxserver [root@itpux nginx]#
七、查看响应头信息
[root@itpux nginx]# curl -I 192.168.26.188:8080 HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Thu, 25 Oct 2018 11:35:27 GMT Content-Type: text/html Content-Length: 3700 Last-Modified: Tue, 06 Mar 2018 09:26:21 GMT Connection: keep-alive ETag: "5a9e5ebd-e74" Accept-Ranges: bytes [root@itpux nginx]#