前端包docker部署


1,首先得有docker环境,没有的参考我以前的记录

1,创建Dockerfile

touch Dockerfile

vim  Dockerfile

2.写入以下内容

FROM nginx:latest
COPY ./dist /usr/share/nginx/html/
COPY ./default.conf /etc/nginx/conf.d/
EXPOSE 80

意思是使用nginx镜像部署, 把当前路径./dist(前端打包后的静态文件) 拷贝至容器/usr/share/nginx/html/ 把当前路径./default.conf的配置文件拷贝至容器/etc/nginx/conf.d/ 暴露给宿主机的端口为 80端口

3,default.conf配置

server {
    listen 80;
    server_name localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
    # http://192.168.1.2/dwzk/123 => http://192.168.1.2:8080/123
    location /dwzk/ {
        proxy_pass http://192.168.1.2:8080/;
        proxy_set_header Host $http_host;
    }
}

就是普通的nginx配置

4.编写脚本

touch build.sh

vim build.sh

写入以下内容

#!/bin/bash

# 输入镜像名称
read -p "Input your image name:" image_name

# 输入镜像名称
read -p "Input your image version:" image_version

# 构建docker镜像
docker build -t $image_name/nginx:$image_version .

# docker run -d --name $image_name -p 8000:80 $image_name 

# docker save -o $image_name.tar dwzk_web:$image_version

echo "运行镜像命令 docker run -d --name ${image_name} -p 8000:80 ${image_name}"

echo "导出镜像命令 docker save -o ${image_name}.tar dwzk_web:${image_version}"

echo "导入镜像命令 docker load -i tar文件路径"

5,根据提示输入就行