Skip to content

Commit d9f1e86

Browse files
committed
Containarize application
Provide a multi-stage dockerfile capable of caching dependencies and producing a live image and a development image to help with the development setup, and others. This also comes with a new CI step to validate the (expected) final image can be built.
1 parent 6714b46 commit d9f1e86

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

.github/workflows/push.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,14 @@ jobs:
5151
git status --porcelain
5252
exit 1
5353
fi
54+
55+
container_build:
56+
name: "build container image"
57+
runs-on: ubuntu-latest
58+
59+
steps:
60+
- name: "Fetch source code"
61+
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
62+
- name: "Build container image"
63+
run: |
64+
docker build --output type=docker -t terraform-config-inspect:ci-${{ github.run_id }}-${{ github.run_number }} -f build/Dockerfile .

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,20 @@ $ terraform-config-inspect --json path/to/module
139139
}
140140
```
141141

142+
## Containarized
143+
144+
One can build a container version of this application with the following
145+
oneliner:
146+
`docker build -t terraform-config-inspect:latest -f build/Dockerfile .`
147+
148+
Feel free to use our development stage to debug or when contributing to this
149+
project by including the `--target dev` in the container build phase.
150+
151+
Example:
152+
- How to use the generated image if you current context is the module you want
153+
to analyize:
154+
`docker run -it -v $(pwd):/$(pwd) -w $(pwd) terraform-config-inspect:latest`
155+
142156
## Contributing
143157

144158
This library and tool are intentionally focused on only extracting simple

build/Dockerfile

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
ARG BUILD_GO_IMAGE=golang:1.18-alpine
2+
ARG LIVE_GO_IMAGE=scratch
3+
4+
###
5+
### Setup
6+
###
7+
8+
### Setup Build
9+
FROM ${BUILD_GO_IMAGE} AS setup-build
10+
11+
ARG GOARCH
12+
ARG GOOS=linux
13+
14+
ENV GOARCH=${GOARCH}
15+
ENV GOBIN=${GOPATH}/bin
16+
ENV GOOS=${GOOS}
17+
18+
WORKDIR /usr/local/src
19+
20+
# Ease the debugging and development build by adding the go
21+
# installed binaries to the PATH
22+
ENV PATH=${PATH}:${GOBIN}
23+
24+
# Copy go.mod and go.sum files seperatly to allow caching
25+
# of dependencies
26+
COPY go.* ./
27+
# Download dependencies
28+
RUN go mod download
29+
30+
COPY . .
31+
32+
### Setup Live Image
33+
FROM ${LIVE_GO_IMAGE} AS setup-live
34+
35+
COPY --from=setup-build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
36+
37+
###
38+
### Build
39+
###
40+
FROM setup-build AS build
41+
42+
# Build the application binary
43+
RUN CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} go build -o ${GOBIN}/terraform-config-inspect .
44+
45+
###
46+
### Final Images
47+
###
48+
49+
### Dev
50+
# This stage is used for development and debugging purposes,
51+
# but it can also be used for CI-related tasks, and others.
52+
FROM setup-build AS dev
53+
54+
# Install delve debugger (the delve version shouldn't be a concern 🤷‍♂️)
55+
RUN go install github.com/go-delve/delve/cmd/dlv@latest
56+
57+
# Explicitly not overriding the entrypoint of the source image
58+
# ENTRYPOINT [ "" ]
59+
60+
### Live
61+
FROM setup-live AS live
62+
63+
COPY --from=build /go/bin/terraform-config-inspect /
64+
65+
ENTRYPOINT ["/terraform-config-inspect"]

0 commit comments

Comments
 (0)