Decoding Docker πŸ‹ : A Beginner's Guide to Containerization and Dockerization πŸ“¦

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

    1. Docker Client is a set of commands that helps us to manage images, containers, networks, etc.

    2. Docker Host is an environment that stores all images, containers, networks, volumes, etc. that are available on that machine

    3. 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

    4. Docker Registry / Hub is similar to Git Hub where our images or official images are stored

πŸ‘‰ Docker Images πŸ“Έ

  1. What is it?

    Docker image is like a template, a complete package.

  2. 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.

  3. How is it made?

    By writing a few commands 😜 [will see in below sections]

  4. 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 version

    docker images ==> Display images available on local [initially it will show empty]

    docker rmi <image-name/id> ==> Remove the image from local

    docker image inspect <image-name/id>

    docker inspect <image-name/id> ==> Display details about the image

    docker 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 file

    docker load -i <file-name>.tar ==> Load image from the tar file

πŸ‘‰ Docker Container πŸ“¦

  1. What is it?

    Docker container is like a running copy of the program from the image

  2. What is inside?

    It's a place where a program runs its little world of files, like a mini-computer just for that program

  3. 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.

  4. 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

    Understanding the Docker Container Lifecycle in Depth

    πŸ”΄ 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 image

    It 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 only

    docker start <container-id/name> ==> Start the container

    docker stop <container-id/name> ==> Stop the container

    docker restart <container-id/name> ==> Restart the container

    docker rm <container-id/name> ==> Delete the container

    docker exec -it <container-id/name> <command> ==> Execute a command in a running container

    docker inspect <container-id/name> ==> Details of container

    docker 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 πŸ«™

  1. What is it?

    Storage for Docker container kept outside of container which helps to keep your data safe.

  2. What is inside?

    Data which container needs or generates like database, logs, config files. Similar to a hard disk.

  3. 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.

  4. 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 volumes

    docker volume create <volume-name> ==> Create a volume

    docker volume rm <volume-name> ==> Remove volume

    docker volume inspect <volume-name> ==> Get details of volume like location, name, etc.

πŸ‘‰ Docker Network πŸ›œ

  1. What is it?

    Docker network is like a virtual LAN.

  2. What is inside?

    It has rules and settings that allow containers to communicate with each other.

  3. How it works?

    Docker connects all containers to a bridge network. [one of the types of docker network]

  4. 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 !!! πŸ‘‹