| 1 |
# ============================================================================= |
| 2 |
# Multi-stage Dockerfile Example |
| 3 |
# ============================================================================= |
| 4 |
# This is a simple Dockerfile that will build an image of scratch-base image. |
| 5 |
# Usage: |
| 6 |
# docker build -t simple:local . && docker run --rm simple:local |
| 7 |
# ============================================================================= |
| 8 |
|
| 9 |
# ----------------------------------------------------------------------------- |
| 10 |
# Build Stage |
| 11 |
# ----------------------------------------------------------------------------- |
| 12 |
FROM golang:alpine3.18 AS build |
| 13 |
|
| 14 |
# Important: |
| 15 |
# Because this is a CGO enabled package, you are required to set it as 1. |
| 16 |
ENV CGO_ENABLED=1 |
| 17 |
|
| 18 |
RUN apk add --no-cache \ |
| 19 |
# Important: required for go-sqlite3 |
| 20 |
gcc \ |
| 21 |
# Required for Alpine |
| 22 |
musl-dev |
| 23 |
|
| 24 |
WORKDIR /workspace |
| 25 |
|
| 26 |
COPY . /workspace/ |
| 27 |
|
| 28 |
RUN \ |
| 29 |
cd _example/simple && \ |
| 30 |
go mod init github.com/mattn/sample && \ |
| 31 |
go mod edit -replace=github.com/mattn/go-sqlite3=../.. && \ |
| 32 |
go mod tidy && \ |
| 33 |
go install -ldflags='-s -w -extldflags "-static"' ./simple.go |
| 34 |
|
| 35 |
RUN \ |
| 36 |
# Smoke test |
| 37 |
set -o pipefail; \ |
| 38 |
/go/bin/simple | grep 99\ こんにちは世界099 |
| 39 |
|
| 40 |
# ----------------------------------------------------------------------------- |
| 41 |
# Main Stage |
| 42 |
# ----------------------------------------------------------------------------- |
| 43 |
FROM scratch |
| 44 |
|
| 45 |
COPY --from=build /go/bin/simple /usr/local/bin/simple |
| 46 |
|
| 47 |
ENTRYPOINT [ "/usr/local/bin/simple" ] |