Docker Basics and Hands On

What is Docker? What is Container?

Docker is a platform and technology for building, shipping, and running distributed applications. It uses containerization to create lightweight, portable, self-sufficient containers that can run on any infrastructure, making it easier to deploy and manage applications. Docker allows developers to package an application with all of its dependencies and ship it as a single container, making it easier to move the application between development, testing, and production environments.

Docker Vs Virtual Machine

The operating system has 2 layers

DockerVirtual Machine
The Docker uses Kernel of its base Operating systemThe Virtual machine has its own kernel
The size of docker image is smaller ranging from few MB'sThe virtual machine images are larger in size ranging from few GB's
Docker are faster as they don't have to start kernelVM takes time to load kernel and then applications.
Windows kernel might not be compatible with Linux so, linux docker can't run on windows machine.VM's have seperate kernel

Docker Image Vs Docker Container

ImageContainer
Actual Package along with configuration and dependencyActually start the application
Artifect -> can be moved aroundContainer is running environment for image
Not in running stateIn running state

Container has a seperate port and virtual filesystem different from host machine.

Docker Installation

  1. Go to docker website and install according to your OS platform https://docs.docker.com/get-docker/

  2. Enable virtualization if not enabled. https://stackoverflow.com/questions/27884846/virtualization-not-enabled-in-bios

  3. For EC2 installation from userdata refer this article.

Basic Commands

Docker commandUse case
docker pull <image-name>pulls the docker image from docker hub
docker run <image-name:tag>
<tag> is optionalpulls image and runs the container
docker psshows all the running container with container-id, image name, ports, container name etc.
docker run -d <container-name>shows the id and runs the container in background without disturbing the host machine shell
docker stop <container-id>stops the docker container
docker start <container-id>starts the docker container
docker ps -ashows both running and stopped container
docker run -p<host-port>:<container-port> <image-name:tag>port mapping host and docker
docker imagesshows images present in docker
docker rmi <image-id>deletes the docker image
docker rm <container-id>deletes the docker container
docker build -t <image-name:tag>create a new Docker image from a specified Dockerfile
docker --helpshows a list of docker commands

Debugging Commands

CommandUse Case
docker run -p<host-port>:<container-port> --name <container-name> <image-name:tag>alloctes container name instead of random name along with port mapping
docker logs <container-id>shows the container logs
docker exec -it <container-id> /bin/bashEnter into docker container with interactive terminal as a bash shell

Creating and pushing Docker Image to Dockerhub

After cloning the app from git repo we went into app and have created a Dockerfile

After we created our Dockerfile, we have to build todo-app Docker image from the same location for all the items.

After that we have to login into docker hub using the above command and provide our credentials. If account do not exist we have to create from Dockerhub and then login.

In this step, we rename our docker image using docker tag and view list of locally created docker images using docker images. After that using push command we send out newly created image to Dockerhub

Pulling the Docker image from Docker hub and run the web application in browser

In the first step we create a container out of docker image with -p as port 8001 on container and port 8001 on host machine are mapped together . -d option helps to run this container in background as a service. It is then followed by image-name : tag . First it checked if image was present on local volume, then it fetched whole image from docker hub.

We check if container has been created by using docker ps . After that we open the port 8001 of host machine through AWS security group in inbound rules . When port is accessible, the web application is displayed through public ip of the host machine followed by mapped port number of the container.

Why Docker container is not used in production?

Docker containers can be kill easily if someone gets container id , therefore for security reasons it is not preferred in production environment. Other reasons can be

  • Lack of persistent storage: By default, Docker containers are ephemeral, meaning that any data stored within them is lost when the container is stopped or deleted. This can make it difficult to use them in production environments where data needs to be retained.

  • Difficulty in scaling: Docker containers can be challenging to scale horizontally, especially when compared to other container orchestration tools like Kubernetes.

  • Difficulty in monitoring: Docker containers can be difficult to monitor and troubleshoot, especially when running multiple containers on the same host.

  • Security concerns: Running untrusted or unknown images in production can pose a security risk.

  • Production requirements: Some production environments might have specific requirements that are not met by containerization, such as GPU support or real-time processing

  • Legacy systems: Some organizations might be running legacy systems that are not designed to be run in containers.

There are other Orchestration tools like docker swarm and kubernetes which are used to deploy the containers. We would be studying these in our upcoming article. Stay tuned!

References

  • OpenAI Chat GPT for clearing my doubts though #90DaysOf devops community helped as well.

  • TrainWithShubham for the hands on examples

    Article written based on my own understanding, References helped me to reduce the time consumed in writing the article.