Day 12 of 100 Days : Deep Dive into Docker Containers

🐳 Why Are Containers So Lightweight?

Unlike VMs, which each require a full operating system, containers share the host OS kernel. This makes them incredibly lightweight, as they don't need to load separate OS layers. Containers also include only the libraries and dependencies essential for the application, reducing overhead. This efficiency allows faster startup times, less memory consumption, and quicker scaling compared to VMs.

📂 Files and Folders in Containers

Containers have their own isolated file systems that include:

  1. Application Files: Specific code, libraries, and dependencies for the app.

  2. Config Files: Environment configurations.

  3. User-defined Data: This may vary based on the app's needs.

Containers can also use specific files and folders from the host OS, making it easy to access persistent data. This flexibility lets you control what each container needs and ensures your applications stay portable.

🏗 Docker Architecture

Docker's architecture is based on a client-server model. Here’s how it works:

  1. Docker Client: Sends commands to the Docker daemon.

  2. Docker Daemon: Builds, runs, and manages containers.

  3. Docker Registry (Docker Hub): A central place to store and share container images.

  4. Docker Engine: Coordinates communication between the client, daemon, and registries.

The Docker daemon handles container creation, monitoring, and deletion, while the Docker client provides an interface to interact with these containers.

🌍 Docker Hub

Docker Hub is a public registry for Docker images, allowing developers to share pre-configured environments or application setups. You can also upload custom images. To use Docker Hub, you need an account, which allows you to pull images for testing or deploy your own images for easy sharing.

🚀 Practical Section: Docker Commands

Let’s go through some basic commands that will help you navigate Docker:

  1. List running containers: docker ps

  2. List all containers (including stopped): docker ps -a

  3. Start a container: docker start <container-id or name>

  4. Display Docker information: docker info

  5. Stop a container: docker stop <container-id or name>

Example Exercise: Run through each command above to get hands-on practice. This will give you a clear view of Docker's functionality.

🛠 Docker Installed and Running!

Once Docker is installed, you can check if everything is running smoothly with the command:

docker run hello-world

If all is well, you’ll see:

Hello from Docker!
This message shows that your installation appears to be working correctly.

📝 First Steps: Building and Running Your Own Docker Image

  1. Clone the Docker Examples Repository
    We’ll use a pre-configured GitHub repository for Docker beginners. Clone it by running:

     git clone https://github.com/iam-veeramalla/Docker-Zero-to-Hero
     cd examples
    

    Note: I've customized this using my own Docker account credentials.

  2. Log in to Docker
    Log in to Docker Hub to push and pull images:

     docker login
    
  3. Build Your First Docker Image
    Run this command to create an image:

     docker build -t muni****/my-first-docker-image:latest .
    

    Output Example:

     Successfully built [image-id]
     Successfully tagged muni****/my-first-docker-image:latest
    
  4. Run Your Docker Container
    Start your container to see it in action:

     docker run -it muni****/my-first-docker-image
    

    Output:

     Hello World
    
  5. Push Your Image to Docker Hub
    Finally, upload your image to Docker Hub:

     docker push muni****/my-first-docker-image
    

    Output:

     Using default tag: latest
     The push refers to repository [docker.io/muni****/my-first-docker-image]
     latest: digest: sha256:... size: 1157
    

Final Thoughts

By now, you’ve built your first Docker image, created a container, and pushed the image to Docker Hub—making it ready for the world to use. Docker’s lightweight, flexible approach makes it essential for DevOps, enabling easy scaling, resource efficiency, and faster deployments.

Congratulations on a fantastic start with Docker! 🎉