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

  1. 编写 Dockerfile 文件:
bash 复制代码
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 文件:
bash 复制代码
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 打成镜像
bash 复制代码
docker build -t tylerApp:latest .
  1. 运行 tylerApp 镜像
bash 复制代码
docker run -id --name tylerApp -p 80:80 tylerApp
  1. 浏览器访问项目

http://ip:port/


扩展:

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

bash 复制代码
docker save -o tylerApp.tar tylerApp

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

bash 复制代码
tar -czvf tylerApp.tar.gz tylerApp.tar

或者使用 zip:

bash 复制代码
zip tylerApp.tar.gz tylerApp.tar

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

bash 复制代码
docker load -i tylerApp.tar.gz

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

bash 复制代码
tar -xzvf tylerApp.tar.gz
docker load -i tylerApp.tar
相关推荐
分布式存储与RustFS14 小时前
MinIO 官方 Docker 镜像被移除:依赖它的项目该怎么办
docker·云原生·devops·对象存储·minio·分布式存储
玉&心16 小时前
通过Arthas在线诊断K8S中的内存及JVM等使用情况
docker·k8s·arthas
xing-xing16 小时前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
guo_wen_qiang17 小时前
上传本地镜像到harbor中
docker·容器·持续部署
羑悻的小杀马特17 小时前
Dockerfile 全景指南:从入门编写dockerfile到生产环境镜像优化实战,一步步带你从啃透制作指令到镜像制作落地!
docker·dockerfile·镜像制作
BianHuanShiZhe17 小时前
docker常见命令
docker
正经教主19 小时前
【FDE系列】阶段2:Day 43:Docker Compose — 多容器一键编排
人工智能·docker·fde
忆~遂愿20 小时前
本地片库怎么在外面打开?Plex + cpolar 搭一套可远程访问的私人影音库
docker·容器
Zhu75821 小时前
docker环境,vLLM 部署 Qwen3.8-27B
docker·qwen·vllm
溜达的大象21 小时前
多台服务器不想上重型监控?用 Beszel 搭一套轻量主机与 Docker 看板
docker