Saturday, August 27, 2022

Docker-Tutorial

 Docker Basics


Docker is a container management service. The keywords of Docker are Build, share and run anywhere. The whole idea of Docker is for developers to easily develop applications, ship them into containers which can then be deployed anywhere.

what are containers?

Containers are completely isolated environments. As in they can have their own processes or services, their own networking interfaces, their own mounts just like virtual machines, except they’re all will be sharing the same operating system kernel.


How does Docker work? 

OS consist of two things, an OS kernel and a set of software. The OS kernel is responsible for interacting with the underlying hardware, while the OS kernel remains the same. it’s the software that makes these operating systems different. For example: Centos, Ubuntu, Rocky, these all uses Linux kernel internally but the software installed may consist of a different user interface drivers, compilers, file managers, developer tools which differentiate them with each other.  In similar fashion Docker can run on any flavour of OS on top of it. Each Docker container only has the additional software and Docker utilizes the underlying kernel of Docker host, which works with all the operating systems.

The main purpose of Docker is to containerize applications and to ship/share(through publishing to  repository like Docker Hub/AWS ECR e.t.c  them and run them.

Containers solve application problems by improving Development Operations, enabling microservices, increasing portability, and further improving resource utilization. 


Advantages of Containers 

  • They are more lightweight than VMs, as their images are measured in megabytes rather than gigabytes. 
  • Containers require fewer IT resources to deploy, run, and manage. 
  • Containers spin up in milliseconds. Since their size is smaller.

Reference: https://www.geeksforgeeks.org/difference-between-virtual-machines-and-containers/


Step 1 − Building the Docker File(named as Dockerfile without any extension)

FROM alpine

CMD ["echo", "Hello Gaurav, Welcome in the Docker World!"]


Step 2 − Executing the command to build the docker image


docker build -t="docker-demo" .



Step 3 − Using docker run command, to get the output




If we would like to go inside any container and see the content or want to do some modifications then we could use below command:

docker exec -it <container-id> /bin/bash or docker exec -it <container-id> /bin/sh

Note: bash and sh are two different shells of the Unix operating system. bash is sh, but with more features and better syntax. Bash is “Bourne Again SHell”, and is an improvement of the sh (original Bourne shell). bash is a superset of sh. The -i and -t options are used to access the container in an interactive mode.

Basic Commands

docker pull – Ex: docker pull rroemhild/test-openldap

docker search – Ex: docker search openjdk -> To search for public images on the Docker hub

docker images- Ex: docker images -> To list all the local images

docker ps(process status): Ex: docker ps -> list all the running containers

docker ps(process status): Ex: docker ps –a -> list all the running containers including stopped one

docker stop: Ex: docker stop <container-id> -> To stop a container we could use either the container id or container name. 


docker run --rm -p 10389:10389 -p 10636:10636 rroemhild/test-openldap

We could use docker run command like above to execute specific image on multiple ports. This will first creates a writeable container layer over the specified image, and then starts it .

If we want to join 2 environments using docker, so that another env should be able to utilize the resources of first one. We could use below command:

docker swarm init

Execution of above command will give below kind of output, We could execute that command in another environment which needs to join and then we could execute node command to verify the joined worker env.

docker swarm join --token SWMTKN-1-3if1odzpyrajovjatjqa5sp9sgydfmbbp3wzirus6lnol96e9l-ceqr8uuu8987k4w5n3ft3e3p9 10.30.14.142:2377

docker node ls

docker logs


docker service ls


docker service logs –f <ID> OR <NAME>


We could use service logs command to check specific service logs by passing the corresponding id or its name. 


Another way to see the logs


docker ps

docker logs –f <CONTAINER ID>

Note: We could use process status logs using above command by passing the corresponding container ID. 


Prune unused Docker objects


We could remove images, containers, volumes, and networks as these objects are generally not removed unless we explicitly mention to remove Or We could directly use docker system prune to clean up multiple types of objects at once.


docker image prune : Allows us to clean up unused images. 


docker image prune –a : Removes all images which are not used by existing containers.


docker container prune : Remove all stopped containers.


docker volume prune : Remove all the volumes not used by any container


docker network prune : Remove all networks not used by any container


docker system prune : docker system prune command is a shortcut that prunes images, containers, and networks but it won’t remove volumes, we must specify volumes like below:


docker system prune --volumes

No comments:

Post a Comment

dig v/s host v/s nslookup

 dig v/s host v/s nslookup Dig and nslookup are two tools that can be used to query DNS servers.  They both perform similar functions, but t...