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.運(yùn)行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)在運(yùn)行了
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.運(yùn)行結(jié)果
[root@localhost my.Shells]# ./openFirewallPort.sh //先在防火墻上開一個(gè)端口
enter the port:
success
---openFirewallPort.sh-------
echo "enter the port: "
read port
firewall-cmd --add-port=$port/tcp
#下圖已經(jīng)成功訪問(wèn)到了
注意:
當(dāng)docker運(yùn)行nginx時(shí),外界訪問(wèn)還是docker所在的那個(gè)IP地址,就相當(dāng)于nginx在那臺(tái)機(jī)器上運(yùn)行一樣。
但對(duì)于docker所在的那臺(tái)機(jī)器來(lái)說(shuō),nginx就是附屬于docker的一個(gè)鏡像。若操作nginx還是由docker登錄nginx容器,進(jìn)行操作。
登錄的nginx容器就是一個(gè)linux系統(tǒng),只不過(guò)只有nginx而已,nginx按照l(shuí)inux默認(rèn)路徑安裝。比如
root@c0462d5e1878:/usr/share/nginx/html# ls 這個(gè)路徑就是默認(rèn)的靜態(tài)頁(yè)面存放路徑
50x.html index.html
bash命令都一樣,但是vi在我機(jī)器上是不能用的,但可以使用cp、mv 等命令,因?yàn)閚ginx都是配置好的,不能亂改。
1)可以通過(guò)在還未登錄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)通過(guò)主機(jī)目錄映射到容器
docker run -p 80:80 -d -v $PWD/html:usr/share/nginx/html docker.io/nginx
-v $PWD/html:usr/share/nginx/html 表示把當(dāng)前路徑下html目錄映射為usr/share/nginx/html
也就是說(shuō)主機(jī)下的html就是容器下的usr/share/nginx/html
html內(nèi)的文件修改和添加就等同于容器usr/share/nginx/html文件操作
外網(wǎng)訪問(wèn)就可以訪問(wèn)得到,就不用再登錄容器操作文件了
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.再開啟一個(gè)相同的服務(wù)
[root@localhost my.Shells]# docker run -p 8081:80 -d docker.io/nginx
//再開啟一個(gè)服務(wù),端口為8081
1fd8a0b5d138203150f1cdbfb9690235159159881785a4654abb04c7c96c5b18
[root@localhost my.Shells]# docker ps //會(huì)有兩個(gè)進(jìn)程,一個(gè)8080,一個(gè)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
上圖訪問(wèn)的是新開啟的8081服務(wù)注意:新啟動(dòng)的服務(wù)和原先的服務(wù)是兩個(gè)容器,原先的hello.html在新服務(wù)中是沒(méi)有的
7.卸載服務(wù)
[root@localhost my.Shells]# docker ps //此時(shí)8080和8081都在運(yùn)行
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還在運(yùn)行
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在運(yùn)行,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 //移除這個(gè)進(jìn)程進(jìn)行了,注意運(yùn)作著的進(jìn)程是無(wú)法rm的,要先stop
1fd8a0b5d138
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。