本文最后更新于 2026-03-18T10:06:53+08:00
Docker部署Nginx
方法一:先复制容器文件到宿主机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| docker run --name temp-nginx nginx:latest
docker cp temp-nginx:/etc/nginx ~/nginx/conf docker cp temp-nginx:/usr/share/nginx/html ~/nginx/html docker cp temp-nginx:/var/log/nginx ~/nginx/logs
docker rm temp-nginx
docker run -d \ --name nginx \ -p 80:80 -p 443:443 \ -v ~/nginx/html:/usr/share/nginx/html \ -v ~/nginx/conf:/etc/nginx \ -v ~/nginx/logs:/var/log/nginx \ nginx:latest
|
方法二:使用初始化脚本(推荐)
创建一个初始化脚本 init-nginx.sh:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #!/bin/bash
NGINX_DIR=~/nginx
mkdir -p $NGINX_DIR/{conf,html,logs}
if [ ! -f $NGINX_DIR/conf/nginx.conf ]; then echo "初始化nginx配置文件..." docker run --name temp-nginx nginx:latest docker cp temp-nginx:/etc/nginx/. $NGINX_DIR/conf/ docker cp temp-nginx:/usr/share/nginx/html/. $NGINX_DIR/html/ docker rm temp-nginx > /dev/null echo "配置文件初始化完成" else echo "配置文件已存在,跳过初始化" fi
echo "启动nginx容器..." docker run -d \ --name nginx \ -p 80:80 -p 443:443 \ -v ~/nginx/html:/usr/share/nginx/html \ -v ~/nginx/conf:/etc/nginx \ -v ~/nginx/logs:/var/log/nginx \ nginx:latest
echo "Nginx容器已启动"
|
方法三:使用Docker Compose(最推荐)
创建 docker-compose.yml:
1 2 3 4 5 6 7 8 9 10 11 12 13
| version: '3.8' services: nginx: image: nginx:latest container_name: nginx ports: - "80:80" - "443:443" volumes: - ~/nginx/html:/usr/share/nginx/html - ~/nginx/conf:/etc/nginx - ~/nginx/logs:/var/log/nginx restart: unless-stopped
|
创建初始化脚本 setup-nginx.sh:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #!/bin/bash
NGINX_DIR=~/nginx
mkdir -p $NGINX_DIR/{conf,html,logs}
if [ ! -f $NGINX_DIR/conf/nginx.conf ]; then echo "正在初始化nginx配置..." docker run --rm nginx:latest cat /etc/nginx/nginx.conf > $NGINX_DIR/conf/nginx.conf docker run --rm nginx:latest cat /etc/nginx/mime.types > $NGINX_DIR/conf/mime.types docker run --rm nginx:latest tar -c -C /etc/nginx/conf.d . | tar -x -C $NGINX_DIR/conf echo "配置初始化完成" fi
docker-compose up -d
|
注意事项
- 权限问题:确保宿主机目录有正确的权限
- 配置文件:复制后记得根据需要修改
nginx.conf
- 数据持久化:这样设置后,配置和数据都会持久化在宿主机
推荐使用方法二或方法三,这样可以确保配置文件的完整性和可维护性。
Docker部署Nginx
https://schrodingerfish.github.io/2026/02/03/Docker/Docker部署Nginx/