前端项目 本地部署 docker nginx容器 调试步骤

531 阅读1分钟

挂载宿主机的nginx配置一直不成功,确认了几遍还是不行,无奈只能进入容器修改配置了

docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:76: mounting "/root/nginx.conf" to rootfs at "/etc/nginx/nginx.conf" caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.
[root@poloyy ~]# client_loop: send disconnect: Broken pipe

unknown: Are you trying to mount a directory onto a file (or vice-versa)?

详细步骤

docker 安装 nginx


docker pull nginx

运行nginx容器 命名nginx-test


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

  • 宿主机本地运行localhost:8080验证是否成功

docker进入nginx-test容器


docker exec -it nginx-test bash

vim修改nginx配置文件

更新安装工具


apt-get update

安装vim


apt-get install vim

修改配置文件


vim /etc/nginx/conf.d/default.conf

  • 注意nginx配置端口要和运行容器时的映射端口保持一致

例如


server {

listen 80;

server_name localhost;

root /usr/share/nginx/dist;

location ^~/api/ {

proxy_pass IP或者域名;

}

location / {

try_files $uri $uri/ /index.html;

index index.html index.htm;

}

}

  • ^~/api/ 对应前端想要代理去访问的接口地址拼接

前端打包项目上传到nginx-test容器中


docker cp /data/dist/ nginx-test:/usr/share/nginx/

  • docker cp 不会覆盖文件

  • /data/dist/ 在宿主机的C盘根目录

  • nginx-test:/usr/share/nginx/ 对应nginx-test容器的位置,也对应nginx配置的root路径

本地浏览器打开地址 localhost:8080 访问即可