Docker Docs

Docker Docs

Docker Docs

Docker Docs - Best Practices for Dockerfiles

Let’s talk about best practices for writing Dockerfiles.

In the last years, we are experiencing improvements on the way Dockerfiles should be structured.

We came from the simple Dockerfile with everything in one image to the multi-stage image build.

FROM ubuntu:18.04
COPY . /app
RUN make /app
CMD python /app/

Instead of using this approach, the tip here is to use Use multi-stage builds

Here is one example of a Dockerfile for a Go application, could be like the following:

FROM golang:1.11-alpine AS build

# Install tools required for project
# Run `docker build --no-cache .` to update dependencies
RUN apk add --no-cache git
RUN go get

# List project dependencies with Gopkg.toml and Gopkg.lock
# These layers are only re-built when Gopkg files are updated
COPY Gopkg.lock Gopkg.toml /go/src/project/
WORKDIR /go/src/project/
# Install library dependencies
RUN dep ensure -vendor-only

# Copy the entire project and build it
# This layer is rebuilt when a file changes in the project directory
COPY . /go/src/project/
RUN go build -o /bin/project

# This results in a single layer image
FROM scratch
COPY --from=build /bin/project /bin/project
ENTRYPOINT ["/bin/project"]
CMD ["--help"]

In the docker docs documentation you can check the best practices when it comes to the latests.

Please check it at Docker Documentation

Please, follow our social networks:

Thank You and until the next one! 😉👍

Published on Nov 15, 2020 by Vinicius Moll

Share on / compartilhe: