Docker was first released in March 2013 and is developed by Solomon Hykes and Sebastien Pahl
Docker is an open-source centralized platform designed to create deploy and run applications.
Docker uses containers on the host OS to run applications.
It allows applications to use the same Linux kernel as s system on the host computer, rather than creating a whole virtual OS.
We can install docker on any OS but the Docker engine runs natively on Linux distribution.
Docker is written in the “Go” language.
Docker is a tool that performs OS-level Virtualization, Also known as containerization.
Before Docker, many users face the problem that a particular code running in the developer’s code is running in the developer’s system but not in the user’s system. Docker is a set of “platform as services” that use OS-level virtualization whereas VMware uses hardware-level virtualization.
ADVANTAGE OF DOCKER
No pre-allocation of ram.
CI (continuous integration) Efficiency => docker enables you to build a container image and use that same image across every step of the deployment process.
Less cost.
It is light in weight
It can run on physical hardware virtual hardware or on the cloud.
you can reuse the image.
It took very less time to create the container.
DISADVANTAGES OF DOCKER
Docker is not a good solution for application that requires a rich GUI.
Difficult to manage a large number of containers.
Docker does not provide cross-platform compatibility means if an application is designed to run in a docker container on Windows then it can’t run on Linux or vice-versa.
Docker is suitable when the development OS and testing OS are the same if the OS is different, we should use VM.
COMPONENTS OF DOCKER
Docker Damon:- Docker demon runs on the host OS. It is responsible for running containers to manage docker services. Docker daemons can communicate with other daemons.
Docker client:- Docker users can interact with docker demons through a client (CLI). The Docker client uses commands and rest-api to communicate with the docker daemon. When a client runs any server command on the docker client terminal the client terminal sends these docker commands to the docker daemon. It is possible for the docker client to communicate with more than one daemon.
Docker Host:-Docker Host is used to provide an environment to execute and run applications it contains the docker demon, images, containers, networks, and storage.
Docker Hub/Registry:- Docker Registry manages and stores the docker images. There are two types of registry in the docker hub (1) public registry:- public registry is also called docker hub (2) Private Registry:- It is used to share images within an enterprise.
Docker images:- Docker images are the read-only binary templates used to create docker containers. Or signal file with all the dependencies and configuration required to run the program.
Ways to create an Image:- (1) Take image from docker hub (2) Create image from docker file (3) Create image from existing docker containers.
Docker Container:- Container hold the entire packages that are needed to run the application. Or in other words, We can say that the image is a template and the container is a copy of that template. It is a like virtual machine. Images become containers when they run on the docker engine.
(1) |
yum install docker |
Install docker on os |
(2) |
Service docker start service docker stop service docker restart service docker status |
Start-stop restart and status of docker service |
(3) |
docker info |
Information about current docker |
(4) |
docker -v or docker –version |
Check docker version |
(5) |
docker images |
To see all images in the local machine |
(6) |
docker search image-name docker search –no-index –no-trunc image-name Eg:- docker search centos docker search –no-index –no-trunc centos |
To find-out images in the docker hub |
(7) |
docker pull image-name Eg:- docker pull centos docker pull docker.io/centos |
Download image from docker hub to the local machine |
(8) |
Docker run –it –name docker-name imagename /bin/bash Eg:- docker run -it –name anil centos /bin/bash |
To give a name to the container where -i=interactive mode and -t= terminal |
(9) |
Docker start container-name Eg:- docker start anil |
To start container |
(10) |
Docker attach container-name Eg:- docker attach anil |
To do inside the specific container |
(11) |
docker ps -a |
To see all the containers |
docker ps |
To see only running containers |
|
(12) |
docker stop container-name Eg:- docker stop anil |
To stop container |
(13) |
docker delete container-name Eg:- docker delete anil |
To delete container |
(14) |
docker diff container-name docker diff anil |
Find-out difference between the original image and the container |
(15) |
docker commit container-name image-name Eg:- |
Create an image from the container |
(16) |
Docker build -t image-name . |
Build an image using Dockerfile |
(17) |
Docker run -it –name newcontainer -v volume_directory_name image-name /bin/bash Eg:- Docker run -it –name apache1 -v /voume1 centos /bin/bash |
Create a container with the volume |
(18) |
Docker run –it –name newcontainer name — privileged=true –volumesfrom old-container-name(which have volume) centos /bin/bash Eg:- Docker run –it –name apache2 — privileged=true –volumesfrom apache centos /bin/bash |
Share volume of container one to container two |
(19) |
Docker run -it –name newcontainer -v host_dir_path:volume_directory_name image-name /bin/bash Eg:- Docker run -it –name apache1 -v /home/docroot:/voume1 centos /bin/bash |
Map host directory with the container as a container volume. |
(20) |
Docker volume ls |
List of created volumes |
(21) |
Docker volume create Volume_Name |
Create volume with the simple command |
(22) |
Docker volume rm Volume_Name |
Delete created volume |
(23) |
Docker volume prune |
Delete all unused volume |
(24) |
Docker volume inspect Volume_Name |
Inspect volume and find out information about the volume |
(25) |
Docker container inspect container_Name |
Inspect the container and find out information about the container |
(26) |
Docker run -td –name container-name -p 80:80 centos Eg:- Docker run -td –name apache_cont -p 80:80 centos docker exec -it container-name /bin/bash |
Expose port for internet -t= terminal and -d for daemon |
(27) |
Docker port container-name Eg:- Docker port apache_cont |
To check which ports are mapped (expose) for the container |
(28) |
Docker login |
Connect system with hub.docker.com |
(29) |
Docker tag local-imagename dockerid/remote_image-name |
Give tag name for local and remote image |
(30) |
Docker push dockerid/newimage |
Push image on hub.docker.com |
(31) |
Docker stop $(docker ps -a -q) |
Stop all running container |
(32) |
Docker rm $(docker ps -a -q) |
Delete all stop container |
(33) |
Docker rmi -f $(docker images -q) |
Delete all images |
DOCKERFILE
A Docker file is basically a text file it contains some set of instructions it is an automation of docker image creations.
*docker file name always “Dockerfile” where D is big and “ckerfile” is small characters
Docker Components:-
* Docker Components’ name is always written in capital letters.
FROM |
For the base image, the command must be on top of docker-file |
|
RUN |
To execute commands it will create a layer in the image |
|
MAINTAINER |
Author/owner/Description |
|
COPY |
Copy file from local system (base system) We need to provide source and destination (we can’t download files from the internet and any remote repo) |
|
ADD |
Add is Similar to copy but, it provides a feature to download files from the internet and also extract the file in the docker image side. |
|
EXPOSE |
To expose ports such as port 25 for mail and 80 for httpd |
|
WORKDIR |
To set the working directory for a container |
|
CMD |
Execute commands but during containers creation |
|
ENTRYPOINT |
Similar to CMD but has higher priority over CMD.!st command will be executed by ENTRYPOINT only |
|
ENV |
Environment Variables |
|
ARG |
ARG is also known as build-time variables. They are only available from the moment they are ‘announced’ in the Dockerfile with an ARG instruction up to the moment when the image is built. Running containers can’t access values of ARG variables. |
|
VOLUME |
Define volume directory to share files with other containers or hosts |
Dockerfile example:-
FROM centos:7 EXPOSE 80 # Start Apache
|
DOCKER VOLUME
Volume is simply a directory inside the container.
Firstly we have to declare the directory as volume and then share volume.
Even if we stop the container still we can access volume.
You can declare a directory as volume only while creating the container.
You can’t create volume from the existing container.
You can share one volume across any number of containers.
The volume will not be included when you update an image.
You can map volume in two ways.
==> container to container.
==> host to container.
Benefits of volume:-
Decoupling containers from storage
Share volume among different container
Attach the volume to containers
Only delete container volume does not delete.
DIFFERENCE BETWEEN DOCKER ATTACH AND DOCKER EXEC?
Docker exec creates a new process in the container’s environments while docker attach just connect the stranded Input/Output of the main process inside the container to the corresponding standard input/output error of the current terminal
Docker exec is specifically for running new things in the already started container.
WHAT IS THE DIFFERENCE BETWEEN EXPOSE AND PUBLISH IN A DOCKER?
Basically, we have three option
==> Neither specify expose nor -p.
==> Only specify expose.
==> Specify expose and -p.
If we neither specify expose nor -p, the service in the container will only be accessible from inside the container itself.
If you expose a port, the service in the container is not accessible from outside docker, but from inside Other docker containers, so –expose is good for internal communication.
|
If you expose and -p a port, the service in the container accessible from anywhere, even out side if container.
|
If you do -p but not expose docker does an implicit –expose. This is because, if a port is open to the public then it is automatically open for the other container. Hence ‘-p’ includes –expose.