golang打包
大约 2 分钟
Dockerfile
FROM golang:1.21.5-alpine3.19 AS builder
#
WORKDIR /app/
## 设置golang代理
ENV GOPROXY=https://goproxy.cn,direct
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
#RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
# 如果使用sqlite 需要安装一下软件,否则可以忽略
RUN apk add --virtual .build-deps \
ca-certificates \
gcc \
g++ \
musl-dev
COPY . .
RUN go env && go build -o main .
FROM alpine:3.19
WORKDIR /app/
# 修改时区
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# Install tzdata and set timezone to Asia/Shanghai
RUN apk add --no-cache tzdata \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& apk del tzdata
COPY /app/main /app/main
COPY /app/docker.yaml ./config.yaml
COPY /app/web/dist ./web/dist
# 暴露端口号
EXPOSE 3000
# 运行应用程序
CMD ["./main","serve"]
本地打包
docker build -t local/movie-player .
修改为线上版本
docker tag local/movie-player orangbus/movie-player:latest
发布dockerhub
docker push orangbus/movie-player:latest
docker-compose
services:
app:
image: orangbus/movie-player
container_name: movie-player
environment:
- APP_PORT=3000
ports:
- "3000:3000"
volumes:
- ./data:/app/data
docker打包vue项目
Dockerfile
# 使用官方Nginx镜像作为基础镜像
FROM nginx:latest
# 设置工作目录
WORKDIR /usr/share/nginx/html
# 将构建好的Vue项目文件复制到Nginx的webroot目录,假设构建的文件在dist目录下
COPY ./dist /usr/share/nginx/html
# 如果需要自定义Nginx配置,可以复制配置文件到Nginx配置目录
# 例如,将自定义配置文件复制到/etc/nginx/conf.d/
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
# 启动Nginx服务
CMD ["nginx", "-g", "daemon off;"]
nginx.conf
server {
listen 80 default_server;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
location /admin {
proxy_pass http://127.0.0.1:3003; # 修改为服务器端域名或者+端口
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
charset utf-8;
}
github action
.github/workflows/docker-build.yml
name: 打包docker镜像发布
on:
push:
branches:
- master
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: "拉取代码"
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: orangbus/movie-cloud:latest
vue: .github/workflows/deploy.yml
name: Build and Release Vue App
on:
push:
branches:
- master
jobs:
build-and-release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Install dependencies and build
run: |
npm install
npm run build
env:
CI: true
- name: Archive production artifacts
uses: actions/upload-artifact@v2
with:
name: cloud-admin
path: dist/
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.BUILD_TOKEN }}
with:
tag_name: ${{ github.sha }} # 使用当前提交的SHA作为标签名
release_name: Release ${{ github.sha }}
draft: false
prerelease: false
- name: Upload Release Asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.BUILD_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./
asset_name: cloud-admin.zip
asset_content_type: application/zip