五分钟带你玩转docker(八)nginx安装及部署vue项目

1.查看镜像

docker search nginx  

2.拉取镜像

 docker pull nginx 

3.查看镜像

docker images

4.运行镜像(需要将dist放于/usr/local/nginx/system中)

docker run --name nginx -d -p 80:80 --net=host -v /usr/local/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /usr/local/nginx/log:/var/log/nginx  -v /usr/local/nginx/system:/usr/share/nginx/system nginx

参数讲解:(ps 宿主机的文件夹需要新建)

docker run --name nginx:容器名称
 -d -p 80:80 
 --net=host:以host方式启动
 -v /usr/local/nginx/conf/nginx.conf:/etc/nginx/nginx.conf :宿主机的/usr/local/nginx/conf/nginx.conf文件映射到容器的/etc/nginx/nginx.conf 实际是以/usr/local/nginx/conf/nginx.conf为配置文件启动
 -v /usr/local/nginx/log:/var/log/nginx  :宿主机/usr/local/nginx/log映射到容器的/var/log/nginx 存放log日志 与nginx搭配使用
 -v /usr/local/nginx/system:/usr/share/nginx/system :宿主机/usr/local/nginx/system文件夹与容器/usr/share/nginx/system映射,将vue的dist文件放入/usr/local/nginx/system,容器中nginx.conf指定跳转的目录为/usr/share/nginx/system
nginx 镜像名称

上面的映射的流程是 在启动时 /usr/local/nginx/conf/nginx.conf会复制到/etc/nginx/nginx.conf中

5.nginx配置

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;


    #指定日志路径 
    access_log /var/log/nginx/access.log; 
    error_log /var/log/nginx/error.log;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location ^~/system { #当访问路径为 ip/system时 会访问/usr/share/nginx/system/dist下的静态文件 然后静态文件会调用接口 ajax完成
	            alias /usr/share/nginx/system/dist; #指定dist文件存放路径
            	index index.html;
		        try_files $uri $uri/ /system/index.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

 

你可能感兴趣的:(五分钟带你玩转docker,新星计划)