Docker Learning Note(vi)

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:

FROM node:12-alpine as base
COPY package.json package-lock.json /src/
COPY . /src
FROM base as production
ENV NODE_ENV=production
RUN npm install --production
CMD ["node", "index.js"]
FROM base as dev
ENV NODE_ENV=development
RUN npm config set unsafe-perm true && npm install -g nodemon
RUN npm install
CMD ["npm", "start"]

Docker-compose file after multi-stage build

version: '3.5'
context: ./
target: dev
- .:/src
command: npm start
- "8080:8080"
VIRTUAL_HOST: 'currency.test'

We ran the following commands to build the dev image:

docker build . -t currency-api-dev -target=dev

To build for production, pass in a different params:

docker build . -t currency-api-dev -target=production

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

docker build --target base -t myApp:v1 .

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

COPY --from=busy-box:latest

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




Hi :)

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

HackTheBox Legacy writeup w/o metasploit

Becoming a Developer | Learning HTML + CSS

Industry Use Cases for Kubernetes / Openshift from Experts.

{UPDATE} Word Tiles: Relax n Refresh Hack Free Resources Generator


Queues behind the scenes

What is fluent validation and what are its advantages?

How I leaned python in 15 days

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


Hi :)

More from Medium

Installing Docker on Ubuntu 21.10

How to Schedule Recurring Jobs or Tasks Using Celery

Communication between two flask services using rabbitmq

Separating application and worker containers in Celery 5