Getting Started With Docker

Why Docker?

  • Makes it easy to setup projects locally
  • Most big companies/apps deploy their apps to the internet via docker

What are we learning today?

  • What is containerization?
  • History of Docker
  • How to install docker
  • containers vs images
  • Creating simple full stack app, dockerfile
  • Containerizing the backend
  • Deploying to dockerhub
  • Pulling an image and deploying it to the internet via AWS EC2 machine

Why Containerization?

  • Everyone has different Operating systems
  • Steps to run a project can vary based on OS
  • Extremely harder to keep track of dependencies as project grows
  • What if there was a way to describe your projects configuration in a single file?
  • What if that could be run in an isolated environment
  • Makes Local setup of OS projects a breeze
  • Makes installing auxiliary services/DBs easy

What is Containerization?

Containerization involves building self-sufficient software packages that perform consistently, regardless of the machines they run on. It’s basically taking the snapshot of a machine, the filesystem and letting you use and deploy it as a construct.

History of Docker

  • Introduced in 2014
  • Caught on fire 2015 onwards
  • Most open source projects have docker files
  • Makes your life easy when you’re setting up the project locally
  • Makes it easier to deploy containers
  • Allows for container orchestration which makes deployment a breeze

Installing Docker

Please visit : Install Docker Engine | Docker Docs and follow the steps

Inside Docker

Docker has 3 main parts,

  1. CLI
  2. Engine
  3. Registry(docker hub)

Images vs Containers

Images is like an iso file which we used back in those days to install OS or games. whereas containers are which runs and executing the image locally.

Images comes with filesystem, codebase, networking informations.

Multiple containers can be executed and run with an image. Incase of scaling the app one can run multiple containers so that more people can hit the app without more load on the machine.

Creating a simple full stack app

just an index.html and index.js for backend route.

Containerizing the app, Dockerfile

docker login

docker build . -t <image_tag>

docker run <image_tg>

port mapping:

docker run -p 3000:3000 <image_tag>

Deploying to dockerhub

docker login

Create a new repository in dockerhub and then run, for example

docker build . -t vasanth/test

docker push <image_tag>

docker pull <image_tag>

To kill running processes

docker ps

docker stop $(docker ps -q) //Stops all currently running containers

docker rm $(docker ps -a -q) //Removes all the stopped containers

Pulling an image and deploying to the internet via AWS EC2 instance

Let us use AWS ec2 instance,

I have created the ec2 instance and let us ssh into the machine/computer running somewhere in the world. This machine does not comes with node installed so it can be used as a good example.

It also doesn’t have docker installed. so, let us install docker in that machine,

sudo yum install -y docker

Start the docker service,

sudo service docker start

Add user to docker group,

sudo usermod -a -G docker ec2-user

login to the docker account,

docker login

pull the image and execute the container,

docker pull <image_tag>

run the image,

docker run <image_tag>

port mapping,

docker run -p 3000:3000 <image_tag>

By this way the app can be easily deployed on the internet by using docker.

Thank you!



Leave a Reply

Your email address will not be published. Required fields are marked *