Docker Tutorial
for beginners
Docker is a platform for building, running and shipping applications in a consistent manner.
Reasons why an application will run on a developer machine and not on a target(test or production) machine:
- One or few files are missing causing the application to not be completely deployed
- Software versions mismatches between developer machine and target machines
- Different configuration settings/environment variables
Docker to the rescue
- Docker allows to easily package-up an application with everything it needs and run it anywhere on any machine.
- Docker will also download and run the application and its dependencies inside an isolated environment called a container. This allows multiple applications to use different versions of same software side by side without messing with each other.
- Docker allows to remove the application and its dependencies in one go.
Container is an light weight isolated environment for running an application.
- All the containers running on a host shares the OS of the host.
- Container’s start quickly as the OS has already started on the host.
- Also, recourses need not to be divided for each container and that allows us to run ten’s or hundred’s of containers on a single host.
How is it different from VM’s?
VM’s are an abstraction of a machine or a physical hardware so we can run several VM’s on a real physical machine.
For ex, we can have a mac machine running 2 VM’s. One running windows and other running Linux using Hypervisor( Hypervisor is used to create and manage VM’s).
- VM’s provide isolation as well. We can run same application on different OS with different version of dependencies.
- Each VM needs a full blown OS. They are slow to start as OS needs to start
- VM’s are resource intensive as each VM takes a slice of the actual physical hardware resources like CPU, memory and disk space.
So a 8GB memory has to be divided and allocated to each VM and that limits in terms of the number of VM we can run on a machine.
Docker’s Client-Server Architecture
- A docker client component talks to the server (also called as docker engine) component using RESTful API.
- The docker engine sits in the background and takes care of building and running the docker containers.
- Container is a special kind of process . All the containers running on the host share the kernel(core of the OS) of the host.
Installing docker: https://www.youtube.com/watch?v=pTFZFxd4hOI&t=714s
Commands to test: docker version
Development workflow
- Add a docker file to any application to dockerize it. A docker file is a plain text file and includes instruction that docker uses to package over the application into an image
- This image contains everything our application needs to run. Typically
A cut down OS
A run time environment (ed node oy python)
Application files
Third part libraries
Environment variables and so-on
- Once we have an image we tell that docker to start a container(special kind of process that has it own file system which is provided by the image) using that image. So, our image gets loaded in a container.
- To run it locally on our development machine we tell the docker to run the application inside the container in an isolated environment, instead of running the application directly.
- Once we have an image we can push it to a docker registry like Docker hub. (Dockerhub to docker is same as github to git, a storage of docker images that any one can use)
- Now any test/prod machine can get the get the image. This eliminates the need to log release documents which has to be precisely followed.
For ex: If I am simple JS application which prints hello-docker to screen. To run this application on another machine, the instructions will be
Start with an OS
Install Node
Copy app files
Run node app.js
Now, beauty of docker
- Write this instruction inside a docker file(Dockerfile)and let docker package our application.
FROM node:alpine
COPY ./app (copy all files from the app folder)
WORKDIR /app (make app folder as the working dir)
CMD node app.js
- Now go to the terminal and tell the docker to package up our application
docker build -t hello-docker . (-t is the tag to identify and . is to search in the current directory)
docker image ls
- To run this docker image
docker run hello-docker
- Publish this image on docker hub
docker pull <repo_name>
docker image ls
docker run hello-docker (can be run from any directory)