Docker Essentials & Building a containerized web application

Have your static web application packaged, built and deployed using Docker

Topgyal Gurung
Data Engineering

--

Why Docker over Virtual Machines?

Virtualization with Containers or Docker does not have hypervisor, we have supporting files and libraries which create runtime environment and deeply coupled with operating system kernel. This provides an environment for apps to run directly without having to use hypervisor or guest operating system. This makes containers lightweight and portable.

Click it to watch: Containers and VMs — A Practical Comparison

Photo by Ian Taylor on Unsplash

What is Docker?

OS-level virtualization to deliver software in packages called containers.

What is a container?

simply another process on your machine that has been isolated from all other processes on the host machine.

Containers are created from the images and run the actual applications. Container wrap up entire code. we can think of a container as an isolated system that contains everything needed to run a certain application given a certain setup.

What is a container image?

contains everything needed to run an application — all dependencies, configuration, scripts, binaries etc. Docker images are blueprints of our applications that form the basis for containers.

A Docker Image is built out of a Dockerfile. Docker Images are the output of building a Dockerfile.

Docker registry:

is a repository of docker images. It can be public or private.

Docker Hub:

is the public cloud docker registry

Install Docker for Mac. It includes Docker Engine and Docker-compose

To use Docker, make sure Docker Desktop is running.

To build an Application:

  1. first create Dockerfile inside file:

Inside dockerfile example:

# syntax=docker/dockerfile:1FROM node:12-alpineRUN apk add — no-cache python g++ makeWORKDIR /appCOPY . .RUN yarn install — productionCMD [“node”, “src/index.js”]

2. Then build container image using docker build

$ docker build -t file-name

3. Start an app container:

$docker run -dp 3000:3000 getting-started

4. Open web browser

http://localhost:3000

List running containers

$ docker ps

or

 $ docker ps -a

Stop containers:

$ docker stop < container-id>

Remove container:

 $ docker rm <container-id>

Remove all container:

sudo docker container prune

Display images:

$ sudo docker images 

Remove image forcefully:

$ sudo docker rmi -f <image name/ image_id(prefered)>

Run container in detached mode

$ docker run -d -p 80:80 image-name-d run container in detached mode-p 80:80 — map port 80 of the host to port 80 in containerimage-name — image to use

Build

$ sudo docker build -t static-website . 

Run Your Static Website on Docker:

$ sudo docker run -d -p 80:80 static-website:beta$ sudo docker run -d -P static-website # map to random ports

NOTE: The versatility of docker: two websites running in different ports in the same system. Run multiple instances of same thing and can scale up the containers

Basic docker monitoring commands

$ sudo docker stats

Graphical view of docker containers: use CAdvisor. run cadvisor.sh shell script

$ ./cadvisor.sh$ sudo docker ps

CAdvisor metrics provide insights into performance and CPU and memory spikes as they occur. This data can be used to upgrade the server resources and scale up or down the containers like if we have high traffic and CPU your CPU is spiking memory and scale up number of containers for that particular website by launching more containers.

Share the application:

To share docker images, use the Docker registry. The default registry is Docker Hub.

Sign in to Docker Hub. Create a repository and make visibility is public.

Login to Docker Hub using command:

docker login -u YOUR_USER_NAMEdocker tag getting-started YOUR_USER_NAME/getting-starteddocker push YOUR_USER_NAME/getting-started

Resources:

--

--