Skip to content

Instantly share code, notes, and snippets.

@PurpleBooth
Last active March 21, 2024 09:33
Show Gist options
  • Star 91 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
  • Save PurpleBooth/ec81bad0a7b56ac767e0da09840f835a to your computer and use it in GitHub Desktop.
Save PurpleBooth/ec81bad0a7b56ac767e0da09840f835a to your computer and use it in GitHub Desktop.
Create a static binary in go and put it in a from scratch docker container
FROM golang:1.9
WORKDIR /go/src/github.com/purplebooth/example
COPY . .
RUN go build -ldflags "-linkmode external -extldflags -static" -a main.go
FROM scratch
COPY --from=0 /go/src/github.com/purplebooth/example/main /main
CMD ["/main"]
@liampulles
Copy link

Really useful!

@MajorWalrus
Copy link

This is exactly what I needed. Thank you!

@PurpleBooth
Copy link
Author

PurpleBooth commented Aug 7, 2019

@ChristianKniep wrote:

Just stole this nice little dockerfile for a pet-project. Thanks for putting it out.
Btw: you do not need the first RUN to create the directory. WORKDIR will do that for you if it does not exist. :)

Ooh! Didn't know that! Updated 👍

@liao02x
Copy link

liao02x commented Nov 16, 2019

Got this error when trying to run the container. Any idea? docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"./main\": permission denied": unknown.

@PurpleBooth
Copy link
Author

PurpleBooth commented Nov 16, 2019

Does main exist? The example copies to /main from the build container rather than ./main

Here's another similar example

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