The latest version of docker has the concept of multi stage builds. Refer: (https://docs.docker.com/engine/userguide/eng-image/multistage-build/)
With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image.