What is Docker?
- Docker is an open source centerlized Platform designed to Create, Deploy and run applications.
- Docker use container on the host OS to run applications. It allows application to use the same linux Kernal as a System on the host computer, rather than creating a whole virtual OS.
- We can install docker on any OS but Docker engine run natively on linux distribution.
- Docker written in 'go' language.
- Docker is a tool that perform OS level virtualization, also known as Containerization.
- Before Docker, many users faces the problem that a particular Code is running in the developer's system but not in the User's System.
- Docker was first Release in March 2013. It is developed by Soloman Hykes and Sebastion Pahl.
- Docker is a set of Platform as a service that uses OS level Virtualization whereas VMware uses Hardware level Virtualization.
- We can say that Docker is a tools which create the VM.
Advantages of Docker :- There are following advantage of Docker :
- No pre-allocation of RAM
- CI 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 H/W, Virtual H/W or on cloud.
- You can re-use the image
- It took very less time to create container.
Disadvantage of Docker :- There are following disadvantage of Docker :
- Docker is not a good solution for application that require rich GUI.
- Difficult to manage large amount of container.
- Docker does not provide cross-platform compatibility means if an application is designed to run in a docker. If container on window, then it can't run on linux or vice-versa.
- Docker is suitable when the development OS and testing OS are same. If the OS is different, we will face many problem.
- No solution for data recovery & backup.
Components of Docker :- There are following components of Docker :
1. Docker Daemon (Docker Engine) :-
- Docker daemon run on the Host OS.
- It is responsible for running containers to manage docker services.
- Docker Daemon can communicate with Other Daemons.
2. Docker Client :- Docker users can interact with docker daemon throught a Client (CLI Interface).
- Docker Client uses commands and Rest API to communicate with the docker daemon.
- When a Client run any server command on the docker Client terminal, the Client terminal sends commands to their docker daemon.
- It is possible for docker client to communicate with more than one daemon.
3. Docker Host :- Docker Host is used to provide an environment to execute and run applications. It contains the docker daemon, images, containers, networks and storages.
4. Docker Hub/Registry :- Docker registry manages and store the docker images. There are two types of registries in docker.
- Public Registry :- Public registry is also called as docker hub.
- Private Registry :- It is used to share images within the enterprise.
5. Docker Images :-
- Docker images are the read only binary templates used to create docker containers.
or
Single file with all dependencies and configuration required to run a program.
- Ways to Create an Docker Images
- Take image from docker hub
- Create image from docker file
- Create image from existing docker containers
6. Docker Container :-
- Container hold the entire packages that is 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.
- Container is like Virtual Machine
- Images becomes container when they run on the docker engine.
-------------------------------------------------------------------------------------------------------------------------
Basic commands in Docker
1. To see all images present in your local machine
docker images
2. To find out images in docker hub
docker search imageName
Exp :- docker search jenkins
3. To download image from docker hub to local machine
docker pull imageName
Exp :- docker pull jenkins
4. To give name to container
docker run -it --name rahulContainer ubuntu /bin/bash
Here :- -it means interactive mode with terminal
5. To check service is start or not
service docker status
6. To Start Container
docker start containerName
Exp :- docker start rahulContainer
7. To go inside container
docker attach containerName
Exp :- docker attach rahulContainer
8. To see all Containers
docker ps -a
9. To see only running containsers
docker ps
Here ps means process status
10. To Stop Container
docker stop containerName
Exp :- docker stop rahulContainer
11. Delete docker Conatiner
docker rm conainerName
Exp:- docker rm rahulContainer
Note :- We can not remove running container so first stop container the delete container.
-------------------------------------------------------------------------------------------------------------------------
Diff Command in Docker :- There are some points to check difference between base image and after changes.
1. Create Container from Docker Image
docker run -it --name rahulcontainer ubuntu /bin/bash
2. Now crate one file inside this tmp directory
cd tmp/
touch myfile
exit
3. Now if you want to see the difference between the base image & changes on it then
docker diff rahulcontainer
Exp :- docker diff rahulcontainer
Output :-
C /root
A /root/.bash_history
C /tmp
A /tmp/myfile
4. Now Create image of from the Container
docker commit containerName imageName
Exp :- docker commit rahulcontainer rahulcontimage
docker images
5. Now Create image from above create image
docker run -it --name containerName imageName /bin/bash
Exp :- docker run -it --name rahulnewcontainer rahulcontimage /bin/bash
6. Now you can check myfile in tmp directory which is create in rahulcontainer
cd tmp/
ls
Here you will see myfile
-------------------------------------------------------------------------------------------------------------------------
Dockerfile :- Dockerfile is basically a text file. It contains some set of instruction and automation of Docker image creation.
Dockerfile Components :- Some points listed below of Dockerfile components.
1. FROM :- For base image, this command must be top of the dockerfile.
2. RUN :- To execute commands, it will create a layer of image.
3. MAINTAINER :- Author / Owner / Description
4. COPY :- Copy files from local system (docker VM). We need to provide source, destination.
(We can't download file from internet and any remote repo)
5. ADD :- Similar to COPY but, it provide a feature to download files from internet, also we extract file a docker image side.
6. EXPOSE :- To expose ports such as port 8080 for tomcat, port 80 for nginx etc.
7. WORKDIR :- To set working directory for a container
8. CMD :- Execute commands but during container creation.
9. ENTRYPOINT :- Similar to CMD, but has higher priority over CMD, fir st commands will be executed by ENTRYPOINT only.
10. ENV :- To set environment variables.
Note :- All components name of Dockerfile should be in capital latter.
There are some steps to create Dockerfile
Step1 :- Create a file name Dockerfile
vi Dockerfile
Step2 :- Add instruction in Dockerfile
FROM ubuntu
RUN echo "This is rahul testing file to check docker file instruction" > /tmp/testfile
WORKDIR /tmp
ENV myname Rahul Kumar
COPY testfile /tmp
ADD test.tar.gz /tmp
Step3 :- Build Dockerfile to create image
docker build -t imageName .
Exp :- docker build -t myimg
docker ps -a
docker images
Note :- It means tag name and . means current directory
Step4 :- Now Create container from above image
docker run -it --name containerName imageName /bin/bash
Exp :- docker run -it --name mycontainer myimg /bin/bash
Step5 :- cat /tmp/testfile
-------------------------------------------------------------------------------------------------------------------------
Docker Volume & How to Share it :- There are many steps to create volume and share which are following :
1. Volume is simply a directory inside our container.
2. Firstly, we have to declare this directory as a volume and then share volume.
3. Even if we stop container, still we can access volume.
4. Volume will be created in one container.
5. You can declare a directory as a volume only while creating container.
6. You can not create volume from exiting container.
7. You can share one volume across any number of containers.
8. Volume will not be included when you update an image.
Benefits of Volume :- There are some benefits of volume which are following :
1. Decoupling container from storage.
2. Share volume among different containers.
3. Attach volume to containers.
4. On deleting container volume does not delete.
We can Mapped volume in two ways which are following :
1. container <------> container
2. host <------> container
1. Container to Container Volume Share
1. How to create volume from Dockerfile and by using Command
1.1 Create a Dockerfile and write following instruction
FROM ubuntu
VOLUME ["/myvolume1"]
1.2 Create docker image from above Dockerfile by using following command
docker build -t myimage .
1.3 Create a container from above create image by using following command
docker run -it --name containerOne myimage /bin/bash
Now do ls command, you can see myvolume1
1.4 Share volume with another container by using the following command
docker run -it --name containerTwo --privileged=true --volumes-from containerOne ubuntu /bin/bash
Now after creating containerTwo, myvolume1 is visible whatever you do in One Volume, can from other volume.
-> touch /myvolume1/samplefile
-> docker start containerOne
-> docker attach containerOne
-> ls/myvolume1
-> exit
You can see samplefile will avilable in myvolume1 directory.
2. How to create volume from Command
2.1 docker run -it --name containerThree -v /myvolume2 ubuntu /bin/bash
ls
cd /myvolume2
touch myfile3
exit
2.2 docker run -it --name containerFour --privileged=true --volumes-from containerThree ubuntu /bin/bash
ls
cd /myvolue2
touch myfile4
exit
Now, you can see myfile4 into containerThree and vice versa.
2. Host to Container Volume Share
2.1 docker run -it --name hostContainer -v /var/www/html:/hostconvol --privileged=true ubuntu /bin/bash
cd /hostconvol
ls , Now you can see all files of host machine
touch mynewfile (In container)
exit
Now, you check host machine /var/www/html folder then you will see all files with container files.
-------------------------------------------------------------------------------------------------------------------------
1. docker volume ls
2. docker volume create <volumename>
3. docker volume rm <volumename>
4. docker volume prune (It removed all unused docker volume)
5. docker volume inspect <volumename> (Details of volume)
6. docker container inspect <containername> (Details of container)
-------------------------------------------------------------------------------------------------------------------------
Docker Port Expose
1. docker run -td --name contanerName -p 80:80 imageName
Note :-
-td :- Run docker but will not show that you are in container
-p :- Publish
80:80 :- Map container port to local machine port
Exp :- docker run -td --name rahulserver -p 80:80 ubuntu
2. docker ps
Note:- This will show mapping port details
3. docker port contanerName
Note:- This will also show mapping port details
4. docker exec -it contanerName /bin/bash
Note:-
exec :- This means goto contaner like attach
5. apt-get update
Note :- We have created ubuntu container
6. apt-get install apache2 -y
Note :- Install apache2
7. ctrl + l
Note :- For screen clear
8. Got to this directory cd /var/www/html
9. echo "This is rahul docker learning" >index.html
10. service apache2 restart
-------------------------------------------------------------------------------------------------------------------------
- Docker exec create a new process in the container's environment while docker attach just connect the standard Input/Output of the main process inside the container to corresponding standard Input/Output error of current terminal.
- Docker exec is specifically for running new things in a already started container, be it a shell or some other process.
-------------------------------------------------------------------------------------------------------------------------
Basically you have three options :-
1. Neither specify expose nor -P
- If you specify neither expose nor -P, the service in the container will only be accessible from inside the container itself.
2. Only specify expose
- If you expose a port, the service in the container is not accessible from Outside docker, but from inside other docker containers so this is good for inter-container communication.
3. Specify expose and -P
- If you expose and -P a port, the service in the container is accessible from anywhere, even outside docker.
If you do -P but do not expose docker does not an implicit expose. This is because, if a port is open to public, it is automatically also open to the other docker container.
Hence -P include expose.
Really such an amazing blog with true words this was easily understand by beginners at all so keep going always regards by Technokryon
ReplyDeleteDrupal web development company
best data science companies
Joomla web development company
docker run -id --name reactContainer -v C:/xampp/htdocs/important_files:/important_files --privileged=true -p 3000:3000 node /bin/bash
ReplyDelete