Docker部署nginx實現(xiàn)過程圖文詳解
1.下載nginx
[root@localhost my.Shells]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/redis latest 1e70071f4af4 6 weeks ago 106.7 MB [root@localhost my.Shells]# docker pull nginx //下載nginx Using default tag: latest Trying to pull repository docker.io/library/nginx ... latest: Pulling from docker.io/library/nginx e7bb522d92ff: Pull complete 6edc05228666: Pull complete cd866a17e81f: Pull complete Digest: sha256:285b49d42c703fdf257d1e2422765c4ba9d3e37768d6ea83d7fe2043dad6e63d [root@localhost my.Shells]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/nginx latest 3f8a4339aadd 3 weeks ago 108.5 MB docker.io/redis latest 1e70071f4af4 6 weeks ago 106.7 MB
2.運行nginx
[root@localhost my.Shells]# docker run -p 8080:80 -d docker.io/nginx //將80端口映射為8080,或者80:80還是原先的80端口,不可以不寫。 c0462d5e18783e20f9515108fa62ab0f2ac808ea85370a7c82aee9407abf4672 [root@localhost my.Shells]# netstat -anp | grep 8080 //端口已經(jīng)開啟了 tcp6 0 0 :::8080 :::* LISTEN 2529/docker-proxy-c [root@localhost my.Shells]# docker ps //nginx已經(jīng)在運行了 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c0462d5e1878 docker.io/nginx "nginx -g 'daemon off" 4 minutes ago Up 4 minutes 0.0.0.0:8080->80/tcp angry_mccarthy
3.運行結(jié)果
[root@localhost my.Shells]# ./openFirewallPort.sh //先在防火墻上開一個端口 enter the port: success ---openFirewallPort.sh------- echo "enter the port: " read port firewall-cmd --add-port=$port/tcp #下圖已經(jīng)成功訪問到了
注意:
當docker運行nginx時,外界訪問還是docker所在的那個IP地址,就相當于nginx在那臺機器上運行一樣。
但對于docker所在的那臺機器來說,nginx就是附屬于docker的一個鏡像。若操作nginx還是由docker登錄nginx容器,進行操作。
登錄的nginx容器就是一個linux系統(tǒng),只不過只有nginx而已,nginx按照linux默認路徑安裝。比如
root@c0462d5e1878:/usr/share/nginx/html# ls 這個路徑就是默認的靜態(tài)頁面存放路徑
50x.html index.html
bash命令都一樣,但是vi在我機器上是不能用的,但可以使用cp、mv 等命令,因為nginx都是配置好的,不能亂改。
1)可以通過在還未登錄nignx容器前,把需要的文件寫好,然后復(fù)制到指定目錄下:
[root@localhost my.Shells]# docker cp hello.html c0462d5e1878://usr/share/nginx/html
[root@localhost my.Shells]# docker exec -it c0462d5e1878 bash
root@c0462d5e1878:/usr/share/nginx/html# ls
50x.html hello.html index.html
2)通過主機目錄映射到容器
docker run -p 80:80 -d -v $PWD/html:usr/share/nginx/html docker.io/nginx
-v $PWD/html:usr/share/nginx/html 表示把當前路徑下html目錄映射為usr/share/nginx/html
也就是說主機下的html就是容器下的usr/share/nginx/html
html內(nèi)的文件修改和添加就等同于容器usr/share/nginx/html文件操作
外網(wǎng)訪問就可以訪問得到,就不用再登錄容器操作文件了
4.停止服務(wù)
[root@localhost my.Shells]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c0462d5e1878 docker.io/nginx "nginx -g 'daemon off" 56 minutes ago Up 56 minutes 0.0.0.0:8080->80/tcp angry_mccarthy [root@localhost my.Shells]# docker stop c0462d5e1878 c0462d5e1878 [root@localhost my.Shells]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5.重啟服務(wù)
[root@localhost my.Shells]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost my.Shells]# docker start c0462d5e1878 c0462d5e1878 [root@localhost my.Shells]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c0462d5e1878 docker.io/nginx "nginx -g 'daemon off" 59 minutes ago Up 12 seconds 0.0.0.0:8080->80/tcp angry_mccarthy
6.再開啟一個相同的服務(wù)
[root@localhost my.Shells]# docker run -p 8081:80 -d docker.io/nginx //再開啟一個服務(wù),端口為8081 1fd8a0b5d138203150f1cdbfb9690235159159881785a4654abb04c7c96c5b18 [root@localhost my.Shells]# docker ps //會有兩個進程,一個8080,一個8081 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1fd8a0b5d138 docker.io/nginx "nginx -g 'daemon off" 4 seconds ago Up 3 seconds 0.0.0.0:8081->80/tcp suspicious_hypatia c0462d5e1878 docker.io/nginx "nginx -g 'daemon off" About an hour ago Up 4 minutes 0.0.0.0:8080->80/tcp angry_mccarthy
上圖訪問的是新開啟的8081服務(wù)注意:新啟動的服務(wù)和原先的服務(wù)是兩個容器,原先的hello.html在新服務(wù)中是沒有的
7.卸載服務(wù)
[root@localhost my.Shells]# docker ps //此時8080和8081都在運行 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1fd8a0b5d138 docker.io/nginx "nginx -g 'daemon off" 4 minutes ago Up 4 minutes 0.0.0.0:8081->80/tcp suspicious_hypatia c0462d5e1878 docker.io/nginx "nginx -g 'daemon off" About an hour ago Up 8 minutes 0.0.0.0:8080->80/tcp angry_mccarthy [root@localhost my.Shells]# docker stop 1fd8a0b5d138 //停下8081 1fd8a0b5d138 [root@localhost my.Shells]# docker ps //就剩8080還在運行 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c0462d5e1878 docker.io/nginx "nginx -g 'daemon off" About an hour ago Up 9 minutes 0.0.0.0:8080->80/tcp angry_mccarthy [root@localhost my.Shells]# docker ps -a //可以看到8080在運行,8081已經(jīng)Exited CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1fd8a0b5d138 docker.io/nginx "nginx -g 'daemon off" 5 minutes ago Exited (0) 7 seconds ago suspicious_hypatia c0462d5e1878 docker.io/nginx "nginx -g 'daemon off" About an hour ago Up 9 minutes 0.0.0.0:8080->80/tcp angry_mccarthy [root@localhost my.Shells]# [root@localhost my.Shells]# docker rm 1fd8a0b5d138 //移除這個進程進行了,注意運作著的進程是無法rm的,要先stop 1fd8a0b5d138
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。