環境:
CentOS Linux release 7.9.2009 (Core)
step1:創建一個nginx的工作目錄
暫定目錄為 nginxpath
step2:創建一個默認的nginx容器
docker run --name nginx-demo -d nginx
step3:復制相關的配置文件
cd /nginxpath docker cp nginx-demo:/etc/nginx/nginx.conf . docker cp nginx-demo:/etc/nginx/conf.d/default.conf . docker cp nginx-demo:/usr/share/nginx/html .
目錄結構為:
nginxpath ├── conf.d │ ├── default.conf ├── html │ ├── 50x.html │ └── index.html └── nginx.conf
step4:編寫docker-compose.yml文件
為了方便管理,我們新建一個文件夾conf用來存放nginx.conf文件
1 version: "3.0" 2 3 services: 4 ngnix: #服務名稱,名稱可自定義 5 container_name: nginx-compose #容器名稱,可自定義 6 image: nginx # 鏡像名稱 7 restart: always 8 ports: # 容器和服務器對應的端口映射,每次新增一個站點之后一定要在這里把對應的端口映射加上,不然肯定會404 9 - 80:80 10 - 8080:8080 11 - 8081:8081 12 - 8082:8082 13 privileged: true 14 volumes: 15 - /home/install/softs/04.nginx/conf.d:/etc/nginx/conf.d 16 - /home/install/softs/04.nginx/conf/nginx.conf:/etc/nginx/nginx.conf 17 - /home/install/softs/04.nginx/logs:/var/log/nginx 18 - /home/install/softs/04.nginx/html:/usr/share/nginx/html 19 - /etc/letsencrypt:/etc/letsencrypt 20 - /etc/localtime:/etc/localtime
目錄結構為:
nginxpath ├── conf.d #配置文件目錄 │ ├── default.conf #默認的配置文件 ├── html #靜態文件資源 │ ├── 50x.html #錯誤頁面 │ └── index.html #首頁 ├── conf #自定義配置文件目錄 │ └── nginx.conf #自定義配置文件 └── docker-compose.yml #docker-compose配置文件
step5:運行docker-compose查看結果
docker-compose up -d