之前說過docker容器是一種軟件的打包技術(shù),接下來我們體驗(yàn)一下Docker與傳統(tǒng)配鏡配置的區(qū)別挪鹏。
傳統(tǒng)編譯安裝nginx:
官網(wǎng)下載Nginx源碼包wget
tar解壓源碼包
創(chuàng)建Nginx用戶
安裝依賴包 編譯安裝三部曲configure月而,make菜谣,make install
修改nginx配置文件
啟動nginx
在docker容器中:
docker run -d -p 80:80 nginx
run:創(chuàng)建并運(yùn)行一個(gè)容器
-d:表示在后臺運(yùn)行
-p:端口映射
nginx:docker鏡像的名字
那么這里我們來實(shí)戰(zhàn)操作一下docker中安裝nginx惭缰,因?yàn)閴Φ脑騞ocker run -d -p 80:80 nginx并不能生效辩稽,如下(連接超時(shí))
[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"]
}
2.重啟docker
[root@docker01 /]# systemctl restart docker
3.重新上面的步驟:time docker run -d -p 80:80 nginx(timer表示計(jì)時(shí)),如下docker啟動報(bào)錯(cuò):
[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查看端口監(jiān)聽情況會發(fā)現(xiàn)80端口已被http監(jiān)聽患整,這時(shí)需要關(guān)閉apache服務(wù)
[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.關(guān)閉apache服務(wù)拜效,目前大多數(shù)平臺都支持IPv6了,所以apr也支持IPv6了各谚。默認(rèn)情況下紧憾,除了 FreeBSD, NetBSD, 和 OpenBSD外,其他平臺都支持IPv6昌渤,所以CentOS 7也支持IPv6赴穗。
/usr/sbin/apachectl stop
6.重新運(yùn)行docker服務(wù)
[root@docker01 /]# time docker run -d -p 80:80 nginx
efe791c12bb9c3c47915e07942278ae1e6b6cc78aa5ca5b9c4ad5327c3f83792
real 0m0.618s
user 0m0.025s
sys 0m0.025s
[root@docker01 /]#
7.再次查看端口情況,docker成功已經(jīng)監(jiān)聽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 /]#