docker pull nginx 命令安裝
查找?Docker?Hub?上的 nginx 鏡像
runoob@runoob:~/nginx$ docker search nginx
NAME? ? ? ? ? ? ? ? ? ? ? DESCRIPTION? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? STARS? ? OFFICIAL? AUTOMATED
nginx? ? ? ? ? ? ? ? ? ? Official build of Nginx.? ? ? ? ? ? ? ? ? ? ? ? 3260? ? ? [OK]? ? ?
jwilder/nginx-proxy? ? ? Automated Nginx reverse proxy for docker c...? 674? ? ? ? ? ? ? ? ? [OK]richarvey/nginx-php-fpm? Container running Nginx + PHP-FPM capable ...? 207? ? ? ? ? ? ? ? ? [OK]million12/nginx-php? ? ? Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS...? 67? ? ? ? ? ? ? ? ? [OK]maxexcloo/nginx-php? ? ? Docker framework container with Nginx and ...? 57? ? ? ? ? ? ? ? ? [OK]...
這里我們拉取官方的鏡像
$ docker pull nginx
等待下載完成后,我們就可以在本地鏡像列表里查到 REPOSITORY 為 nginx 的鏡像。
runoob@runoob:~/nginx$ docker images nginx
REPOSITORY? ? ? ? ? TAG? ? ? ? ? ? ? ? IMAGE ID? ? ? ? ? ? CREATED? ? ? ? ? ? SIZE
nginx? ? ? ? ? ? ? latest? ? ? ? ? ? ? 555bbd91e13c? ? ? ? 3 days ago? ? ? ? ? 182.8 MB
以下命令使用 NGINX 默認的配置來啟動一個 Nginx 容器實例:
$ docker run --name runoob-nginx-test -p 8081:80 -d nginx
runoob-nginx-test?容器名稱。
the?-d設(shè)置容器在在后臺一直運行。
the?-p?端口進行映射,將本地 8081 端口映射到容器內(nèi)部的 80 端口。
執(zhí)行以上命令會生成一串字符串,類似?6dd4380ba70820bd2acc55ed2b326dd8c0ac7c93f68f0067daecad82aef5f938,這個表示容器的 ID,一般可作為日志的文件名。
我們可以使用 docker ps 命令查看容器是否有在運行:
$ docker ps
CONTAINER ID? ? ? ? IMAGE? ? ? ? ...? ? ? ? ? ? ? PORTS? ? ? ? ? ? ? ? ? NAMES6dd4380ba708? ? ? ? nginx? ? ? ? ...? ? ? 0.0.0.0:8081->80/tcp? runoob-nginx-test
PORTS 部分表示端口映射,本地的 8081 端口映射到容器內(nèi)部的 80 端口。
在瀏覽器中打開?http://127.0.0.1:8081/,效果如下:

nginx 部署
首先,創(chuàng)建目錄 nginx, 用于存放后面的相關(guān)東西。
$ mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf
拷貝容器內(nèi) Nginx 默認配置文件到本地當(dāng)前目錄下的 conf 目錄,容器 ID 可以查看?docker ps?命令輸入中的第一列:
docker cp 6dd4380ba708:/etc/nginx/nginx.conf ~/nginx/conf
www: 目錄將映射為 nginx 容器配置的虛擬目錄。
logs: 目錄將映射為 nginx 容器的日志目錄。
conf: 目錄里的配置文件將映射為 nginx 容器的配置文件。
部署命令
$ docker run -d -p 8082:80 --name runoob-nginx-test-web -v ~/nginx/www:/usr/share/nginx/html -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/nginx/logs:/var/log/nginx nginx
命令說明:
-p 8082:80:?將容器的 80 端口映射到主機的 8082 端口。
--name runoob-nginx-test-web:將容器命名為 runoob-nginx-test-web。
-v ~/nginx/www:/usr/share/nginx/html:將我們自己創(chuàng)建的 www 目錄掛載到容器的 /usr/share/nginx/html。
-v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:將我們自己創(chuàng)建的 nginx.conf 掛載到容器的 /etc/nginx/nginx.conf。
-v ~/nginx/logs:/var/log/nginx:將我們自己創(chuàng)建的 logs 掛載到容器的 /var/log/nginx。
啟動以上命令后進入 ~/nginx/www 目錄:
$ cd ~/nginx/www
創(chuàng)建 index.html 文件,內(nèi)容如下:
<!DOCTYPE html><html><head><meta charset="utf-8"><title>菜鳥教程(runoob.com)</title></head><body>? ? <h1>我的第一個標題</h1>? ? <p>我的第一個段落。</p></body></html>
輸出結(jié)果為:

相關(guān)命令
如果要重新載入 NGINX 可以使用以下命令發(fā)送 HUP 信號到容器:
$ docker kill -s HUP container-name
重啟 NGINX 容器命令:
$ docker restart container-name