一、docker 起一个nginx 容器
docker run -d --name 起一个容器名 -p 8899:8899 --privileged nginx
注
:端口映射前面是外部的端口,后面是容器内项目的端口
-
因为容器中无法直接进行修改,将nginx.conf的配置文件复制出来
注:sad_gates
为容器名docker cp sad_gates:/etc/nginx/nginx.conf /opt/nginx.config
-
修改刚才复制的nginx.conf,在http内添加以下内容
server { listen 8899; #1.vue项目跑在哪个端口 server_name 192.168.1.123; #2.当前服务器ip location / { root /opt/dist; #3.dist文件的位置(我是直接放在home目录下了) try_files $uri $uri/ /index.html; #4.重定向,内部文件的指向(照写) } location /api { #4.当请求跨域时配置端口转发 proxy_pass http://192.168.1.123/api; #5.转发地址 } }
-
替换nginx.conf文件,将此文件复制到容器中
docker cp /opt/nginx.config sad_gates:/etc/nginx/nginx.conf
-
在容器中创建dist文件夹,将dist内容复制到容器中
docker cp /opt/dist sad_gates:/opt/dist
二、重启容器
docker restart 容器名