Docker
Docker is a tool to create, Deploy and Run applications using containers.
Docker is a tool to create, Deploy and Run applications using containers.
Os Level Virtualization: OS-level virtualization is an operating system paradigm in which the kernel allows the existence of multiple isolated user space instances
Union File System:
Layered Architechture:
Advantages:
We can create containers without OS
95% OS at Main node + 5% OS images
No Pre-Allocation of RAM
Can Replicate Same Environment(Using OS Image)
Less Cost and Size
Fast to Diploy
Can run on Physical Virtual and Cloud Machines
Can re-use the same image
Disadvantage
Docker Installation:
For Ubuntu: https://docs.docker.com/engine/install/ubuntu/
For RHEL: https://docs.docker.com/engine/install/rhel/
yum install docker -y
service docker start
docker info
Docker root directory: /var/lib/docker/
Docker Commands:
docker images // it list all iages enabled
docker ps -a // List all containers
docker run -it ubuntu /bin/bash //to Create/start a VM
(t - getting terminal
i - interactive mode)
docker run -it --name vmname ubuntu /bin/bash //Create a VM with vm name
docker run kodecloud/webapp //It run the web application in attach mode and Ctrl+c will close it
docker run -d kodecloud/webapp //It run the web application in detach mode background
docker start containerid //to start a vm
docker attach containerid // to enter a container fro node
docker stop containerid // stop a container
docker rm containerid // to remove the docker container
docker rmi imagename //to remove the docker images
docker exec containername cat /etc/hosts //Executr some commands in a container
docker search ubuntu //list all ubntu images available at docker hub
docker rename oldname newname // renamig the VM name
docker commit containerid imagename //Creating a new image
docker inspect containername/ID //to detail aboutthe contaciner including IP
DocketFile
Create a "Dockerfile" //Docker file is a text file with some instrctions to build an image
FROM Ubuntu
RUN echo "Hi Jo" /tmp/ghgg
RUN apt-get update
vi Dockerfile
FROM ubuntu
VOLUME "/data"
docker build -t imagename .
docker run -it --name containername imagename /bin/bash
docker run -it --name containername2 --privileged=true --volumes-from containername imagename /bin/bashInstallation
docker build -t demo . // by taking docker file in the existing directory it will create image
docker run -it --name democontainer demo /bin/bash //Creating containerwith newly created demo image
Dockerfile Command:
RUN
ADD
ENV : provides default values for variables that can be accessed within the container
FROM
WORKDIR
COPY
PULL - Adds files from your Docker repository
eg: docker pull ubuntu //It download latest ubuntu image
CMD - Specifies what command to run within the container
ENTRYPOINT allows specifying a command along with the parameters
MAINTAINER declares the author field of the images
Docker Volumes
vi Dockerfile
FROM ubuntu
VOLUME "/data"
docker build -t imagename . //Build image with the help of Docker file with volumes
docker run -it --name containername imagename /bin/bash //Create a container using Docker file with volume
docker run -it --name containername2 --privileged=true --volumes-from containername imagename /bin/bash //Contaner to container volume sharing
docker run -it --name containername3 -v /sai ubuntu /bin/bash //Create a container with volume
docker run -it --name containername4 --privileged=true --volumes-from containername3 ubuntu /bin/bash //Contaner to container volume sharing
docker run -it --name containername3 -v /home/ec2-user:/raj ubuntu /bin/bash //Hostname directory mapping to container volume
Port Maping
docker run -td --name webserver -p 80:80 ubuntu /bin/bash
docker exec -it webserver /bin/bash // ENter in to the newly created VPS
docker run -p 80:5001 //This is for port maping
docker run -v /opt/var/lib/mysql:/var/lib/mysql // for directory maping
docker logs containerID //to view logs
Tags: Tags are different OS version representation
OS Image File: https://hub.docker.com/
Run time command will overwrite the arguments mentioned in the CMD instruction in docker file
Run time command will overwrite the arguments mentioned in the ENTRYPOINT instruction in docker file. It will just append
Docker Environment Variables:
MYSQL_ROOT_PASSWORD=
APP_COLOR
Eg: docker run --name containername -e MYSQL_ROOT_PASSWORD=db_password -p 8080:3242 imagename
Entry Point
doker run --links
docker run -d --name=redis
docker run -d --name=clickcounter --link redis:redis -p 8085:5000 kodekloud/click-counter
Docker Compose: Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services
References
Docker Registry:
It is a online repository where the docker images are stored.
https://hub.docker.com/ - DockerHub is a hosted registry solution by Docker Inc
Private registry:
docker run -d -p 5000:5000 --restart=always --name my-registry registry:2
Run: docker pull nginx:latest then docker image tag nginx:latest localhost:5000/nginx:latest and finally push it using docker push localhost:5000/nginx:latest.
We will use the same steps for the second image docker pull httpd:latest and then docker image tag httpd:latest localhost:5000/httpd:latest and finally push it using docker push localhost:5000/httpd:latest
ENTRYPOINT is one of the many instructions you can write in a dockerfile. The ENTRYPOINT instruction is used to configure the executables that will always run after the container is initiated.
Eg: FROM Ubuntu
ENTRYPOINT ["sleep"] //in this scenario, we dont need to enter sleep command again, only needs to add the value
Docker Engine: docker-deamon, docker-restapi, docker-cli
the docker cli need not to be on the same machine, it could be on a laptop or another machine. [docker -H=remote-docker-engine:2375
Configure resoures limitation - cgroup
eg: docker run --cpus=.5 ubuntu; docker run --memory=100M ubuntu;
Storage: Layered architecture
/var/lib/docker
Volume:
#docker volume create data_volume //It will create a volume at /var/lib/docker/volumes/data_volume
#docker run -v dockervolume:/var/lib/mysql mysql //It mount the container /var/lib/mysql to /var/lib/docker/volumes/data_volume. If the volume in custom path or external drive, we should mention exact path
docker system df -v //Disk usage of the docker machine
docker exec mysql-db mysql -pdb_pass123 -e 'use foo; select * from myTable' //retrieve data from a mysql database which is there in a docker container
Network
Bridge : Default container assigned with a private internal IP address (docker run ubuntu)
None: No network and no access to other containers and public network (docker run ubuntu --network=none)
Host: (docker run ubuntu --network=host)
#docker network ls
# docker network create --driver bridge --subnet 182.18.0.0/16 custom-isolated-network
# docker run -d --name=mysql-db --network=wp-mysql-network -e MYSQL_ROOT_PASSWORD=db_pass123 mysql:5.6
#docker run --network=wp-mysql-network -e DB_Host=mysql-db -e DB_Password=db_pass123 -p 38080:8080 --name webapp --link mysql-db:mysql-db -d kodekloud/simple-webapp-mysql
Docker in Windows
Docker on Windows has two options
Docker on Windows using Docker Toolbox - Docker on Linux VM on Oracle Virtual Box on Windows
Docker Toolbox Requirements
64-bit operating system
Windows 7 or Higher
Virtualization enabled on Windows
Docker Toolbox Contents
Oracle Virtualbox
Docker Engine
Docker Machine
Docker Compose
Kitematic GUI
Docker For Windows - Docker on Linux VM on Windows Hyper-V on Windows
Docker For Windows Requirements
Windows 10 Enterprise/Professional Edition
Windows Server 2016
Docker For Windows supports Linux Containers (Default) and Windows Containers
Docker For Windows Container Types:
Windows Server Core: Windows container on native windows server core
Hyper-V Isolation: Windows container on an isolated hyper-v kernel
References and Links:
Docker on Windows Documentation: https://docs.docker.com/docker-for-windows/
Docker For Windows Download: https://www.docker.com/docker-windows
Docker Toolbox Download: https://www.docker.com/products/docker-toolbox