Docker专题-04 Nginx部署

时间:2024-03-22 17:05:21

操作如下

  • 总体目录结构
nginx/
├── conf
├── docker-compose.yml
├── html
└── logs
  • 先获取nginx相关文件
# 拉取镜像并运行
docker pull nginx
docker run -d --name nginx nginx:latest
docker exec -it nginx /bin/bash

# 到以下路径压缩目录
cd /usr/share/nginx/ && tar -zcvf nginx_html.tar.gz
cd /etc/ && tar -zcvf nginx_conf.tar.gz
cd /var/log/ && tar -zcvf nginx_logs.tar.gz

# 拷贝以上压缩包至本地 /home/hukanfa/backupFile/nginx
docker cp 05e9651921a8:/usr/share/nginx/nginx_html.tar.gz ./
docker cp 05e9651921a8:/etc/nginx_conf.tar.gz ./
docker cp 05e9651921a8:/var/log/nginx_log.tar.gz ./
  • 创建项目目录并解压包文件
# /home/hukanfa
mkdir nginx
# 解压重名 /home/hukanfa/backupFile/nginx/
tar -zxvf nginx_html.tar.gz -C /home/hukanfa/nginx
tar -zxvf nginx_conf.tar.gz -C /home/hukanfa/nginx
mv /home/hukanfa/nginx/nginx  /home/hukanfa/nginx/conf
tar -zxvf nginx_log.tar.gz -C /home/hukanfa/nginx
mv /home/hukanfa/nginx/nginx  /home/hukanfa/nginx/logs
  • docker-compose.yml
# host 模式不用再另行映射端口,直接使用宿主机80/443端口
version: "3"
services:
web:
    image: nginx:latest
  container_name: nginx
    restart: always
    volumes:
      - ./html:/usr/share/nginx/html
      - ./conf:/etc/nginx
      - ./logs:/var/log/nginx
    network_mode: host
  • 创建容器
docker-compose up -d
  • 如果懒得通过上面方式获取相关包文件,这里提供现成的直接下载即可
https://pan.baidu.com/s/1BRh-t-jBpzlX7cc0PnexKw?pwd=wzv9