Docker is a tool, which separates applications from infrastructure using the container technology. It is easier to create, deploy, and run applications by using containers. Docker daemon always run by the root user because daemon interacts with kernel and only root user should have access to interact with the kernel.
Docker Container:
A Docker container is an open source software development platform. Its main benefit is to package applications in containers, allowing them to be portable to any system running a Linux or Windows operating system. Docker Containers are very light in weight. Each container has its own file system, like /etc, /var, /opt, etc. Containers can be accessed on the application layer.
Fig: Docker Container |
Docker Registry:
A registry is a storage and content delivery system, holding named Docker images, available in different tagged versions. Users interact with a registry by using Docker push and pull commands. One can create and share the Docker image on 'Docker Hub' for sharing with co-workers, customers, etc.
$ docker pull
$ docker run <container name>
Docker Hub:
Docker Hub is a centralized docker/image registry.Difference between Vagrant and Docker:
- Vagrant is based on Virtual Machines means it runs complete Operating System whereas Docker uses same kernel code as 'Host Operating System'.
- Vagrant boxes are difficult to scale up while by Docker we can scale up multiple containers.
- In Vagrant, it is difficult to communicate between Host OS files to Guest OS files but in Docker, we can do that easily.
Fig: Virtual Machine vs Docker |
Install Docker on Windows:
Perquisites to install Docker in Windows:- Windows 10 Pro version required. The home version does not support Docker.
- Hyper-v must be activated.
- Docker Toolbox should be installed first, which is a small Virtual Box to share between the host OS and the guest OS.
To install Docker on Windows machine, kindly go to the URL:
https://docs.docker.com/docker-for-windows/install
Docker on windows will need the hyper-v to be enabled on windows. If you have any other virtualization platform installed on your windows machine, like VirtualBox, it will have to be disabled in order to use Docker on Windows.
Once we download the 'InstallDocker.msi' file, install it by following standard installation instructions.
Install Docker-CE on Linux:
Here, I am going to launch Docker-CE on Google Cloud Platform (GCP) instance:
Login to GCP ➔ Navigation Menu ➔ Compute Engine ➔ VM Instances ➔ Create Instance ➔ Name: docker ➔ Select region and zone ➔ Machine type: n1-standard-2 (2 vCPU, 7.5 GB memory) ➔ Boot disk: CentOS 7 ➔ Select ➔ [*] Allow HTTP traffic ➔ [*] Allow HTTPS traffic ➔ Create.
This will create an instance in GCP:Fig: Docker instance in GCP |
Now, click on SSH, this will show us terminal for Docker instance. Now, switch to root user because daemon interacts with kernel and only root user should have access to interact with the kernel:
$ sudo su -
Use command:
$ sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
Add a repository, named 'docker-ce.repo':
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
Now, install Docker-CE package:
$ sudo yum install -y docker-ce
Start Docker service:
$ sudo systemctl start docker
Enable Docker service:
$ sudo systemctl enable docker
Create a Container:
Generally, we create a container in interactive or detach mode:
Syntax for Interactive mode:
$ sudo docker run -it <image> <command>This command will run and start a container.
i: interactive mode (in it, we can actually see and modify files)
t: to allocate a terminal to this container
image: An image can only use one at a time. We can get Images from Docker Hub. We can create our account on Docker Hub to share our customize images.
command (optional): e.g. /bin/bash, actually running bash executable. This command which we provide in last, will run first and override any embedded command inside that image.
We use the exit command to exit from the container. But this exit command will stop the container. If we want to exit from a container but the container should not be stopped, we need to press 'control p q' all together.
Once a container is created we can not change its name and its mode (interactive or detach). Containers have their own IPs.
Download an image from Docker Hub:
Open https://hub.docker.com in the browser. In Search bar type the image name which we want to download ➔ Click on that image ➔ in the right side we can see Docker Pull Command. Use that command to download the image from Docker Hub.
Fig: Docker Hub |
Basic Docker Commands:
$ sudo docker ps(Shows only running containers.)
$ sudo docker ps -a
(Shows all containers (running or stopped))
$ sudo docker attach <docker-code>
(Allows you to attach to a running container using the container ID or name)
$ sudo docker rm <container-1 ID> <container-2 ID>…
(Remove containers)
$ sudo docker rm $(docker ps -aq)
(Remove all containers)
$ sudo docker inspect <container ID>
(To know information about that container)
$ sudo docker run -it -p <host_port>:<container_port> <image name>
(To map host port to container port. Once a container is created, we cannot expose any port.)
$ sudo docker images
(To list all downloaded images)
$ sudo docker rmi <image ID>
(To remove an image)
$ sudo docker rmi $(docker images -q)
(Remove all images)
$ sudo docker commit <container_ID OR container _name> <image_name_you_wat_to_give>
(This will create an image from given container ID or container name.)
Thank you for sharing very useful blog!!!!
ReplyDeleteDocker and Kubernetes Training
Docker Training
Docker Online Training
Docker Training in Hyderabad
Kubernetes Online Training