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.
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?
It is simple to learn and use.
Deployment process is fast.
Easy Collaboration.
Huge Community Support.
Built for developers, by developers.
Monolithic -> Microservices
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 .
Docker Hub
Manages and stores Docker Images.
2 Types of Registry -
Public Registry - Open for everyone.
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.
Best Practices for writing commands in Docker file
Check if the base image is certified or not, to make sure that it doesn't have any malware.
Expose port at standard ports.
Docker File Components
FROM base-image - If you want to inherit properties of a image, then mention its name here.
ADD source destination - Copy pasting files from source to destination. It can also can new URL to the file system.
COPY source destination - Similar to ADD, but it can't put new URL to the file system.
RUN command - Run specific commands which you want to, during the creation of container.
WORKDIR directory-name - It defines the workspace / working directory. Its used to keep everything organized.
CMD command - Tells container which command to run when container has started.
VOLUME path - It makes mount point for the volume of a specified name.
EXPOSE port - Tells at which port the containers should be exposed at.
ENTRYPOINT command - Similar to CMD, only difference is that it cannot be overridden at runtime.
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 -
Overlay2 - Most used
aufs - Used for old docker versions
devicemapper - For RHEL and CENT-OS.
btrfs - For creating snapshots / backups.
vfs - For testing purposes.
Docker Container states -
Created ( Container Created, but never started )
Running ( Container Started, using docker start command )
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 )
Exited ( When Container terminates )
Pause
Docker Networking
Bridge Used when 2 containers, on the same docker daemon host want to communicate.
Host For standalone containers, remove network isolation between container and docker host, instead use the Host's networking directly.
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 -
Create or Pull a docker Image.
Create a yaml file in which all configurations will be there.
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.
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
No pre-allocation of RAM.
Cost is less. (When compared to virtual machines, containers have a very low cost)
Light - weight. (Light - weight applications are those which require less resources to run)
It can run on a Physical hardware, virtual hardware or on a cloud.
You can re-use the image.
Disadvantages of Docker
Docker is not a good solution for applications that requires rich GUI.
Difficult to manage large amount of container.
Docker does not provide cross - platform compatibility.