Advanced Dockerfile techniques
Dockerfiles are used to define how an application should be built inside a Docker container. We covered most of the available commands in Chapter 8. Here, we will introduce more advanced techniques, such as multi-stage builds or not-so-common ADD
command uses.
Multi-stage build
Multi-stage builds are a feature of Docker that allows you to use multiple Docker images to create a single final image. By creating multiple stages, you can separate the build process into distinct steps and reduce the size of the final image. Multi-stage builds are particularly useful when building complex applications that require multiple dependencies as they allow developers to keep the necessary dependencies in one stage and the application in another.
One example of using multi-stage builds with a Golang application involves creating two stages: one for building the application and one for running it. In the first stage, the Dockerfile pulls in the necessary dependencies...