Skip to content

Commit 4dc08bc

Browse files
authored
create docker image (#12)
* create initial dockerfile and supporting config * add github workflow for build and push docker image * update dockerfile to support writing SSL certs to tmp * scripts/docker-entrypoint: improve certificate env var names
1 parent 5b6931f commit 4dc08bc

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

.dockerignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
flake.*
2+
**.md
3+
.github
4+
**.example
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Build Push Image
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- master
7+
push:
8+
branches:
9+
- master
10+
11+
permissions:
12+
contents: read
13+
packages: write
14+
15+
jobs:
16+
build-push-image:
17+
name: github container registry
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v4
23+
24+
- name: Setup qemu
25+
uses: docker/setup-qemu-action@v3
26+
27+
- name: Setup buildkit
28+
uses: docker/setup-buildx-action@v3
29+
30+
- id: metadata
31+
name: Set image metadata
32+
uses: docker/metadata-action@v5
33+
with:
34+
images: ghcr.io/${{ github.repository }}
35+
tags: |
36+
type=raw,value=latest,enable={{is_default_branch}}
37+
type=semver,pattern={{version}}
38+
type=semver,pattern={{major}}.{{minor}}
39+
type=ref,event=pr
40+
41+
- name: Login to ghcr.io
42+
uses: docker/login-action@v3
43+
with:
44+
registry: ghcr.io
45+
username: ${{ github.actor }}
46+
password: ${{ secrets.GITHUB_TOKEN }}
47+
48+
- name: Build and push to ghcr.io/${{ github.repository }}
49+
uses: docker/build-push-action@v5
50+
with:
51+
push: ${{ github.event_name != 'pull_request' }}
52+
platforms: linux/amd64,linux/arm64
53+
cache-from: type=gha
54+
cache-to: type=gha,mode=max
55+
tags: ${{ steps.metadata.outputs.tags }}
56+
labels: ${{ steps.metadata.outputs.labels }}

Dockerfile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
FROM --platform=$BUILDPLATFORM golang:1.21 AS build
2+
3+
WORKDIR /go/src/app
4+
ARG TARGETOS
5+
ARG TARGETARCH
6+
ENV CGO_ENABLED=0 \
7+
GOOS=${TARGETOS} \
8+
GOARCH=${TARGETARCH} \
9+
GOCACHE=/cache/go \
10+
GOMODCACHE=/cache/gomod
11+
12+
RUN <<-EOF
13+
go env -w GOCACHE=${GOCACHE}
14+
go env -w GOMODCACHE=${GOMODCACHE}
15+
EOF
16+
17+
RUN --mount=type=bind,source=go.mod,target=/go/src/app/go.mod,readonly \
18+
--mount=type=bind,source=go.sum,target=/go/src/app/go.sum,readonly \
19+
--mount=type=cache,target=${GOCACHE} \
20+
--mount=type=cache,target=${GOMODCACHE} \
21+
--mount=type=cache,target=/go/pkg \
22+
go mod download -x
23+
24+
RUN --mount=type=bind,source=.,target=/go/src/app,readonly \
25+
--mount=type=cache,target=${GOCACHE} \
26+
--mount=type=cache,target=${GOMODCACHE} \
27+
--mount=type=cache,target=/go/pkg \
28+
go build -x -a -ldflags="-w -s" -trimpath -o /go/bin/app ./cmd/migrate
29+
30+
FROM alpine:3.18 AS main
31+
RUN apk add --no-cache ca-certificates
32+
USER nobody
33+
COPY scripts/docker-entrypoint.sh /docker-entrypoint.sh
34+
COPY --from=build /go/bin/app /migrate
35+
ENTRYPOINT ["/docker-entrypoint.sh"]

scripts/docker-entrypoint.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
3+
if test -n "$DATABASE_CLIENT_CERT_B64"; then
4+
echo "$DATABASE_CLIENT_CERT_B64" | base64 -d >/tmp/client-cert.pem
5+
fi
6+
7+
if test -n "$DATABASE_CLIENT_KEY_B64"; then
8+
echo "$DATABASE_CLIENT_KEY_B64" | base64 -d >/tmp/client-key.pem
9+
fi
10+
11+
if test -n "$DATABASE_SERVER_CA_B64"; then
12+
echo "$DATABASE_SERVER_CA_B64" | base64 -d >/tmp/ca.pem
13+
fi
14+
15+
exec /migrate "$@"

0 commit comments

Comments
 (0)