之前说过docker容器是一种软件的打包技术,接下来我们体验一下Docker与传统配镜配置的区别。

传统编译安装nginx:

官网下载Nginx源码包wget  
tar解压源码包
创建Nginx用户
安装依赖包 编译安装三部曲configure,make,make install
修改nginx配置文件
启动nginx

在docker容器中: 

docker run -d -p 80:80 nginx

run:创建并运行一个容器
-d:表示在后台运行
-p:端口映射
nginx:docker镜像的名字

那么这里我们来实战操作一下docker中安装nginx,因为墙的原因docker run -d -p 80:80 nginx并不能生效,如下(连接超时)

[root@docker01 /]# time docker run -d -p 80:80 nginx
Unable to find image 'nginx:latest' locally
docker: Error response from daemon: Get https://registry-1.docker.io/v2/library/nginx/manifests/latest: Get https://auth.docker.io/token?scope=repository%3Alibrary%2Fnginx%3Apull&service=registry.docker.io: net/http: request canceled (Client.Timeout exceeded while awaiting headers).
See 'docker run --help'.

real	0m16.321s
user	0m0.021s
sys	0m0.040s
[root@docker01 /]# 

因此在此之前,我们还需要做镜像加速

1.配置docker镜像加速(手动添加该文件,并加入以下代码)

[root@docker01 /]# vi /etc/docker/daemon.json

{
"registry-mirrors": ["https://registry.docker-cn.com","https://dockerhub.azk8s.cn","https://reg-mirror.qiniu.com"]
}

2.重启docker

[root@docker01 /]# systemctl restart docker
[root@docker01 /]# 

3.重新上面的步骤:time docker run -d -p 80:80 nginx(timer表示计时),如下docker启动报错:

[root@docker01 /]# time docker run -d -p 80:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
0a4690c5d889: Pull complete 
9719afee3eb7: Pull complete 
44446b456159: Pull complete 
Digest: sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
Status: Downloaded newer image for nginx:latest
c57ccf765ec7f8587d208fefbf6d35d7098e705105549de8fb6cb63bd264560e
docker: Error response from daemon: driver failed programming external connectivity on endpoint trusting_lumiere (1d708b2c2a1294f393c272941975f95839714198e3553d20a8356c9b7916e32d): Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use.

real	0m30.810s
user	0m0.042s
sys	0m0.082s
[root@docker01 /]# 

4.netstat -lntup查看端口监听情况会发现80端口已被http监听,这时需要关闭apache服务

[root@docker01 /]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 10.0.0.11:11211         0.0.0.0:*               LISTEN      1076/memcached      
tcp        0      0 0.0.0.0:4369            0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1075/sshd           
tcp        0      0 0.0.0.0:15672           0.0.0.0:*               LISTEN      1082/beam.smp       
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1372/master         
tcp        0      0 0.0.0.0:25672           0.0.0.0:*               LISTEN      1082/beam.smp       
tcp        0      0 10.0.0.11:3306          0.0.0.0:*               LISTEN      1314/mysqld         
tcp6       0      0 ::1:11211               :::*                    LISTEN      1076/memcached      
tcp6       0      0 :::80                   :::*                    LISTEN      1077/httpd          
tcp6       0      0 :::22                   :::*                    LISTEN      1075/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1372/master         
tcp6       0      0 :::35357                :::*                    LISTEN      1077/httpd          
tcp6       0      0 :::5672                 :::*                    LISTEN      1082/beam.smp       
tcp6       0      0 :::5000                 :::*                    LISTEN      1077/httpd          
udp        0      0 10.0.0.11:11211         0.0.0.0:*                           1076/memcached      
udp        0      0 0.0.0.0:123             0.0.0.0:*                           773/chronyd         
udp        0      0 127.0.0.1:323           0.0.0.0:*                           773/chronyd         
udp6       0      0 ::1:11211               :::*                                1076/memcached      
udp6       0      0 ::1:323                 :::*                                773/chronyd   

5.关闭apache服务,目前大多数平台都支持IPv6了,所以apr也支持IPv6了。默认情况下,除了 FreeBSD, NetBSD, 和 OpenBSD外,其他平台都支持IPv6,所以CentOS 7也支持IPv6。

/usr/sbin/apachectl stop

6.重新运行docker服务

[root@docker01 /]# time docker run -d -p 80:80 nginx
efe791c12bb9c3c47915e07942278ae1e6b6cc78aa5ca5b9c4ad5327c3f83792

real	0m0.618s
user	0m0.025s
sys	0m0.025s
[root@docker01 /]# 

7.再次查看端口情况,docker成功已经监听80

[root@docker01 /]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 10.0.0.11:11211         0.0.0.0:*               LISTEN      1076/memcached      
tcp        0      0 0.0.0.0:4369            0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1075/sshd           
tcp        0      0 0.0.0.0:15672           0.0.0.0:*               LISTEN      1082/beam.smp       
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1372/master         
tcp        0      0 0.0.0.0:25672           0.0.0.0:*               LISTEN      1082/beam.smp       
tcp        0      0 10.0.0.11:3306          0.0.0.0:*               LISTEN      1314/mysqld         
tcp6       0      0 ::1:11211               :::*                    LISTEN      1076/memcached      
tcp6       0      0 :::80                   :::*                    LISTEN      7309/docker-proxy   
tcp6       0      0 :::22                   :::*                    LISTEN      1075/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1372/master         
tcp6       0      0 :::5672                 :::*                    LISTEN      1082/beam.smp       
udp        0      0 10.0.0.11:11211         0.0.0.0:*                           1076/memcached      
udp        0      0 0.0.0.0:123             0.0.0.0:*                           773/chronyd         
udp        0      0 127.0.0.1:323           0.0.0.0:*                           773/chronyd         
udp6       0      0 ::1:11211               :::*                                1076/memcached      
udp6       0      0 ::1:323                 :::*                                773/chronyd         
[root@docker01 /]# 

8.浏览器访问nginx,成功。

最后修改于 2019-07-22 15:54:26
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇