DEV Community

Cover image for Introduction to Dockerize series
Karan Pratap Singh
Karan Pratap Singh

Posted on • Edited on

Introduction to Dockerize series

Introduction

Hey, welcome to the Dockerize series. Here, I will be discussing how to use docker with your applications during both development and production.

We'll also use techniques like Builder pattern, multi stage builds to optimize our production builds.

This will serve as in introduction to upcoming articles where we'll dockerize our React, Node, Go applications!

Docker...What and Why?

docker

Docker is a software platform for building applications based on containers, which are small and lightweight execution environments.

It also helps to eliminate environment specific issues since you can replicate your production environment locally, which provides consistency across our teams.

Docker compose

docker compose

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the containers from your configuration.

Note: It's is not advised to use docker-compose to run your application in production environment. Use a container orchestration tools like Kubernetes, Openshift, AWS ECS etc.

Builder pattern

In Builder pattern we use a docker image (which usually contains the whole runtime) to create small build artifacts and then use those binaries/artifacts in another smaller image hence reducing size of our built image.

Let's take Node images for example, They're usually upwards of 850mb in general (some slim variants are bit smaller) as they contain the full runtime and other things which might not be useful to us in production. So we can use builder pattern to do the following:

  • Derive from a Node base image with the whole runtime/SDK
  • Copy your source code
  • Install dependencies
  • Produce build artifact/bundle (usually done with bundlers like webpack, parcel) etc
  • Use the built artifacts in a much smaller images like Alpine
  • Push the small image to a repository of your choice like AWS ECS, Dockerhub etc.

Multi stage builds

Multi-stage builds makes it easier to use the builder pattern without hassle of creating multiple files, copying builds to host system and other things we had to do to implement builder pattern. More info here

multistage-builds

I go over builder pattern and multistage builds in this article

We're good to go! See you in the next part!

Top comments (5)

Collapse
 
peteking profile image
Pete King

Looking forward to seeing more, keep it up.

For those who are more .net oriented, my blog series, API's from Dev to Production - Part 1 covers Docker also. Multi-stage builds, optimisation, CVE's and more.

dev.to/newday-technology/api-s-fro...

Collapse
 
karanpratapsingh profile image
Karan Pratap Singh

Thank you, React article is out!! dev.to/karanpratapsingh/dockerize-...

Also, Thanks for sharing the .net impl, I will check it out

Collapse
 
karanpratapsingh profile image
Karan Pratap Singh

Yes, right on! For microservices I use skaffold with minikube...thanks for the feedback, I'll fix it

Collapse
 
outranker profile image
Justin

Why is docker-compose not recommended for production?

Collapse
 
karanpratapsingh profile image
Karan Pratap Singh

Hi, Doing docker-compose up to update (properties of) running containers can result in some downtime, because of the re-creating of the containers. if you have prototype or simple usecase, compose might be okay