Docker

Docker

1. In this Article You will learn the Docker basics, Docker Architecture, Installation of Docker in your local, Microservice Architecture.

·

6 min read

Docker

  • Docker is a software platform that simplifies the process of building, running, managing and distributing applications. It does this by virtualizing the operating system of the computer on which it is installed and running.

  • Docker is a tool designed to make it easier to create, deploy, and run applications by using containers.

Difference Between Virtual Machines and Docker

Docker

Virtual machine

Containers stop working when the “stop command” is executed

Virtual machines are always in the running state 

It has lots of snapshots as it builds images upon the layers

Doesn’t comprise many snapshots

Images can be version controlled; they have a local registry called Docker hub

VM doesn’t have a central hub; they are not version controlled

It can run multiple containers on a system

It can run only a limited number of VMs on a system

It can start multiple containers at a time on the Docker engine

It can start only a single VM on a VMX

Docker Architecture :-

The four key components that make up the entire Docker architecture are -

  1. The Docker Daemon or the server

  2. The Docker Command Line Interface or the client

  3. Docker Registries

  4. Docker Objects -

    1. Images

    2. Containers

    3. Network

    4. Storage

  • Docker Daemon

    creating and managing Docker objects including containers, volumes, images, and networks.

  • Docker CLI

    The Docker users can leverage simple HTTP clients like Command line to interact with Docker. The Docker CLI can also communicate with over one daemon.

  • Docker Registries

    • The official Docker registry called Dockerhub contains several official image repositories. A repository contains a set of similar Docker images that are uniquely identified by Docker tags. Dockerhub provides tons of useful official and vendor-specific images to its users. Some of them include Nginx, Apache, Python, Java, Mongo, Node, MySQL, Ubuntu, Fedora, Centos, etc.

    • You can even create your private repository inside Dockerhub and store your custom Docker images using the Docker push command. Docker allows you to create your own private Docker registry in your local machine using an image called ‘registry’. Once you run a container associated with the registry image, you can use the Docker push command to push images to this private registry.

  • Docker Objects
    A Docker image is simply a blueprint of the container environment. Once you create a container, it creates a writable layer on top of the image, and then, you can make changes. The images all the metadata that describes the container environment. You can either directly pull a Docker image from Dockerhub or create your customized image over a base image using a Dockerfile. Once you have created a Docker image, you can push it on Dockerhub or any other registry and share it with the outside world.

  • Docker Containers

    Docker containers are isolated, encapsulated, packaged, and secured application environments that contain all the packages, libraries, and dependencies required to run an application. For example, if you create a container associated with the Ubuntu image, you will have access to an isolated Ubuntu environment. You can also access the bash of this Ubuntu environment and execute commands.

  • Reasons to use Docker for the Development Environment

    1. Runs on my machine = runs anywhere If you have correctly dockerized your app and it runs without problems on your machine, 99% of the times it will run smoothly anywhere. By anywhere it means on your friend's machine, on the staging environment and production too. Given all of them have docker installed and configured correctly your app will run. Using docker also makes the application code cloud provider agnostic. Your application can potentially run on AWS or GCP or Azure without issues.

    2. New team member can be productive from day 1 Think of this, a new team member joins then s/he spends more than a day to set up the machine with the right OS. Setup the language(s) used in the company add database(s) on top of it. 2-3 days is wasted on just getting the environment setup correctly. Enter docker + docker-compose, the new joiner sets up the OS. Installs docker then runs 3-5 commands, grabs some coffee and magic: your apps(s) are running. The new joiner can contribute with the working code on day 1. Think of all the cost a company can save with this approach. A streamlined docker implementation makes it a reality.

How To Install Docker?

https://docs.docker.com/get-docker/

Some Docker Commands:-

docker version
docker pull nginx                                    #Pull Nginx
docker run --name docker-nginx -p 80:80 nginx        #Expose Nginx 80 Port
docker run --name docker-nginx -p 8080:80 -d nginx   #Expose 8080
docker run -P nginx                                  # run on random Port
docker run -d -P nginx                               # detech mode

  • ngnix server running on 80 port Successfully.

8080:80 :- 1. 8080 is Random Port where You can redirect the traffic.

2. 80 on this port your container is running

docker run --name docker-nginx2 -p 80:80 -d nginx

-d is deteach mode , means your container still running if you close the terminal, logs are generated at the backend


More imp Commands:-

docker build -t friendlyname .              # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyname          # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyname       # Same thing, but in detached mode
docker run --name test-ubuntu -it ubuntu:16.04 ./bin/bash 
docker exec -it [container-id] bash         # Enter a running container
docker ps                                   # See a list of all running containers
docker stop <hash>                          # Gracefully stop the specified container
docker ps -a                                # See a list of all containers, even the ones not running
docker kill <hash>                          # Force shutdown of the specified container
docker rm <hash>                            # Remove the specified container from this machine
docker rm $(docker ps -a -q)                # Remove all containers from this machine
docker images -a                            # Show all images on this machine
docker rmi <imagename>                      # Remove the specified image from this machine
docker rmi $(docker images -q)              # Remove all images from this machine
docker logs <container-id> -f               # Live tail a container's logs
docker login                                # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
docker push username/repository:tag         # Upload tagged image to registry
docker run username/repository:tag          # Run image from a registry
docker system prune                         # Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes. (Docker 17.06.1-ce and superior)
docker system prune -a                      # Remove all unused containers, networks, images not just dangling ones (Docker 17.06.1-ce and superior)
docker volume prune                         # Remove all unused local volumes
docker network prune                        # Remove all unused networks

cd usr/share/nginx/html/

docker volume create my_vol                 # Create a volume
docker volume ls
docker volume inspect my_vol                # troulbeshooting
docker volume rm my_vol



##Setup Docker in EC2
Allows access to port 80 (HTTP) from anywhere
HTTP  TCP  80 Anywhere
sudo yum update -y
sudo yum install -y docker
sudo service docker start
sudo usermod -aG docker ec2-user

Microservice Architecture:

Defination:-

  • It is a framework which consists of small, individually deployable services performing different operations.

  • Microservices focus on a single business domain that can be implemented as fully independent deployable services and implement them on different technology stacks.

Difference :-

Learn How the Microservices are works:-

Thanks..