Docker Learning Note(vi)

Image for post
Image for post
Photo by Joshua Sukoff on Unsplash

In this blog we are looking at the common patterns for Docker.

In Docker we always want to focus on two issues: size and surface area. A way to solve this is the Build Pattern.

A builder pattern refers to having two Dockerfiles:

  • one is for development. It contains everything for building the application
  • one is for production. It contains only necessary elements for the container to run.

A script file is needed like which builds the development version first and copies the necessary layers or files into some folder and build the production version from there with only the files we need.

This is nice but we have the hassle to maintain two docker files, a shell script all the time. That’s why Multi-Stage Builds is introduced.

Docker 17.05 introduced multi-stage builds, which allow you to use multiple FROM statements in a single Dockerfile.

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.

For a common node-based Docker file:

Docker-compose file after multi-stage build

We ran the following commands to build the dev image:

To build for production, pass in a different params:

You can also build it imperatively with the flag — target:

The from command is not only limited to build from the current image. We can use — from flag with external docker images as well.

🛥 🚤 ⛴ 🛳 ⛵ ⚓️ 🌊🛥 🚤 ⛴ 🛳 ⛵ ⚓️ 🌊🛥 🚤 ⛴ 🛳 ⛵ ⚓️ 🌊🛥 🚤 ⛴ 🛳 ⛵ ⚓️ 🌊🛥 🚤 ⛴ 🛳 ⛵ ⚓️ 🌊🛥 🚤 ⛴ 🛳 ⛵ ⚓️ 🌊🛥 🚤

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store