Docker - Ship me anywhere

Docker provides consistency of development throughout environment


Introduction

Scenario : Sometimes, application works fine on development machine but not on other machines

Probable reason :

  1. One or more files are missing

  2. Target machine is running different version of software your application needs

  3. Target machine's environment variables are different


With docker, we can easily package our application with everything we need for it to run. (ex - Node of same version, mongo of same version etc.)

Reduced external dependency + portability


Provides Isolated environment -- CONTAINER

Two different application with different versions of Node/Mongo/DB/SW can run on same machine

Command : docker-compose up (to download dependencies inside container)

Application and dependencies can be removed in one go.

Command : docker-compose down --rmi all (remove application and its dependencies)


Virtual Machine vs Containers

Virtual Machine - abstraction of a machine (physical hardware)

Multiple OS can run on same VM using tool called Hypervisor (VirtualBox/VMWare/Hyper-v).

Requires actual OS, hardware and is slow to start.


Containers - An isolated environment for running an application

  1. are lightweight.

  2. doesn't contain the OS, instead share the OS of the host.

  3. start quickly.

  4. need less hardware resources.


Architecture of Docker

Docker implements Client-server architecture communication through APIs.

Server here is Docker engine which builds and run Containers.

Single OS dependent containers run on a particular host OS.

On Linux only linux containers

On Windows, both windows and linux containers

On Mac, Linux VM is used to run linux containers.


Development workflow

  • Dockerize an application

Add Dockerfile to the application -- plain text file which contains instruction to package our application to an IMAGE. This IMAGE contains everything our applications need to run.

A cut-down OS

A runtime environment (ex Node)

Application files

Third-party libraries

Environment variables

  • Start a container using the IMAGE

Container is a special type of process.

Command : docker run

We can also push an IMAGE to Registry (Docker hub similar to Github)

It is storage for Images from where IMAGE can be pulled/pushed to any server and run.

Release documents are not required since all instructions are present in Dockerfile.


Detailed Workflow

  • Add Dockerfile (without extension)

Docker file content

from base image, node image is built over different linux distribution image

copy application files

set working directory - where CMD will be executed

tell command to execute

	FROM linux or FROM node:alpine	
	COPY app-directory path
	WORKDIR /app-directory
	CMD node app.js

  • Package our application to an IMAGE

Command : docker build -t image-name Dockerfile-path

Image is created and stored in the computer

This image will consist alpine Linux, node and application files


To see all the images on a computer

Command : docker images OR docker image ls

List all images with REPOSITORY, TAG (for versioning), IMAGE ID, CREATED, SIZE


  • Run docker from anywhere

Command : docker run docker-name

Now this image can be published to the Registry (docker-hub) from where it can be pulled and

run on any computer irrespective of node presence, Linux presence etc.


  • Pull docker

Command : docker pull path-to-registry

Happy Learning!


21 views0 comments

Recent Posts

See All