Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. FROM golang:1.11-alpine AS build
  2. WORKDIR /src/
  3. COPY main.go go.* /src/
  4.  
  5. RUN CGO_ENABLED=0 go build -o /bin/demo
  6. FROM scratch
  7. COPY --from=build /bin/demo /bin/demo ENTRYPOINT ["/bin/demo"]
  8.  
  9. # p.53 of 344
  10. # The exact details of how this works don’t matter for now, but it uses a fairly standard build process for Go containers called multi-stage builds.
  11. # The first stage starts from an official golang container image, which is just an operating system (in this case Alpine Linux) with the Go language environment installed.
  12. # It runs the go build command to compile the main.go file we saw earlier.
  13. # The result of this is an executable binary file named demo. The second stage takes a completely empty container image (called a scratch image, as in from scratch)
  14. # and copies the demo binary into it.
  15. # Minimal Container Images
  16. # Why the second build stage? Well, the Go language environment, and the rest of Alpine Linux, is really only needed in order to build the program.
  17. # To run the pro‐ gram, all it takes is the demo binary, so the Dockerfile creates a new scratch container to put it in. The resulting image is very small (about 6 MiB)—and
  18. # that’s the image that can be deployed in production.
  19. # Without the second stage, you would have ended up with a container image about 350 MiB in size, 98% of which is unnecessary and will never be executed.
  20. # The smaller the container image, the faster it can be uploaded and downloaded, and the faster it will be to start up.
  21. # Minimal containers also have a reduced attack surface for security issues. The fewer programs there are in your container, the fewer potential vulnerabilities.
  22. # Because Go is a compiled language that can produce self-contained executables, it’s ideal for writing minimal (scratch) containers.
  23. # By comparison, the official Ruby con‐ tainer image is 1.5 GiB; about 250 times bigger than our Go image, and that’s before you’ve added your Ruby program!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement