前提介紹
OS:騰訊云,CentOS7
登陸權(quán)限:普通用戶
Docker版本:17.12.0-ce
最后實(shí)現(xiàn):訪問容器里 nginx服務(wù)器 的自定義頁面
1、檢查是否存在 nginx 鏡像
[sun@docker ~]$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 16.04 00fd29ccc6f1 4 weeks ago 111MB
laradock_redis latest 19a977a11685 3 months ago 107MB
<none> <none> a03f07cc3324 3 months ago 15.5MB
laradock_workspace latest 318c7dc4d367 3 months ago 696MB
redis latest b6dddb991dfa 3 months ago 107MB
mysql 8.0 6cfa8ff69d16 3 months ago 343MB
laradock/workspace 1.8-71 b88aa2c85533 8 months ago 691MB
laradock/php-fpm 1.4-71 a8cca8d57319 8 months ago 400MB
docker-whale latest 5bce90a6d2b4 9 months ago 257MB
nginx latest 5766334bdaa0 9 months ago 182MB
注:最后一個(gè)就是 nginx 鏡像
如果不會(huì)下載鏡像,可參考 Docker常用命令(一)
2、啟動(dòng)并運(yùn)行容器
[sun@docker ~]$ docker run --name=webserver -d -p 8080:80 nginx
7d2e9bee679e60967dc5d7e25fded0fa5b6305fbb9c5d7773651e557305a816d
這條命令會(huì)基于 nginx 鏡像,啟動(dòng)并運(yùn)行一個(gè)容器
--name [容器名稱] 給這個(gè)容器命名
-d 后臺運(yùn)行
-p 指定從外部到容器的內(nèi)部的端口映射
[外部端口 : 容器內(nèi)部端口](8080:80)(注:因?yàn)槲业尿v訊云服務(wù)器,8080端口沒有被使用,所以作為外部映射的端口,而80端口,是容器里的 nginx 服務(wù)器默認(rèn)的訪問端口)
nginx 指定基于的鏡像
該命令最后返回值是:容器的ID
3、查看當(dāng)前運(yùn)行的容器
[sun@docker ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d2e9bee679e nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 443/tcp, 0.0.0.0:8080->80/tcp webserver
// 查看所有容器
[sun@docker ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d2e9bee679e nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 443/tcp, 0.0.0.0:8080->80/tcp webserver
e806ecf3c1db hello-world "/hello" 6 days ago Exited (0) 6 days ago romantic_haibt
// 查看最新創(chuàng)建的容器
[sun@docker ~]$ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d2e9bee679e nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 443/tcp, 0.0.0.0:8080->80/tcp webserver
4、訪問容器里 nginx 服務(wù)器

image.png
注:顯示的是 nginx 默認(rèn)歡迎頁面
5、修改歡迎頁面
[sun@docker ~]$ docker exec -it webserver bash
root@7d2e9bee679e:/# echo '<h1>Hello World</h1>' > /usr/share/nginx/html/index.html
root@7d2e9bee679e:/# exit
exit
docker exec 進(jìn)入容器
-it 開啟一個(gè)交互模式的終端
webserver 容器名稱
bash 交互式 Shell 程序
命令效果:用 <h1>Hello World</h1> 覆蓋了 /usr/share/nginx/html/index.html 的內(nèi)容
刷新頁面后,顯示內(nèi)容為:

image.png
6、啟用/停用容器
// 停用
[sun@docker ~]$ docker stop webserver
webserver
// 啟用
[sun@docker ~]$ docker start webserver
webserver