Docker镜像-手动制作yum版nginx镜像

                                     作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

 

一.载镜像并初始化容器

1>.下载CentOS指定版本镜像(默认下载最新的版本,而我指定的是CentOS 7.6主流版本)

[root@docker201.yinzhengjie.org.cn ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker image pull centos:centos7.6.1810
centos7.6.1810: Pulling from library/centos
ac9208207ada: Pull complete 
Digest: sha256:62d9e1c2daa91166139b51577fe4f4f6b4cc41a3a2c7fc36bd895e2a17a3e4e6
Status: Downloaded newer image for centos:centos7.6.1810
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              centos7.6.1810      f1cb7c7d58b7        10 months ago       202MB
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# 

2>.创建一个容器指定主机名及DNS信息并验证

[root@docker201.yinzhengjie.org.cn ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker container run -it -d --name myNginx --hostname yinzhengjieNginx --dns 192.168.7.254 centos:centos7.6.1810 
7d2e8defc0fc6bdcd14b683468987b5837a709c64794e7dc85aa15576bd99f32
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker container ls
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS               NAMES
7d2e8defc0fc        centos:centos7.6.1810   "/bin/bash"         2 seconds ago       Up 1 second                             myNginx
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker container exec -it myNginx bash
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# cat /etc/hostname 
yinzhengjieNginx
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# ping www.cnblogs.com -c 3
PING www.cnblogs.com (121.40.43.188) 56(84) bytes of data.
64 bytes from 121.40.43.188 (121.40.43.188): icmp_seq=1 ttl=127 time=33.2 ms
64 bytes from 121.40.43.188 (121.40.43.188): icmp_seq=2 ttl=127 time=33.3 ms
64 bytes from 121.40.43.188 (121.40.43.188): icmp_seq=3 ttl=127 time=33.3 ms

--- www.cnblogs.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 33.283/33.323/33.387/0.155 ms
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# cat /etc/resolv.conf 
search yinzhengjie.org.cn
nameserver 192.168.7.254
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# exit 
exit
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker container ls 
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS               NAMES
7d2e8defc0fc        centos:centos7.6.1810   "/bin/bash"         2 minutes ago       Up 2 minutes                            myNginx
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# 

3>.使用exec连接容器并配置阿里云的软件源

[root@docker201.yinzhengjie.org.cn ~]# docker container ls 
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS               NAMES
7d2e8defc0fc        centos:centos7.6.1810   "/bin/bash"         4 minutes ago       Up 4 minutes                            myNginx
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker container exec -it myNginx bash
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# ls /etc/yum.repos.d/
CentOS-Base.repo  CentOS-CR.repo  CentOS-Debuginfo.repo  CentOS-Media.repo  CentOS-Sources.repo  CentOS-Vault.repo  CentOS-fasttrack.repo
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2523  100  2523    0     0  27811      0 --:--:-- --:--:-- --:--:-- 28033
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# curl -o /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   664  100   664    0     0   7277      0 --:--:-- --:--:-- --:--:--  7296
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# ls /etc/yum.repos.d/
CentOS-Base.repo  CentOS-CR.repo  CentOS-Debuginfo.repo  CentOS-Media.repo  CentOS-Sources.repo  CentOS-Vault.repo  CentOS-fasttrack.repo  epel-7.repo
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# exit 
exit
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker container ls 
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS               NAMES
7d2e8defc0fc        centos:centos7.6.1810   "/bin/bash"         5 minutes ago       Up 5 minutes                            myNginx
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# 

 

二.安装并配置nginx

1>.使用yum的方式安装nginx

[root@docker201.yinzhengjie.org.cn ~]# docker container exec -it myNginx bash
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# yum -y install nginx
[root@yinzhengjieNginx /]# 

2>.安装常用工具

[root@yinzhengjieNginx /]# yum -y install -y vim wget pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop  

3>.关闭nginx后台运行并自定义nginx日志格式(仅供参考连接:https://www.cnblogs.com/yinzhengjie/p/12047186.html)

[root@yinzhengjieNginx /]# vim /etc/nginx/nginx.conf
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# egrep -v "^ *#|^$" /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
daemon off;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request"' '$status $body_bytes_sent "$http_referer"' '"$http_user_agent"' '"$http_x_forwarded_for"' '$server_name:$server_port';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;
        location / {
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# 

4>.自定义nginx的web页面

[root@yinzhengjieNginx /]# ll /usr/share/nginx/html/index.html 
lrwxrwxrwx. 1 root root 25 Jan 17 09:57 /usr/share/nginx/html/index.html -> ../../doc/HTML/index.html
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# rm -f /usr/share/nginx/html/index.html 
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# vim /usr/share/nginx/html/index.html 
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# cat /usr/share/nginx/html/index.html 
<h1>YinZhengjie's Nginx docker201.yinzhengjie.org.cn</h1>
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# 

5>.启动nginx并验证服务是否正常运行

[root@yinzhengjieNginx /]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# ss -ntl
State      Recv-Q Send-Q                                                                                          Local Address:Port                                                                                                         Peer Address:Port              
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# nginx             #启动nginx时当前终端会阻塞住,需要重新开启一个新的终端来验证nginx服务哟,具体如下图所示。

Docker镜像-手动制作yum版nginx镜像

[root@docker201.yinzhengjie.org.cn ~]# docker container ls
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS               NAMES
7d2e8defc0fc        centos:centos7.6.1810   "/bin/bash"         37 minutes ago      Up 37 minutes                           myNginx
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker container exec -it myNginx bash
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# ss -ntl
State      Recv-Q Send-Q                                                                                          Local Address:Port                                                                                                         Peer Address:Port              
LISTEN     0      128                                                                                                         *:80                                                                                                                      *:*                  
LISTEN     0      128                                                                                                      [::]:80                                                                                                                   [::]:*                  
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# curl 127.0.0.1
<h1>YinZhengjie's Nginx docker201.yinzhengjie.org.cn</h1>
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# hostname -i
172.17.0.2
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# curl 172.17.0.2
<h1>YinZhengjie's Nginx docker201.yinzhengjie.org.cn</h1>
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# exit 
exit
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# curl 172.17.0.2
<h1>YinZhengjie's Nginx docker201.yinzhengjie.org.cn</h1>
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker container exec -it myNginx bash
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# cat /var/log/nginx/
access.log  error.log   
[root@yinzhengjieNginx /]# cat /var/log/nginx/access.log 
127.0.0.1 - - [17/Jan/2020:10:25:58 +0000] "GET / HTTP/1.1"200 58 "-""curl/7.29.0""-"_:80
172.17.0.2 - - [17/Jan/2020:10:26:53 +0000] "GET / HTTP/1.1"200 58 "-""curl/7.29.0""-"_:80
172.17.0.2 - - [17/Jan/2020:10:27:22 +0000] "GET / HTTP/1.1"200 58 "-""curl/7.29.0""-"_:80
172.17.0.1 - - [17/Jan/2020:10:27:30 +0000] "GET / HTTP/1.1"200 58 "-""curl/7.29.0""-"_:80
172.17.0.1 - - [17/Jan/2020:10:27:39 +0000] "GET / HTTP/1.1"200 58 "-""curl/7.29.0""-"_:80
[root@yinzhengjieNginx /]# 
[root@yinzhengjieNginx /]# 
验证nginx服务是否可以正常(戳我查看详情)

相关文章: