[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
[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 //端口已經開啓了 tcp6 0 0 :::8080 :::* LISTEN 2529/docker-proxy-c [root@localhost my.Shells]# docker ps //nginx已經在運行了 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
[root@localhost my.Shells]# ./openFirewallPort.sh //先在防火牆上開一個端口 enter the port: 8080 success
---openFirewallPort.sh-------
echo "enter the port: "
read port
firewall-cmd --add-port=$port/tcphtml
下圖已經成功訪問到了
注意: 當docker運行nginx時,外界訪問仍是docker所在的那個IP地址,就至關於nginx在那臺機器上運行同樣。 但對於docker所在的那臺機器來講,nginx就是附屬於docker的一個鏡像。若操做nginx仍是由docker登陸nginx容器,進行操做。 登陸的nginx容器就是一個linux系統,只不過只有nginx而已,nginx按照linux默認路徑安裝。好比 root@c0462d5e1878:/usr/share/nginx/html# ls 這個路徑就是默認的靜態頁面存放路徑 50x.html index.html bash命令都同樣,可是vi在我機器上是不能用的,但能夠使用cp、mv 等命令,由於nginx都是配置好的,不能亂改。
1)能夠經過在還未登陸nignx容器前,把須要的文件寫好,而後複製到指定目錄下: [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)
2)經過主機目錄映射到容器linux
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內的文件修改和添加就等同於容器usr/share/nginx/html文件操做
外網訪問就能夠訪問獲得,就不用再登陸容器操做文件了
[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
[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
[root@localhost my.Shells]# docker run -p 8081:80 -d docker.io/nginx //再開啓一個服務,端口爲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服務
注意:新啓動的服務和原先的服務是兩個容器,原先的hello.html在新服務中是沒有的
[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已經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