Docker中Nginx搭建以及配置

docker nginx搭建

1 docker pull nginx

docker pull nginx

2 启动nginx

docker run --name nginx -p 80:80 -d nginx

3 查看是否启动

① docker ps

② 在浏览器输入服务器ip,可以看到“Welcome to nginx!”

4 配置映射

① 在本地创建nginx配置的文件夹

mkdir -p /mydata/nginx/{conf,log,html}

② 将容器中的配置先复制到创建的文件夹中

docker cp nginx:/etc/nginx/nginx.conf /mydata/nginx/conf/nginx.conf
docker cp nginx:/etc/nginx/conf.d /mydata/nginx/conf/conf.d
docker cp nginx:/usr/share/nginx/html /mydata/nginx/gs/

***为容器的id,可以通过docker ps查看

③ 停止并移除容器

docker stop ***
docker rm ***

④ 再次启动容器并做目录挂载

docker run -d --name nginx -p 80:80 \
-v /mydata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /mydata/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/log:/var/log/nginx \
--privileged=true nginx

挂载完成之后,在本地的修改会映射到容器中

⑤ 设置docker自动启动nginx

docker update nginx --restart=always

也可以配置在第四步

5 部署前端并配置反向代理

① 将打包的dist项目放置到创建的/mydata/nginx/html/中,可以进入容器内部发现容器里面也有了dist文件

② 修改配置文件/mydata/nginx/conf/conf.d/default.conf

    location / {
        root   /usr/share/nginx/html/dist;
        index  index.html index.htm;
    }
    location /api {
        proxy_pass http://gmall-h5-api.atguigu.cn;
    }

posted @ 2022-09-05 19:11  Tod4  阅读(2414)  评论(0编辑  收藏  举报