Why Docker? A Beginner’s Journey into the World of Containers

What is Docker ?

Docker is a platform that enables developers to package applications and their dependencies into portable containers. It's widely used for automating deployment and scaling, making applications more reliable and easier to manage.There are many tools available to create and manage containers, like Docker, buildah,containerd, Podman, and CRI-O. Out of these, Docker is very popular because it is simple to use, has great community support, and offers powerful features.

Docker Objects :

Docker objects are the core components of Docker's ecosystem, used to build, manage, and run containerized applications.

  • Images :
    Pre-made templates used to create containers. Think of them as a recipe for your app.

    Example: An image with Python installed to run a Python app.

  • Containers :
    Running versions of images. They are like a working copy of the recipe (image).

    Example: A container running your Python app.

  • Volumes :
    A place to store data from containers so it’s safe even if the container stops or gets deleted.

    Example: Saving database files in a volume.

  • Networks
    They connect containers so they can talk to each other or connect to the internet.

    Example: A backend container talking to a frontend container.

  • Services :
    Groups of containers working together to handle large workloads, like in a cluster.

    Example: A service running multiple web server containers to handle high traffic.

  • Dockerfile:
    A file with instructions to build a Docker image. It’s like writing the recipe.

    Example: A Dockerfile with instructions to install Ubuntu and set up Python.

These objects work together in Docker’s ecosystem, where images are used to create containers, containers can store data in volumes, and containers communicate with each other through networks.

Why docker ?

  • Docker is Better than other tools for smaller projects, individual developers, and teams because of its simplicity, rich ecosystem, and ease of use.

  • For enterprise-level applications, tools like OpenShift or Kubernetes may be better suited, but Docker is still widely used in such environments for containerization.

  • Docker’s mature ecosystem (Docker Hub, Docker Compose, Docker Swarm) makes it a go-to tool for most containerization needs.

Docker Architecture :

  • Hardware:

    • This is the physical or virtual machine where Docker runs. It provides the resources like CPU and memory.
  • Host OS:

    • The Host OS is the operating system (Linux, Windows, or macOS) running on the machine. Docker runs on this OS.
  • Docker Engine:

    • Docker Engine is the main part of Docker. It includes:

      • Docker Daemon: It is a background service that runs Docker. It manages containers (small isolated environments for apps).

      • Docker CLI: This is the command-line interface that lets users tell Docker what to do (like run containers, create images, etc.).

    • The Docker Engine manages the containers' lifecycle, while the Docker Client provides the interface for users to interact with Docker through commands.

  • Docker Client:

    • The Docker Client is the tool that users interact with. It could be a command-line interface (CLI) or a GUI. The user sends commands to the Docker Engine through the Docker Client to manage containers and images.
  • Docker Containers:

    • Docker Containers are like mini virtual machines that hold everything an app needs to run. They are lightweight, fast, and isolated from each other, which makes them easy to manage.

Docker Registry:

  • A Docker Registry is a place to store Docker images (the blueprints for containers). The most common registry is Docker Hub, but you can have your own private registry too.

  • When you need a container, Docker pulls the image from the registry and then runs it as a container.

How docker engine works :

  • Docker Engine is the brain of Docker. It includes the Docker Daemon (which runs containers) and the Docker CLI (which lets you give commands).

    Docker Commands :

    Working with docker images :

  • docker images: List all images on the system.

  • docker pull: Download an image from Docker Hub (e.g., docker pull ubuntu).

  • docker build -t : Build an image from a Dockerfile (e.g., docker build -t myapp:1.0 .).

  • docker tag <new_tag>: Add a tag to an image.

  • docker rmi : Remove an image (e.g., docker rmi ubuntu).

  • docker rmi $(docker images -aq): Remove all images.

    Managing Containers

  • docker ps: List running containers. docker ps -a: List all containers (including stopped ones).

  • docker run: Run a container (e.g., docker run ubuntu)

    -it: Interactive mode (e.g., docker run -it ubuntu bash).

    -d: Run in detached mode (e.g., docker run -d nginx).

    --name : Assign a custom name to the container.

  • docker exec -it : Run a command inside a running container (e.g., docker exec -it myapp bash).

  • docker stop : Stop a running container.

  • docker start : Start a stopped container.

  • docker rm : Remove a container.

  • docker logs : View logs of a container.

  • docker commit <container_name> <image_name>:version : Converting container to image.

  • docker cp <container_name>:<path_inside_container> <path_on_local_system> : copy a file or directory from container to your local system.

    Working with Volumes

  • docker volume create <volume_name>: Create a volume.

  • docker volume ls: List all volumes.

  • docker volume inspect <volume_name>: Get details of a specific volume.

  • docker volume rm <volume_name>: Remove a volume.

    Managing Networks

  • docker network ls: List all Docker networks.

  • docker network create <network_name>: Create a custom network.

  • docker network connect : Connect a container to a network.

  • docker network disconnect : Disconnect a container from a network.

    Docker Compose

  • docker-compose up: Start services defined in a docker-compose.yml file.

    -d: Run in detached mode.

  • docker-compose down: Stop and remove containers, networks, and volumes created by Compose.

  • docker-compose ps: List all containers managed by Compose.

  • example :

    Monitoring and Debugging

  • docker stats: Show real-time resource usage by containers.

  • docker inspect <container|image|network>: Get detailed information about a container or image.

  • docker top : Display running processes in a container.

    Docker System Management

  • docker system df: Show disk usage by Docker objects.

  • docker system prune: Remove unused containers, images, and networks.

    -a : Remove all docker objects.

    Advanced Commands

  • docker save -o : Save an image to a tar file.

  • docker load -i : Load an image from a tar file.

  • docker export -o : Export a container’s filesystem to a file.

  • docker import : Import a container's filesystem from a file.

If your goal is to move or backup an image with all its layers, history, and optimizations intact, then docker save and docker load are the right choice.

If you need to export and transfer the filesystem of a running container, docker export and docker import are suitable, but they come with the downside of losing layers and history, which may lead to a less optimized image.

Docker is a platform that allows developers to package applications and their dependencies into portable containers, simplifying deployment and scaling. Key components include Docker images, containers, volumes, networks, and services. Docker's architecture consists of hardware, host OS, Docker Engine, Docker CLI, Docker Client, Docker Containers, and Docker Registry. It is favored for its simplicity, ecosystem, and ease of use, making it suitable for smaller projects and individual developers. While tools like OpenShift or Kubernetes may be better for enterprise-level applications, Docker remains widely used. Core commands involve managing images, containers, volumes, networks, and using Docker Compose for orchestrating services.