作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.下载镜像并初始化容器
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服务哟,具体如下图所示。
[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服务是否可以正常
三.在宿主机(使用自定义容器ID)基于命令行制作镜像
[root@docker201.yinzhengjie.org.cn ~]# docker container commit --help
Usage: docker container commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
[root@docker201.yinzhengjie.org.cn ~]#
[root@docker201.yinzhengjie.org.cn ~]#
[root@docker201.yinzhengjie.org.cn ~]# docker container commit --help #查看命令的帮助信息
1>.提交时不不带版本号(不推荐使用,因为默认提交的版本号是"latest")
[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 ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d2e8defc0fc centos:centos7.6.1810 "/bin/bash" About an hour ago Up About an hour myNginx
[root@docker201.yinzhengjie.org.cn ~]# [root@docker201.yinzhengjie.org.cn ~]# docker container commit -m "YinZhengjie's Docker Nginx" 7d2e8defc0fc jason/centos7-nginx
sha256:c4e0980a825a1778d0c54a8c24644bc687543fac91a1fcc6687ac842f945bf17
[root@docker201.yinzhengjie.org.cn ~]#
[root@docker201.yinzhengjie.org.cn ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
jason/centos7-nginx latest c4e0980a825a 15 seconds ago 448MB
centos centos7.6.1810 f1cb7c7d58b7 10 months ago 202MB
[root@docker201.yinzhengjie.org.cn ~]#
2>.带tag的镜像提交(生产环境推荐使用,后期可以根据tag标记不同版本的image进行启动)
[root@docker201.yinzhengjie.org.cn ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
jason/centos7-nginx latest c4e0980a825a 6 minutes ago 448MB
centos centos7.6.1810 f1cb7c7d58b7 10 months ago 202MB
[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" About an hour ago Up About an hour myNginx
[root@docker201.yinzhengjie.org.cn ~]# [root@docker201.yinzhengjie.org.cn ~]# docker container commit -m "YinZhengjie's Docker Nginx" 7d2e8defc0fc jason/centos7-nginx:v0.1.20200114
sha256:7372d16c99bce2e8345432154179075ad627658e620379b3739a4d492bbcdeb4
[root@docker201.yinzhengjie.org.cn ~]#
[root@docker201.yinzhengjie.org.cn ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
jason/centos7-nginx v0.1.20200114 7372d16c99bc 2 seconds ago 448MB
jason/centos7-nginx latest c4e0980a825a 10 minutes ago 448MB
centos centos7.6.1810 f1cb7c7d58b7 10 months ago 202MB
[root@docker201.yinzhengjie.org.cn ~]#
四.基于自己制作的镜像启动容器
1>.创建容器
[root@docker201.yinzhengjie.org.cn ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
jason/centos7-nginx v0.1.20200114 7372d16c99bc 9 minutes ago 448MB
jason/centos7-nginx latest c4e0980a825a 20 minutes ago 448MB
centos centos7.6.1810 f1cb7c7d58b7 10 months ago 202MB
[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" About an hour ago Up About an hour myNginx
[root@docker201.yinzhengjie.org.cn ~]#
[root@docker201.yinzhengjie.org.cn ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@docker201.yinzhengjie.org.cn ~]#
[root@docker201.yinzhengjie.org.cn ~]# hostname -i
192.168.6.201
[root@docker201.yinzhengjie.org.cn ~]#
[root@docker201.yinzhengjie.org.cn ~]# docker container run -it -d --name myDockerNginx --hostname yinzhengjieDockerNginx -p 192.168.6.201:8080:80/tcp --dns 192.168.7.254 jason/centos7-nginx:v0.1.20200114 /usr/sbin/nginx
4d4bbd064581e4ca301d1fe8de1978e38ab63cb6a64fc8615f8d37779fff087e
[root@docker201.yinzhengjie.org.cn ~]#
[root@docker201.yinzhengjie.org.cn ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d4bbd064581 jason/centos7-nginx:v0.1.20200114 "/usr/sbin/nginx" 3 seconds ago Up 2 seconds 192.168.6.201:8080->80/tcp myDockerNginx
7d2e8defc0fc centos:centos7.6.1810 "/bin/bash" About an hour ago Up About an hour myNginx
[root@docker201.yinzhengjie.org.cn ~]#
[root@docker201.yinzhengjie.org.cn ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 20480 192.168.6.201:8080 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@docker201.yinzhengjie.org.cn ~]#
[root@docker201.yinzhengjie.org.cn ~]#
2>.连接咱们的容器并验证nginx服务是否正常启动
五.使用客户端浏览器访问测试,如下图所示