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
相关推荐
Patrick_Wilson4 天前
从「改个端口」到 502:Next.js on k8s 的容器端口、Service 映射与 env 覆盖
docker·kubernetes·next.js
Suroy5 天前
DockerView-Go:用 Go 写一个终端 Docker 监控工具,顺便做了个 Web 仪表盘
docker
云恒要逆袭5 天前
运行你的第一个Docker容器
后端·docker·容器
宋均浩6 天前
# Docker 镜像瘦身实战:从 1.2G 到 80MB 的五个优化步骤
ci/cd·docker
程序员老赵6 天前
10 分钟部署 OpenCode:Docker 一键安装,浏览器打开就能用 AI 写代码(附完整命令与排错)
docker·容器·ai编程
WangMingHua1116 天前
LM Studio Docker 部署——本地大模型一键启动
docker
曲幽8 天前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate
武子康9 天前
调查研究-183 Apple container:Mac 上用轻量 VM 跑 Linux 容器,Swift 会改写本地容器体验吗?
docker·容器·apple
Alsn8612 天前
等待学习-学习目录:Docker 容器安全攻防
学习·安全·docker
程序员老赵12 天前
服务器没有桌面?Docker 跑个 Chrome,浏览器就能远程用
docker·容器·devops