Docker [Complete Guide]
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
This article at OpenGenus provides an overview of virtual machines (VMs) and their disadvantages, along with an introduction to Docker, its terms, and an example of using Docker to host a PHP application. It explains how Docker solves the cost, performance, and efficiency issues associated with VMs by providing a container-based approach for software development and deployment.
Table of Content
- Virtual Machines
- Disadvantages of virtual machines
- cost
- performance
- Efficiency
- Docker
- Terms related to Docker
- Docker-client
- Daemon
- Containerd
- CLI
- Orchestrator
- gRPC
- Example of Docker program
- Docker CLI
The very first way to host a website on the web required big-big hardware. you must have seen the gynormous servers in movies or in Pictures. each server hosted a web-page which was very expensive and resourceful, but also that was not for consumer use, it was used for scientific research and big companies. The hardware upgraded and later it became a little viable option for a engineer or businessmen to use for their respective work.
Virtual Machines
Then came the VM Virtual machine which tool the same portable hardware and divided the storage hardware using Hypervisor, it helps in connection between Host OS and infrastructure. Virtual Machine divided the physical storage and set the individual space as a different server which requires their respective guest os ( i.e. if you want to run a application in Ubuntu then you needed a Ubuntu guest OS ). It lets you host exact same environment you want your application to run.
Disadvantages of virtual machines
-
Cost
As it requires its own hardware to run it was costlier to scale. And depending on the need this cost varies. If there is more need more investments needs to be done.
-
Performance
Even though the machine are virtualized in virtual machine, it still relies on the resources from the host machine. A computer needs to powerful to run multiple VMs on a system.
-
Efficiency
in terms of hardware accessibilty a virtual machine is less effiicient . It cannot access the hardware directly. And also its speed is not sufficient for most IT firms.
Docker
To solve all the disadvantages of VMs, docker comes into picture.
It is a Open source project that offers software development as a container. Docker contains list of instructions and predefined files that was written by official companies, it provides pre-written codes to build our application on top of.
We take the predefined Software add our own requirements on top or with the Dockerfile ( dockerfile is the line of codes we write to meet our own requirements that later will be integrated in the predefined software). we combine these Dockerfiles which forms image ( Image is the snapshot of a certain software of a certain time which meets our requirements ) , the image contains OS, software, application code etc and run it on cloud platform and this running instance of the file is known as Containers.
- It runs on same environment which is much more efficient and faster.
- Sandbox Projects: it runs application in different containers which is good for security and tweaking or scaling individual parts of a project.
- It makes it easy to run with someone else’s project. Otherwise it will take a lot more time and effort to build our own servers and required software.
Containers talk directly to the Host OS and makes containers for each instance using software which eliminates the step of Hyperviser i.e. having an extra step to talking to the host OS and it does not require different Kernal for each container to run.
Terms related to Docker
-
Docker-client
The Docker client is the primary way that many docker users interact with Docker. It is the command line which lets you control docker.
docker build
docker pull
docker run
-
Daemon
A computer handles many programs, a daemon is program that runs as a background process, it is not under the control of the user. the programs of daemon end the letter ‘d’. Each server of pages on the web has an httpd or Hypertext transfer protocol daemon that continually waits for requests to come in form Web clients and their users.
-
Containerd
Containerd is a daemon that can be used to control the container runtime. The daemon provides an API that allows users to manage containers and their resources. containerd can act as a standalone container runtime, or it can be integrated with docker engine via Docker’s runC comand.
Conatinerd is different form Docker because it does not user the same resource isolation mechanisms, which means that it cannot provide the same level of security as Docker does.
-
CLI
It is the command line that allows you to issue build, run, and stop application commands to a Docker daemon.
-
Orchestrator
Orchestrator is a workflow management solution for the data centre
With Orchestrator you can automate the creation, monitoring, and deployment of resources in your environment.Kubernetes is one example of Orchestrator.
-
gRPC
gRPC is a powerful framework for working with Remote Procedure Calls. RPCs allow you to write code as though it will be run on a local computer, even though it may be executed on another computer.
Let’s make a example to show how Docker works
First make a file which or application you want to run.
I am making a php hello world for this example.
<?php
echo "Hello, World";
//save this file as src file.
Make a file named Dockerfile
go to docker.hub and search for the required image
FROM php:7.0-apache //FROM is the keyword to use the image from the docker.
COPY src/ /var/www/html //it will look for the file in src where you created your php file and copy our file from src to the var/www/html
EXPOSE 80 // when you run a container it will listen to the port 80
$ cd Desktop/docker/
$ ls
Dockerfile src //this is where your Dockerfile and src file is.
$ docker build -t hello-world . // -t is used to name the file and '.' will be used to tell the docker to look in current directory
$ docker run -p 80:80 hello-world // when the request gets to the host docker is going to forward that to container
and when it gets to the container it will refer to expose line in Dockerfile.
Now go the web browser and search the localhost:80 it will show the message Hello, World we build in php file.
When you go the php file and change the message it will not change the message form the server or web-page your local host is hosting. you will have to rebuild the image and repeat all the process above.
You can mount a local directory on your computer as a volume inside the container, then the container when it’s running will be able to see the file you edit in real and time and update in real time too.
$ docker run -p 80:80 -v /Users/manish/Desktop/docker/src/:/var/www/html/ hello-world
// use full path not a relative one. it will mount the local folder inside the container.
Docker CLI ( command line )
- docker ps → Shows running images
- docker run, stop, docker start →Starts or stops the container
- docker exec -it (container id or name of docker) /bin/bash →open container as a root user
- docker run -d -p8080:4559 — name (name your container) redis → runs container in detached mode
- docker logs → shows logs of your container
- docker images → shows images
- docker ps -a
- docker network ls
- docker network create mongo-network
- docker run -d -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=(name) — =
- MONGO_INITDB_ROOT_PASSWORD=(password) —name (name) —net (network you created) mongo
With this article at OpenGenus, you must have a strong hold of Docker.
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.