Skip to content

Commit c122302

Browse files
KEINOSmattn
authored andcommitted
feat: simple example of Dockerfile w/ multi-stage build
1 parent aa1e904 commit c122302

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

Diff for: .github/workflows/docker.yaml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: dockerfile
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
tags:
7+
- 'v*'
8+
pull_request:
9+
branches: [ master ]
10+
11+
jobs:
12+
dockerfile:
13+
name: Run Dockerfiles in examples
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v2
17+
18+
- name: Run example - simple
19+
run: |
20+
cd ./_example/simple
21+
docker build -t simple .
22+
docker run simple | grep 99\ こんにちわ世界099

Diff for: _example/simple/Dockerfile

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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:alpine 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+
go mod init github.com/mattn/sample && \
30+
go mod tidy && \
31+
go install -ldflags="-s -w -extldflags \"-static\"" ./simple.go
32+
33+
RUN \
34+
# Smoke test
35+
set -o pipefail; \
36+
/go/bin/simple | grep 99\ こんにちわ世界099
37+
38+
# -----------------------------------------------------------------------------
39+
# Main Stage
40+
# -----------------------------------------------------------------------------
41+
FROM scratch
42+
43+
COPY --from=build /go/bin/simple /usr/local/bin/simple
44+
45+
ENTRYPOINT [ "/usr/local/bin/simple" ]

0 commit comments

Comments
 (0)