chris-crone / containerized-go-dev Goto Github PK
View Code? Open in Web Editor NEWContainerize your Go development environment!
License: The Unlicense
Containerize your Go development environment!
License: The Unlicense
first of all thank you for sharing this system,
I tried this system but unable to use for unit-test target with exporting test coverage output file for use with CI system.
I did mount the folder --output
go test -coverprofile=/out/cover.out ./...
any idea what could be missing here ?
My application creates files and in my test I check whether the creation was successful or not. with go test
everything is fine, but with the containerised build I get the error of
read-only file system
I tried it with setting the mount to "bind"
FROM base AS unit-test RUN --mount=target=. \ --mount=type=bind,target=/root/.cache/go-build \ go test -v .
but without success.
Any hints for me?
If I bumb the FROM to version 1.16, the docker build fails with the following message:
> [build 4/4] RUN go build -o /out/example .:
#8 0.337 go: cannot find main module
Can be fixed by changing the RUN in the dockerfile:
RUN go env -w GO111MODULE=auto && go build -o /out/example .
Maybe I've been doing it wrong all my life, but what's the point of building first, before testing or linting ?
I usually see linting first, then tests, and if everything is fine (which should produce a working binary), build.
Sorry to ask than in an issue... :)
BTW, great work explaining all these features
All of my images require vips so I can't use a scratch (I don't think..). As such, I'm running with Alpine:
Sorry for the mess of docker files. I need to switch back to using a build-base and base images.
# syntax = docker/dockerfile:1-experimental
FROM --platform=${BUILDPLATFORM} golang:1.16.3-alpine3.13 AS build
RUN apk update
RUN apk upgrade
RUN apk add gcc g++
RUN apk add --update --no-cache --repository https://dl-3.alpinelinux.org/alpine/edge/community vips-dev
RUN apk --no-cache --update add ca-certificates --repository https://dl-3.alpinelinux.org/alpine/edge/main vips
WORKDIR /src
COPY go.mod .
COPY go.sum .
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
RUN --mount=target=. \
--mount=type=cache,target=/root/.cache/go-build \
GOOS=linux CGO_ENABLED=1 \
go build -a -installsuffix cgo -o /out/api-server ./server/cmd/api/.
FROM --platform=${BUILDPLATFORM} alpine:latest AS application
RUN apk update
RUN apk add --update --no-cache --repository https://dl-3.alpinelinux.org/alpine/edge/community vips-dev
RUN apk --no-cache --update add ca-certificates --repository https://dl-3.alpinelinux.org/alpine/edge/main vips
WORKDIR /app
COPY --from=build /out/api-server .
ENTRYPOINT [ "./api-server" ]
Which definitely works, (thank you! the build times can definitely creep up on Go without caching in conjunction with cgo).
The only issue I'm running into is that because I need alpine, the payload for copying back to the client is 227.21MB which happens in full, every time.
▶ docker build -f server/cmd/api/Dockerfile . --output build/api --target application --platform local
[+] Building 21.9s (25/25) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 37B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> resolve image config for docker.io/docker/dockerfile:1-experimental 0.4s
=> CACHED docker-image://docker.io/docker/dockerfile:1-experimental@sha256:600e5c62eedff338b3f7a0850beb7c05866e0ef27b2d2e8c02aa46 0.0s
=> [internal] load metadata for docker.io/library/alpine:latest 0.0s
=> [internal] load metadata for docker.io/library/golang:1.16.3-alpine3.13 0.0s
=> [build 1/11] FROM docker.io/library/golang:1.16.3-alpine3.13 0.0s
=> CACHED [build 2/11] RUN apk update 0.0s
=> CACHED [build 3/11] RUN apk upgrade 0.0s
=> CACHED [build 4/11] RUN apk add gcc g++ 0.0s
=> CACHED [build 5/11] RUN apk add --update --no-cache --repository https://dl-3.alpinelinux.org/alpine/edge/community vips-dev 0.0s
=> CACHED [build 6/11] RUN apk --no-cache --update add ca-certificates --repository https://dl-3.alpinelinux.org/alpine/edge/mai 0.0s
=> [application 1/6] FROM docker.io/library/alpine:latest 0.0s
=> [internal] load build context 2.3s
=> => transferring context: 2.25MB 2.2s
=> CACHED [application 2/6] RUN apk update 0.0s
=> CACHED [application 3/6] RUN apk add --update --no-cache --repository https://dl-3.alpinelinux.org/alpine/edge/community vips 0.0s
=> CACHED [application 4/6] RUN apk --no-cache --update add ca-certificates --repository https://dl-3.alpinelinux.org/alpine/edge 0.0s
=> CACHED [application 5/6] WORKDIR /app 0.0s
=> CACHED [build 7/11] WORKDIR /src 0.0s
=> CACHED [build 8/11] COPY go.mod . 0.0s
=> CACHED [build 9/11] COPY go.sum . 0.0s
=> CACHED [build 10/11] RUN go mod download 0.0s
=> CACHED [build 11/11] RUN --mount=target=. --mount=type=cache,target=/root/.cache/go-build GOOS=linux CGO_ENABLED=1 go bu 0.0s
=> CACHED [application 6/6] COPY --from=build /out/api-server . 0.0s
=> exporting to client 18.7s
=> => copying files 227.21MB 18.6s
I'm assuming that I need the files copied back for caching and build purposes, which is fine. However, is there a way I can cherry pick what comes back? Is that feasible?
Thanks again,
Chance
Hi Chris @chris-crone
thanks a lot for your open project and the content in your blogs.
Maybe you also like to check my repo at https://github.com/TomFreudenberg/golang-cli-cmd-dev-starter
It may give some additional points.
Cheers
Tom
Hi,
First of all, thank you for providing this repo as an example of a containerized Go dev environment!
When launched this repo as a Dev Environment in Docker Desktop 3.5.2 (66501) and opened in VSCode, both go
and make
are missing from the container image:
vscode ➜ /com.docker.devenvironments.code (main) $ go version
bash: go: command not found
vscode ➜ /com.docker.devenvironments.code (main) $ make
bash: make: command not found
Could it be because the main language of this repository is Dockerfile
instead of Go
?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.