Before Docker
Initially, one application runs on one server. which won't be cool if more people are using the application the load will be increased then we have added a new server which can not be handled by those servers. They did not have a way to run multiple applications on one server.
Who solves these problems?
VMWare solves this problem in which we can run multiple application on the same server, but the problem is that virtual machine requires their own operating system which is not perfect. If we have to Dual booted the computer we have to give the dedicated amount of hardware, RAM, CPU, etc.
Docker is the container platform through which we can build, test and deploy applications. All the dependencies of our application are in the Dockerfile which is used to create a Docker image this run then a container will be created it ensure that our application will run in any environment.
Containerization makes our application portable which same code can be run on any device. Though docker deploys our application on the container which is easy to deploy our application, easy to scale application or we can perform the rollback operations. It also helps us during the open source project to set up our project in our machine.
Docker helps us to run the application in an isolated environment. Dual boot the computer one part of the hard disk window was installed and another part of Ubuntu is installed. The window has no idea what is run in Ubuntu and Ubuntu has no idea what is run into the window.
containerization is many application runs on a single operating system they do not require an entirely new operating system.
Containerization and virtualization
A containerization is a form of virtualization. Virtualization aims to run multiple os instances into a single server, whereas containerization runs a single OS instance with multiple users in an isolated environment.
Containerization is achieved by packaging software code, libraries, frameworks, and other dependencies together in an isolated user space called a container. This container is portable.
How does virtualization work?
Hypervisor software facilitates virtualization. A hypervisor sits on top of an operating system. But, we can also have hypervisors that are installed directly onto the hardware. Hypervisors take physical resources and divide them up so that virtual environments can use them.
When a user or program issues an instruction to the VM that requires additional resources from the physical environment, the hypervisor relays the request to the physical system and caches the changes. There are two types of hypervisors, Type 1 (Bare Metal) and Type 2 (Hosted).
Installation
Install Docker Desktop on Mac Here.
Install Docker Desktop on Windows
Here
.Install Docker Desktop on Linux Here.
Docker architecture
Docker uses the client-server architecture. Docker clients talk to the docker daemon which does the heavy lifting of building, running, and distributing on your containers. The Docker client and docker daemon communicate using REST API.
Docker daemon
It listens to the API request of the docker client and manages the object of the docker such as images, containers, network, and volumes. It also communicates with another Docker daemon to manage the Docker services.
Docker Client
It is the way through which the user is communicate with Docker. When you use a command such as a docker run the client sends this command to the daemon which carries them out. Docker clients can communicate with more than one daemon.
Docker registry
It is the place where the docker images are stored. A Docker hub is a public registry that ant one can use. When you pull an image, Docker by default looks in the public registry and saves the image in the local system on the Docker host.
Dockerfile
Describes the steps to create the Docker image. This file is used to create the Docker image. These images can be pulled to create the containers in any environment. These images can also be stored in the Docker hub. The container is the running state of the docker image. The container will have the applications with all their dependencies. Create a file named 'Dockerfile'.
Dockerfile:-
FROM ubuntu
MAINTAINER Meenu yadav <meenu@gmail.com>
RUN apt-get update
CMD [“echo”, “Hello Docker”]
FROM:-It is an instruction to initialize a new build stage and set the base image for subsequent instructions.
MAINTAINER:-Author or description
RUN:-To execute the commands in a new layer on top of the current image and commit the result.
CMD:-Execute commands but during the container creation.
Docker image
The docker image is the file that defines the Docker Container. A docker image is a read-only template with instructions to create the Docker Container. A container that moves from one environment to another environment with the same OS work without changes because images contain the instruction of the dependencies of the application which is needed to execute the code. If we have to change something we can change it in the container not in the image because images are immutable. Docker images are a template to create the container. Images can store locally or remote registry. Docker can build the images using the instructions from the Dockerfile.
Container
A container is the runnable instance of the image. This is the place where your application is run. You can create, stop, start, or delete containers using Docker API or Docker CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state. If we delete a container the whole data will be deleted.
Docker volume
The container has a virtual file system where they stored the data but there is no persistence if you have to remove the container or stop it and restart the container then the data which is inside the virtual file system is gone and which starts from the very fresh state which is not very practical because we want to save the changes that your application made in the database and that's where docker volume is comes into the picture. where plug the physical file system path could be a folder or directory and plugged into the container file system path. In simple terms, docker volume is a directory or folder in the physical host file system mounted into the virtual file system of docker. Now if you change something in the host file system then it automatically appears in the container and if you restart the container or restart from the fresh state then it also appears in the container. It gets the data automatically from the host because the data is still there and that's how data is populated on the startup of the container every time you restart.
Docker basic commands
docker version
– Echoes Client’s and Server’s Version of Dockerdocker images
– List all Docker imagesdocker build <image>
– Builds an image from a Docker filedocker run
– Runs a command in a new container.docker start
– Starts one or more stopped containersdocker stop <container_id>
– Stops containerdocker rmi <image>
– Removes Docker imagedocker rm <container_id>
– Removes Containerdocker pull
– Pulls an image or a repository from a registrydocker push
– Pushes an image or a repository to a registrydocker export
– Exports a container’s filesystem as a tar archivedocker exec
– Runs a command in a run-time containerdocker ps
– Show running containersdocker ps -a
– Show all containers which are running and stopped containerdocker ps -l
– Show the latest created containerdocker search
– Searches the Docker Hub for imagesdocker attach
– Attaches to a running containerdocker commit
– Creates a new image from a container’s changesdocker run -phost_port:container_port
-Binds the two ports first one is the host machine port and another is the container port.docker run -v /home/mount/data
-for creating the docker volume where we define the connection of the reference between the host directory and container directory.
Wrap it with a demo
Pulling the latest version of the Mongo from the docker hub
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker pull mongo
latest: Pulling from library/mongo
b2ddfd337773: Pull complete
2217df6bf2b5: Pull complete
29c228992bd2: Pull complete
fc95cad02df0: Pull complete
2c411bcb7900: Pull complete
2086fcd3f2dc: Pull complete
0ff48fd69cff: Pull complete
434eda66f547: Pull complete
18000c72efc8: Pull complete
Digest: sha256:9c8a0a019671ed7d402768d4df6dddcc898828e21e9f7b90a34b55fe8ca676ac
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest
$ docker images
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
imagetest latest 773018bd3161 5 weeks ago 69.2MB
updateimage latest 33df67f0eeec 5 weeks ago 69.2MB
mongo latest 58042414fbaa 5 weeks ago 618MB
ubuntu latest bab8ce5c00ca 6 weeks ago 69.2MB
ubuntu <none> 4c2c87c6c36e 4 months ago 69.2MB
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
ac1ef50462bf mongo "docker-entrypoint.s…" 50 seconds ago Exited (0)
Thanks for reading, if you find it useful do subscribe to my newsletter for more such articles.