Learn Docker | Basics | Day 1
Container Vs Virtual Machine (VM)
A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.
While, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.
What is Docker?
- Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers.
- Docker provides the ability to package and run an application in a loosely isolated environment called a container.
- Containers are lightweight because they don’t need extra load of a hypervisor, but run directly within the host machine’s kernel.
- if you were using virtual machines. You can even run Docker containers within virtual machines.
- Docker is perfect for high density environments and for small and medium deployments where you need to do more with fewer resources.
Let’s learn docker quickly
You can setup a small environment on your windows/Linux laptop to learn docker.
you can install virtual box on windows laptop and provisioned a CentOS VM to prepare docker environment. If you have Linux Laptop, you can install docker directly as shown below.
Now let’s perform following activities in Linux either VM or on host machine wherever you have. Assuming operating system is CentOS.
Add Docker Repository to install docker. To add any repository you need “yum-config-manager” command which comes from “yum-utils”. So install yum-utils. You can also manually add repository in /etc/yum.repo.d/
yum install -y yum-utils ⇒ yum-utils provides the yum-config-manager utility, yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo yum list docker-ce --showduplicates | sort -r ⇒ list available docker engine packages yum install docker-ce-17.09.1.ce-1.el7.centos ⇒ use full qualified package version. if you just install docker-ce, this will install latest version. I would recommend to install 'Latest-1' Version. Docker support kernel 3.0 and above. Hence use CentOS 7 which comes default with Kernel >3.0. Or if you are using older version of CentOS, please upgrade your kernel version at least to 3.0 systemctl start docker ⇒ Start Docker daemon systemctl enable docker ⇒ Enable Docker daemon on system boot
yum remove docker-ce rm -rf /var/lib/docker ⇒ Images, containers, volumes, or customized configuration files on your host are not automatically removed. To delete all images, containers, and volume
Search & download docker image from docker hub (hub.docker.com)
docker search ubuntu ⇒ Search for available Ubuntu Images. docker pull ubuntu:16.04 ⇒ download docker image. Here we have downloaded ubuntu:16.04 docker images ⇒ to see how many images I have in my system docker image ls ⇒ List Docker Images, Similar to above command.
List containers in your system
docker container ls ⇒ List docker containers "running" docker container ls -a ⇒ List docker containers "all" docker container ls -aq ⇒ List docker containers "all in quiet mode" Try following - similar to above commands docker ps ⇒ to see how many containers are running docker ps -a ⇒ to show all containers wheather stop or running docker ps -aq ⇒ only container IDs
docker run -it ubuntu:16.04 ⇒ launch a container( i - interactive, t - terminal tty), you will get container console after launch. Ubuntu:14.04 is available image. on docker console, Type "exit". This will stop the container and you will be back on host console. ctrl+p+q ⇒ come out from container console without stopping container. docker run -it --name=myname ubuntu:14.04 ⇒ Launch a container with a name, Help us to remember a name. Docker IDs can't be memorized. docker run -dit --name myname ubuntu:14:04 ⇒ d - detach mode, Launch a container & you will not get into the console of container. After Launching few containers, list docker processes using "docker ps"
Let’s launch as more as containers you want in few seconds 🙂
#for((i=1;i<=10;i++)) ⇒ Launching 10 Containers, Change the value for i as you want. >do >docker run -dit --name=container$i ubuntu:14:04 ⇒ Make sure you launch in detach mode >done
Play with few more command
docker start container_ID/name ⇒ to start a container docker attach container_ID/name ⇒ Get container console docker stop container_ID/name ⇒ Stop container docker rm container_ID/name ⇒ Terminating container (only stop container will be removed) docker rm -f container_ID/name ⇒ Terminating container forcefully (even running container will also terminate) docker rm -f $(docker ps -aq) ⇒ remove all container forcefully docker exec -it container_ID /bin/bash ⇒ execute linux shell commands on container docker logs container_ID ⇒ will show you what is running on container console witout entering/attaching to console docker info ⇒ docker information docker version ⇒ Display docker version docker --version ⇒ Display docker version docker ⇒ List docker CLI commands docker container --help