Docker 使用 Dockerfile 文件打包部署前端项目

  1. 编写 Dockerfile 文件:
FROM nginx:latest
ADD dist /etc/nginx/html/dist
COPY nginx.conf /etc/nginx/nginx.conf
ENV PATH /usr/sbin:$PATH
EXPOSE 80
ENTRYPOINT ["nginx"]
CMD ["-g","daemon off;"]
  1. 编写 nginx.conf 文件:
user  root;
worker_processes  auto;

events {
    worker_connections  1024;
}

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

    sendfile        off;
    tcp_nopush     on;
    server_tokens   off;
    keepalive_timeout  65;

    client_max_body_size 1000m;
    client_header_buffer_size 512k;
    large_client_header_buffers 4 512k;

    server {
        listen       80;
        server_name  localhost;

        gzip on;
    	gzip_min_length 1k;
    	gzip_buffers 4 16k;
    	gzip_comp_level 6;
    	gzip_vary on;
    	gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;

        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        location / {
            add_header X-Frame-Options "SAMEORIGIN";
            add_header Cache-Control "no-cache";
            root    html/dist;
            index   index.html;
            try_files $uri $uri/ /index.html;
        }

        location /new/oliver/ {            
            proxy_pass http://127.0.0.1:9090;
            rewrite "^/new/(.*)$" /$1 break;
        }

        location /oliver/ {
          proxy_http_version 1.1;
          proxy_pass http://172.30.10.141:11213;
          #proxy_pass_request_headers on;
          #proxy_set_header Host $http_host;
          #proxy_set_header Connection "";

          client_max_body_size    1024m;
          client_body_buffer_size 128k;

          proxy_redirect off;
          proxy_connect_timeout   900;
          proxy_send_timeout      900;
          proxy_read_timeout      900;
          proxy_buffer_size       4k;
          proxy_buffers           4 32k;
          proxy_busy_buffers_size 64k;
          proxy_temp_file_write_size 64k;

          proxy_next_upstream http_502 http_504 error invalid_header;
        }

        location /zhong/ {
            proxy_pass http://api.oliver.com;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            rewrite "^/zhong/(.*)$" /$1 break;
        }

        location /tyler/ {            
            proxy_pass http://192.168.56.200:8999;
            rewrite "^/tyler/(.*)$" /$1 break;
        }

        location /ai {            
            proxy_pass https://tylerzhong.ai.com;
        }
    }
}
  1. 将 Dockerfile 打成镜像
docker build -t tylerApp:latest .
  1. 运行 tylerApp 镜像
docker run -id --name tylerApp -p 80:80 tylerApp
  1. 浏览器访问项目

http://ip:port/


扩展:

如果要将打包好的镜像发送给别人,让别人部署,则可以将镜像打成 tar 包,然后将 tar 包发送给别人。

docker save -o tylerApp.tar tylerApp

由于 Docker 镜像文件可能比较大,可以将 .tar 文件进行压缩以减少文件大小,便于发送。可以使用 tar 或 zip 等工具压缩:

tar -czvf tylerApp.tar.gz tylerApp.tar

或者使用 zip:

zip tylerApp.tar.gz tylerApp.tar

对方收到镜像文件后,需执行以下命令来加载 Docker 镜像:

docker load -i tylerApp.tar.gz

如果是压缩文件,需要先解压:

tar -xzvf tylerApp.tar.gz
docker load -i tylerApp.tar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值