Docker, the Hero we all deserved

Docker, the Hero we all deserved

Docker is a container platform that allows you to build, test, and deploy applications quickly. Container - Box in which application is running ( Isolated environment ). This isolation and security allows you to build many containers simultaneously on a given host.

container - vm.png

Docker Ecosystem -

1. Docker Daemon / Engine / Server - ( Brain of Docker ) - It will create the docker image, convert the docker image to the container. Also pulls the image from the Docker hub.

2. Docker Images - It's like a template. It defines the container.

Docker Images consists of collection of files / layers. These images are Read only.

You can either Build the image writing a dockerfile or download it from docker hub.

3. Docker Hub - Storage where docker images are stored.

4. Docker client - Terminal where we write our commands, create files. Its used to interact with the Docker Daemon / Engine. It can communicate with more than 1 Daemon. Docker users can interact with the Docker Daemon / Engine either through this Docker Client or REST API.

Why docker?

Why docker meme.jpg

  1. It is simple to learn and use.

  2. Deployment process is fast.

  3. Easy Collaboration.

  4. Huge Community Support.

  5. Built for developers, by developers.

Monolithic -> Microservices

Monolithic vs Microservices.jpg

Earlier we used to have Monolithic applications wherein all components(like frontend, backend, database) were bundled together and run as a single component. The problem with this was if you want to make an update in any individual component or scale it, then you've to make changes in other components as well because these components are dependent on each other. This also means that if one component fails then everything fails -> application goes down.

To solve this problem, Microservices was introduced.

Here, all the components are deployed individually. So if we want to make a change / scale in a component then we can do it without touching other components. These components communicate with each other & also we can manage it using Orchestrators (like Kubernetes, Docker Swarm).

Now, these components are individually deployed in containers which gives us a lot more flexibility.

Two ways to create a Docker Image -

1. Pull Image from Docker Hub

Docker Hub is a place where you'll find many ready-made docker images. You can download it using docker pull .

Pull Image.png

Docker Hub

Manages and stores Docker Images.

2 Types of Registry -

  1. Public Registry - Open for everyone.

  2. Private Registry - A company buys it for storing Docker Images for their employees. They just want their images to be NOT accessed by anyone who doesn't have the permission.

2. Create Image from the Docker file.

Write Dependencies in a Docker file -> Run it through the Docker client -> Docker Daemon / Engine will create the image.

Docker Containers - It has entire packages that is needed to run the application.

Docker file.png

Best Practices for writing commands in Docker file
  1. Check if the base image is certified or not, to make sure that it doesn't have any malware.

  2. Expose port at standard ports.

file - image - container.png

Docker File Components
  1. FROM base-image - If you want to inherit properties of a image, then mention its name here.

  2. ADD source destination - Copy pasting files from source to destination. It can also can new URL to the file system.

  3. COPY source destination - Similar to ADD, but it can't put new URL to the file system.

  4. RUN command - Run specific commands which you want to, during the creation of container.

  5. WORKDIR directory-name - It defines the workspace / working directory. Its used to keep everything organized.

  6. CMD command - Tells container which command to run when container has started.

  7. VOLUME path - It makes mount point for the volume of a specified name.

  8. EXPOSE port - Tells at which port the containers should be exposed at.

  9. ENTRYPOINT command - Similar to CMD, only difference is that it cannot be overridden at runtime.

  10. LABEL - Used to add Meta Data to the Docker image.

Docker Storage -

Normally, if you want to store data in docker container, it would be stored in the writable layer of docker container. But this is NOT an efficient, way to store data. So we make use of different storage types.

Docker Storage Types

1. Volumes - It's a Persistent storage locations for containers. It is managed by Docker completely. It can be easily attached and removed from container. Data inside volumes can be over-riden.

2. Bind mount - Similar to volumes, only difference is that it is NOT managed completely by Docker. They just reference the data.

3. Tmpfs mount - Used for temporary storing of data, without affecting container's performance. Use and Throw mechanism. When container lifecycle ends, data inside tmpfs mount also ends. You cannot share this data with other containers, because it works only with 1 container. Also it works with only linux based OS containers.

Its Advantages

1. Persistent Data.

2. Transfer Data easily.

3. Increase container performance.

Storage Drivers

Helps us to maintain images and containers. It sees where these things can be stored properly.

Different storage drivers available -

  1. Overlay2 - Most used

  2. aufs - Used for old docker versions

  3. devicemapper - For RHEL and CENT-OS.

  4. btrfs - For creating snapshots / backups.

  5. vfs - For testing purposes.

Docker Container states -

  1. Created ( Container Created, but never started )

  2. Running ( Container Started, using docker start command )

  3. Restarting ( Rule can be set to specify when the container will restart, by default rule -> Don't start, Other rules -> On-failure restart, always restart, Restart unless stopped )

  4. Exited ( When Container terminates )

  5. Pause

Docker Networking

  1. Bridge Used when 2 containers, on the same docker daemon host want to communicate.

  2. Host For standalone containers, remove network isolation between container and docker host, instead use the Host's networking directly.

  3. Overlay When multiple docker daemons want to communicate.

Docker Compose

Service within docker that let's us launch multiple containers at the same time.

3 steps to do it -

  1. Create or Pull a docker Image.

  2. Create a yaml file in which all configurations will be there.

  3. Execute the yaml file.

Docker Swarm

Orchestration service within docker that allows us to manage and handle multiple containers, at the same time.

Docker Commands -

1.docker run -it --name container-name image-name /bin/bash -> To create and start a Container.

Run Image.png

2. docker images - To see all images present in your local machine.

3. docker pull - To download image from the docker Hub to local machine of a particular OS and its version. If you don't mention the version then it will download the latest version.

4. service docker status - To check if a service has started or not.

5. docker start - To start container

6. docker stop - To stop container

7. docker rm - To delete container

8. docker rmi - To delete image

9. docker attach - To go inside a container

10. docker ps -a -> To see all containers. ( ps -> Process status, -a -> all, both running containers and stopped containers )

11. docker ps - To see only the running containers.

12. docker search - To find out images in the docker hub related to a particular operating system.

Advantages of Docker

  1. No pre-allocation of RAM.

  2. Cost is less. (When compared to virtual machines, containers have a very low cost)

  3. Light - weight. (Light - weight applications are those which require less resources to run)

  4. It can run on a Physical hardware, virtual hardware or on a cloud.

  5. You can re-use the image.

Disadvantages of Docker

  1. Docker is not a good solution for applications that requires rich GUI.

  2. Difficult to manage large amount of container.

  3. Docker does not provide cross - platform compatibility.