Docke搭建:Nginx

12/31/2024 运维操作系统Docker

目录


参考:


# Docke搭建:Nginx

# 环境依赖

软件/系统 版本 架构 包名 备注
Windows 11 x86_64

# 查询镜像

# 查询nginx镜像
docker search nginx
1
2

# 拉取镜像

# 拉取对应的版本镜像
docker pull nginx:latest
1
2
# 检查镜像
docker images
1
2

# 启动容器

创建挂载目录

mkdir -p /data/nginx/{conf,html,log}
mkdir -p /data/nginx/conf/conf.d
1
2

复制配置文件

# 创建临时nginx容器,用于复制配置文件
docker run --name nginx \
           -p 80:80 \
           -d nginx:latest
1
2
3
4
# 将容器nginx.conf文件复制到宿主机
docker cp nginx:/etc/nginx/nginx.conf /data/nginx/conf/nginx.conf

# 将容器conf.d文件夹下内容复制到宿主机
docker cp nginx:/etc/nginx/conf.d /data/nginx/conf/

# 将容器中的html文件夹复制到宿主机
docker cp nginx:/usr/share/nginx/html /data/nginx
1
2
3
4
5
6
7
8
#停止并删除创建的临时nginx容器
docker stop nginx
docker rm nginx
1
2
3

创建&启动容器

#创建nginx容器,并将nginx目录映射到宿主机
docker run --name nignx \
           -p 80:80 \
           -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
           -v /data/nginx/conf/conf.d:/etc/nginx/conf.d \
           -v /data/nginx/log:/var/log/nginx \
           -v /data/nginx/html:/usr/share/nginx/html \
           -d nginx:latest
1
2
3
4
5
6
7
8

命令结束,输出的是容器的 ID,表示容器已成功启动。

# 检查容器的状态
docker ps -a
# 查看日志
docker logs nginx
1
2
3
4

访问服务器ip:80例:http://127.0.0.1:80/ (opens new window)

# 进入容器

# 进入Nginx容器
docker exec -it nginx /bin/bash
1
2
#检查配置文件是否正确
nginx -t
1
2
# 不重启重新加载配置文件
nginx -s reload
1
2
上次更新时间: 9/25/2024, 1:19:15 AM