Decoding Docker π : A Beginner's Guide to Containerization and Dockerization π¦
Learn Docker on AWS EC2 Instance - Ubuntu
π Quick Intro π
In earlier times, deploying an application would take many hours, but with the help of docker it's been so efficient and flexible
Docker creates a magic box that contains everything your application needs to run β the code, libraries, dependencies, and even the operating system. This magic box is called a "container" [will see the container in detail]
Docker enables developers to bundle their applications into containers, ensuring consistency across different environments, such as DEV, QA, UAT, PILOT, and PROD.
π Virtualization vs Containerization π¦
If you have ever used Oracle Virtual Box, you might know that we install operating systems in it, which will have its environment inside, and we can install many operating systems inside it
-
You can see the above image, in the virtual box we use a hypervisor, above which we install operating systems [Guest OS] for each VM, which will have its dependencies and libraries
As there are multiple OS installed it will cause a deficiency in infrastructure and speed
But in the case of containerization, we will have a single operating system [Host OS], on top of which we will use a container engine [similiar to a virtual box], in which we will run containers that are lightweight with minimal dependencies
Containerization results in efficiency and faster processing and booting
π Docker Installation π
we will install docker in the ubuntu EC2 instance in AWS
Run the below commands one by one
To run docker commands we need to use sudo for each command or else add a particular user to the docker group
# update ubuntu sudo apt-get update # install docker sudo apt-get install git docker.io -y # add current user to docker group sudo usermod -aG docker $USER # start docker service sudo systemctl start docker # check docker version docker --version
# single command for all steps sudo apt-get update && \ sudo apt-get install git docker.io -y && \ sudo usermod -aG docker $USER && \ sudo systemctl start docker && \ docker --version
Restart the VM
π Docker Architecture β
-
Four main components are Docker Client, Docker Host, Docker Daemon, Docker Hub/Registry
When we install docker we will get Docker Client and Docker Host (Docker Deamon included) by default
Docker Client is a set of commands that helps us to manage images, containers, networks, etc.
Docker Host is an environment that stores all images, containers, networks, volumes, etc. that are available on that machine
Docker Deamon is a background unended process that acts as a mediator between Docker Client and Docker Hub or Docker Host environment. Every command of Docker Client has to go through Docker Daemon. It manages images and containers that are available in the local
Docker Registry / Hub is similar to Git Hub where our images or official images are stored
π Docker Images πΈ
What is it?
Docker image is like a template, a complete package.
What is inside it?
It has everything the program needs to run, like the program itself, the tools it uses, libraries, dependencies, and any special settings.
How is it made?
By writing a few commands π [will see in below sections]
Why it's useful?
This makes sure the program works the same way every time, for everyone, on every machine no matter where it runs
Similar to GitHub you can pull or push the images from or to Docker Hub
Each image has a unique ID and tag [kind of version]
π΄ COMMANDS
docker pull <image>:[tag]
==> Pull images from the docker hub, a tag is optional, when a tag is not provided will pull the latest versiondocker images
==> Display images available on local [initially it will show empty]docker rmi <image-name/id>
==> Remove the image from localdocker image inspect <image-name/id>
docker inspect <image-name/id>
==> Display details about the imagedocker build -t <image-name>:<tag> <dockerfile>
==> Create your own image from docker file [will see in detail below]docker save -o <file-name>.tar <image-name>:<tag>
==> Save an image as a tar filedocker load -i <file-name>.tar
==> Load image from the tar file
π Docker Container π¦
What is it?
Docker container is like a running copy of the program from the image
What is inside?
It's a place where a program runs its little world of files, like a mini-computer just for that program
How it works?
You can start, stop, or remove these containers easily. They're like portable units that always run the program the same way.
Why it's useful?
Containers make sure the program runs smoothly, and you can use them anywhere, like on your computer or in a big computer center.
In simple words, a container is the running state of an image
There are 5 container states
π΄ COMMANDS
docker create <image-name/id:tag>
==> Create a container from the image, if the image is not found locally then will be pulled from the docker hub [tag is optional]docker run <image-name/id>
==> Create a running container from an imageIt is running a container, but we can't use our terminal because the terminal is occupied by a container, to run the container in the background we can use some flag
π© FLAGS
--network
==> Add container to network [will see network below]-d
==> Run container in the background--name
==> Give some name to container [docker will give some random name by default]-it
==> Open container in interactive mode-p
==> Port Mapping-v <volume-name>:<path-in-container>
==> Attach volume while running a container# Preferred sequence for providing flags docker run --name <container-name> -p <local-port>:<docker-port> -v <volume-name>:<path-in-container> -d <image-name/id>
# Run a container interactively docker run -it <image-name>:<tag> /bin/bash
docker ps
==> List all the running containersπ© FLAGS
-a
==> List all containers-q
==> List idβs onlydocker start <container-id/name>
==> Start the containerdocker stop <container-id/name>
==> Stop the containerdocker restart <container-id/name>
==> Restart the containerdocker rm <container-id/name>
==> Delete the containerdocker exec -it <container-id/name> <command>
==> Execute a command in a running containerdocker inspect <container-id/name>
==> Details of containerdocker logs <container-id/name>
==> View container logs# Copy files from/to a container docker cp <container_id>:<path_in_container> <local_path> docker cp <local_path> <container_id>:<path_in_container>
π Docker Volume π«
What is it?
Storage for Docker container kept outside of container which helps to keep your data safe.
What is inside?
Data which container needs or generates like database, logs, config files. Similar to a hard disk.
How it works?
They are created outside of a container, in your host machine. Which can be easily attached to multiple containers. You can read, write, and share data across containers.
Why it's useful?
It keeps your data intact even if the container crashes or is deleted. Perfect for storing important stuff like databases or logs that you donβt want to lose. π
You can see the location of volume in your host machine using inspect [given below]
π΄ COMMANDS
docker volume ls
==> List all volumesdocker volume create <volume-name>
==> Create a volumedocker volume rm <volume-name>
==> Remove volumedocker volume inspect <volume-name>
==> Get details of volume like location, name, etc.
π Docker Network π
What is it?
Docker network is like a virtual LAN.
What is inside?
It has rules and settings that allow containers to communicate with each other.
How it works?
Docker connects all containers to a bridge network. [one of the types of docker network]
Why it's useful?
It allows you to manage how your containers communicate with each other and the outside world, whether on the same machine or spread across different hosts. π
By default, docker creates three networks [Bridge, Host, None]
Types of Docker Networks
Bridge: Default network for containers on the same host.
Host: Use the host network directly, skipping the network bridge.
None
Overlay: Used for multi-host communication, like in Docker Swarm or Kubernetes.
Macvlan: Assigns a MAC address to containers for direct network access.
π΄ COMMANDS
docker network ls
==> List all networks
docker network create --driver <network-type> <network-name>
==> Create a network
docker network rm <network-name>
==> Remove the network
docker network connect <network-name> <container-name/id>
==> Remove container from network.
docker network connect <network-name> <container-name/id>
==> Add container to network.
- You can verify if the container is added to a network or not by running the inspect command on the container.
π Conclusion β
Yo, peeps! Youβre all set to roll with Docker now! π. Wanna check out Docker files? Stay tuned for my Docker Part 2 blog... itβs droppinβ soon! π. Till thenβ¦
Happy Learning !!! π