add dockerfile

This commit is contained in:
zhangchao
2024-10-31 19:10:55 +08:00
parent c096a94351
commit 4b7192cb59
2 changed files with 58 additions and 13 deletions

View File

@@ -1,24 +1,43 @@
name: Go build
on: [push]
on:
push:
branches: [ master ]
jobs:
build:
name: Build and test
runs-on: ubuntu-latest
container:
image: golang:latest
env:
GO111MODULE: on
GOPROXY: https://goproxy.cn,direct
ports:
- 8080
steps:
- name: Checkout
- uses: actions/checkout@v4
- name: Build
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.21
run: go build -v
- name: Build project
run: |
go build -v ./...
- name: Test with the Go CLI
run: go test
docker:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build Docker image
run: |
docker build -t your-image-name:latest -f ./Dockerfile .
- name: Run Docker container
run: |
docker run -d -p 8080:8080 your-image-name:latest

26
Dokerfile.yaml Normal file
View File

@@ -0,0 +1,26 @@
# 使用官方的 Go 运行时作为基础镜像
FROM golang:1.18-alpine AS builder
# 设置工作目录
WORKDIR /app
# 将当前目录下的所有文件复制到工作目录中
COPY . .
# 构建可执行文件
RUN CGO_ENABLED=0 GOOS=linux go build -v -o app .
# 使用一个更小的基础镜像来减小最终镜像的大小
FROM alpine:latest
# 设置工作目录
WORKDIR /root/
# 从构建阶段复制可执行文件
COPY --from=builder /app/app .
# 暴露端口
EXPOSE 8080
# 运行可执行文件
CMD ["./app"]