1、拉取镜像

  1. docker pull nginx:1.21.4

2、创建目录

  1. mkdir -p /opt/nginx/{html,logs}
  2. chmod 777 /opt/nginx/{html,logs}

3、增加配置文件

(1)先随便启动nginx容器,用于复制配置文件

  1. docker run -p 80:80 --name nginx -d nginx:1.21.4

(2)进入待存放nginx配置的目录下

  1. cd /opt/nginx

(3)将容器内文件复制到当前目录

  1. docker container cp nginx:/etc/nginx /opt/nginx

(4)将nginx重命名为config

  1. mv nginx config
  2. chmod 7777 config

(5)查看config文件夹

  1. cd /opt/nginx/config
  2. ls

image.png

(3)删除容器

  1. docker rm -f nginx

4、启动容器

  1. docker run -p 80:80 --name nginx --restart=always \
  2. -v /opt/nginx/config:/etc/nginx \
  3. -v /opt/nginx/html:/usr/share/nginx/html \
  4. -v /opt/nginx/logs:/var/log/nginx \
  5. -d nginx:1.21.4