Skip to content

Instantly share code, notes, and snippets.

@mwmahlberg
Last active November 23, 2019 17:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwmahlberg/6af4cb8edc56a517b599a59a6b1b767e to your computer and use it in GitHub Desktop.
Save mwmahlberg/6af4cb8edc56a517b599a59a6b1b767e to your computer and use it in GitHub Desktop.
FROM golang:1.12.0
ENV SRC_DIR=/go/src/vk_acceptor/
ADD . $SRC_DIR
# 1. Use workdir instead of `cd`, as cd is only valid for the current `RUN`.
WORKDIR $SRC_DIR
# 2. Use /usr/local/bin (or sbin) as per FHS. Adhere to the principle of least suprise - this is what you expect, too, or don't you?
# 3. Use `&&` instead of `;` to connect commands to ensure that the following command is only exeecuted
# if the preceding command was successfull. If not, the whole command will return a non-zero exit code.
RUN export GO111MODULE=on && env && go env && go get -u && go build -o application && cp application /usr/local/bin/ && chmod +x /usr/local/bin/application
# Use something according to FHS instead! Most likely, somthing under /var
WORKDIR /app
ENTRYPOINT ["/usr/local/bin/application"]
FROM golang:1.12-alpine as build
ENV SRC_DIR=/go/src/vk_acceptor/
ADD . $SRC_DIR
WORKDIR $SRC_DIR
RUN export GO111MODULE=on && go get -u && go build -o application
FROM alpine:3.10
ENV SRC_DIR=/go/src/vk_acceptor/
COPY --from=build $SRC_DIR/application /usr/local/bin/application
# With workdir, you can denote where application will be relative
# in the filesystem. Say you start in tmp, and create a file "foo",
# it will be created in "/tmp/foo".
WORKDIR /tmp
ENTRYPOINT ["/usr/local/bin/application"]
@mwmahlberg
Copy link
Author

The Dockerfile.twostage is a simple example of creating a slimmer docker image, getting rid of all the build time dependencies. Furthermore, I used an alpine base image here, as this produces smaller images, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment