Docker搭建Nginx


1 查找 Docker Hub 上的 nginx 鏡像

docker search nginx

 

2 這里我們拉取官方的鏡像

docker pull nginx

 

3 等待下載完成后,我們就可以在本地鏡像列表里查到 REPOSITORY 為 nginx 的鏡像。

docker images nginx

 

4 以下命令使用 NGINX 默認的配置來啟動一個 Nginx 容器實例:

 docker run --name runoob-nginx-test -p 8081:80 -d nginx
  • runoob-nginx-test 容器名稱。
  • the -d設置容器在在后台一直運行。
  • the -p 端口進行映射,將本地 8081 端口映射到容器內部的 80 端口。

執行以上命令會生成一串字符串,類似 1dd4380ba70820bd2acc55ed2b326dd8c0ac7c93f68f0067daecad82aef5f938,這個表示容器的 ID,一般可作為日志的文件名。

 

5 我們可以使用 docker ps 命令查看容器是否有在運行:

docker ps

 

6 PORTS 部分表示端口映射,本地的 8081 端口映射到容器內部的 80 端口。

在瀏覽器中打開 http://127.0.0.1:8081/,效果如下:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

 

 第一次運行上面的容器可以把容器內部默認的配置文件復制出來,然后后面使用他的默認配置文件。

 

 

 

nginx 部署

首先,創建目錄 nginx, 用於存放后面的相關東西

mkdir -p /opt/nginx/html /opt/nginx/logs /opt/nginx/conf /opt/nginx/ssl

 

拷貝容器內 Nginx 默認配置文件和靜態文件到本地創建的掛載目錄,容器 ID 可以查看 docker ps 命令輸入中的第一列:

docker cp 88a5cb9e51b2:/etc/nginx/nginx.conf /opt/nginx/conf    //把默認的配置文件也拷貝過來,方便直接使用
docker cp 88a5cb9e51b2:/etc/nginx/conf.d/default.conf /opt/nginx/conf/conf.d    //把默認的配置文件也拷貝過來,方便直接使用
docker cp 88a5cb9e51b2:/usr/share/nginx/html/index.html /opt/nginx/html //把默認的靜態文件也拷貝過來,方便直接使用
docker cp 88a5cb9e51b2:/usr/share/nginx/html/50x.html /opt/nginx/html  //把默認的靜態文件也拷貝過來,方便直接使用
  • html: 目錄將映射為 nginx 容器配置的虛擬目錄。
  • logs: 目錄將映射為 nginx 容器的日志目錄。
  • conf: 目錄里的配置文件將映射為 nginx 容器的配置文件。
  • ssl: 目錄為后面作https配置用,為可選項。

部署命令

docker run -d -p 80:80 --name nginx -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/conf/conf.d:/etc/nginx/conf.d nginx


如果加上ssl配置
docker run -d -p 80:80 -p 443:443 --name nginx -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/conf/conf.d:/etc/nginx/conf.d -v /opt/nginx/ssl:/etc/nginx/ssl nginx


  • -p 80:80: 將容器的 80 端口映射到主機的 80 端口。

  • -p 443:443: 將容器的 443 端口映射到主機的 443 端口。(可選項,配置https時加上)
  • --name nginx:將容器命名為 nginx。

  • -v /opt/nginx/html:/usr/share/nginx/html:將我們自己創建的 html目錄掛載到容器的 /usr/share/nginx/html。

  • -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:將我們自己創建的 nginx.conf 掛載到容器的 /etc/nginx/nginx.conf。

  • -v /opt/nginx/logs:/var/log/nginx:將我們自己創建的 logs 掛載到容器的 /var/log/nginx。

  • -v /opt/nginx/conf/conf.d:/etc/nginx/conf.d: 將conf.d目錄掛載,后面進行二級域名反向代理映射端口時用,不配做這個后面配置不生效
  • -v /opt/nginx/ssl:/etc/nginx/ssl:將我們自己創建的 ssl目錄 掛載到容器的 /etc/nginx/ssl,此項為可選項,要使用https證書時加上。

 

在瀏覽器中打開 http://127.0.0.1:80/,效果如下:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

88a5cb9e51b2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM