Skip to content

Instantly share code, notes, and snippets.

@maxfunke
Created July 3, 2018 10:23
Show Gist options
  • Save maxfunke/e6be41124c0457c35e45c3a1290cb01a to your computer and use it in GitHub Desktop.
Save maxfunke/e6be41124c0457c35e45c3a1290cb01a to your computer and use it in GitHub Desktop.
Golang - multistage docker images

How to build and run sources

build image from latest sources

docker build .
run sources (from docker container, will be deleted after exits)

# --rm deletes container after it exits
# -t for tty
# -i for interactive

docker run --rm -ti maxfunke/go-training
# Multistage build
FROM golang:alpine as build
## stage1: build app, make use of /go/src
RUN mkdir /go/src/app
WORKDIR /go/src/app
## copy sources to build-image
ADD ./src/main.go /go/src/app
RUN go build -o /bin/app
## stage2: copy built bin to plain-image
FROM scratch
COPY --from=build /bin/app /bin/app
ENTRYPOINT ["/bin/app"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment