Docker Architecture and it’s component
Before going into the docker let’s understand about Virtualization
- Virtualization is a combination of software and hardware that creates Virtual Machines (VMs)
- Basically, it is the abstraction of the computer hardware that allows the creation of multiple machines from a single machine.
How Does Virtualization Work?
- We have sets of layers to understand the virtualization technology, first is the Hardware that includes CPU, RAM, Storage, Network, Drive, etc.
- On top of hardware, we have an operating system that manages all the software and hardware on the computer. it is the interface between the computer hardware and users and controls the execution of the program.
- The hypervisor is the piece of software that is installed on the top of the OS that enables for creating virtual machines and then managing, allocating resources to them.
- With the help of the hypervisor, we can run the many applications on the top of the guest OS that interacts and uses actual (host) hardware, such as CPU RAM hard disk, etc.
- We have many tools for creating virtual machines. VirtualBox, VMware ESXi, Microsoft Hyper-V which helps for creating VMs.
Advantage of the containers over virtualization
- Containers are very lightweight and very fast if you create a virtual machine it can take 1–2 minutes but docker can spin up a few seconds from the container image.
- Docker containers are process-isolated and don’t require a hardware hypervisor. Docker containers are much smaller and require fewer resources than a VM.
- Portability: Containers are highly portable. it’s easy to move containers around, even across environments, such as the public cloud, private cloud, and hybrid cloud
- Easier to maintain: As containers use a microservices-based architecture, your code is broken down into manageable pieces that can be handled individually. Hence, you can update and maintain a container without worrying it will affect other parts of your application.
- Highly Scalable: Container orchestration platforms are created to help you manage your containers. Container orchestrators, like Kubernetes or Docker Swarm, automate most of your container management process, including scaling, networking, and deployment.
What is Docker?
- Started on 20 March 2013, as an open-source project by a hosting company called dot cloud.
- The platform for virtualization with multiple Operating systems running on the same host.
- Docker enables you to separate your applications from your infrastructure.
- It helps to separate infrastructure and applications in order to deliver software quickly.
As you can see the difference in the image above, In the place of hypervisor we have docker-engine which creates and manage the docker containers.
Docker containers run as an isolated environment on top of the host’s Operation system. This helps you to improves efficiency and security. Moreover, we can run more containers on the same infrastructure than we can run Virtual machines because containers use fewer resources.
Even if your host runs Windows OS, you can have Linux images running in containers by pulling the docker images from the Docker Hub.
Docker is the client-server type of application which means we have clients who relay to the server.
There are a few major components in the Docker architecture:
- Docker Client When a client (user) runs any docker command on the docker client terminal, the client terminal sends these docker commands to the Docker daemon with the help of docker REST API. The Docker client and daemon communicate using a REST API, over a network interface.
- Docker Engine: Docker daemon is also called dockerd, which listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes. A daemon can also communicate with other daemons to manage Docker services.
- Docker Host provides a complete environment to execute and run applications. It comprises the Docker daemon, Images, Containers, Networks, and Storage.
- Docker Registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to use images on Docker Hub by default. You can run your own registry on it. you can create one private free repository when you create an account on the Docker hub.
- Dockerfile: It is a simple text file with a set of commands or instructions. These commands/instructions are executed successively to perform actions on the base image to create a new docker image. It will help you create custom Docker images.
- Docker Images are read-only templates that you build from a set of instructions written in Docker file. Images define both what you want your packaged application and its dependencies to look like what processes to run when it’s launched.
- Docker Containers: Containers are the structural units of Docker, which are used to hold the entire package that is needed to run the application. In other words, we can say that the image is a template, and the container is the instance of the template.
- Docker Volume: Docker volume enables the persistence and sharing of the container’s data. The mechanism is presented in the following diagram:
Docker volume is the Docker host’s directory mounted inside the container. that allows the container to write container data into the host volumes.
- Docker network:
Using Docker Networking, an isolated package can be communicated. Docker contains the following network drivers -
Bridge — Bridge is a default network driver for the container. It is used when multiple dockers communicate with the same docker host.
Host — It is used when we don’t need network isolation between the container and the host.
None — It disables all the networking.
Overlay — Overlay offers Swarm services to communicate with each other. It enables containers to run on different Docker hosts.
Macvlan — Macvlan is used when we want to assign MAC addresses to the containers.
How to install Docker on Linux/Ubuntu
Step 1) To install Docker, we need to use the Docker team’s DEB packages.
Use the below command to install prerequisite Ubuntu packages
$ sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
Step 2) Add the official Docker GPG key with the fingerprint.
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Step 3) Next, Add the Docker APT repository.
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
You may be prompted to confirm that you wish to add the repository and have the GPG key automatically added to your host.
Step 4) Update APT sources using the below Docker command
$ sudo apt-get update
We can now install the Docker package itself.
Step 5) Now, start installing the Docker packages on Ubuntu using the below Docker command
$ sudo apt-get install docker-ce
Optional, If you want to run docker as non-root user then you need to add it to the docker group.
- Create the docker group if it does not exist
$ sudo groupadd docker
- Add your user to the docker group.
$ sudo usermod -aG docker $USER
- Run the following command or Logout and login again and run (that doesn’t work you may need to reboot your machine first)
$ newgrp docker
- Check if docker can be run without root
$ docker run hello-world
- Reboot if still got error
Docker basic Commands
1. docker — version
This command is used to get the currently installed version of docker
2. docker pull
Usage: docker pull <image name>
This command is used to pull images from the docker repository(hub.docker.com)
3. docker run
Usage: docker run -it -d <image name>
This command is used to create a container from an image
4. docker ps
This command is used to list the running containers
5. docker ps -a
This command is used to show all the running and exited containers
6. docker exec
Usage: docker exec -it <container id> bash
This command is used to access the running container
7. docker stop
Usage: docker stop <container id>
This command stops a running container
8. docker kill
Usage: docker kill <container id>
This command kills the container by stopping its execution immediately.
The difference between ‘docker kill’ and ‘docker stop’ is that ‘docker stop’ gives the container time to shutdown gracefully, in situations when it is taking too much time for getting the container to stop.
9. docker commit
Usage: docker commit <conatainer id> <username/imagename>
This command creates a new image of an edited container on the local system
10. docker login
This command is used to login to the docker hub repository
11. docker push
Usage: docker push <username/image name>
This command is used to push an image to the docker hub repository
12. docker images
This command lists all the locally stored docker images
13. docker rm
Usage: docker rm <container id>
This command is used to delete a stopped container
14. docker rmi
Usage: docker rmi <image-id>
This command is used to delete an image from local storage
15. docker build
Usage: docker build <path to docker file>
This command is used to build an image from a specified docker file