Introduction to Docker: Simplifying Deployment with Containers

Β·

4 min read

Why Docker? πŸš€

Have you ever wondered how tech giants like Google manage to deploy billions of environments per week without the hassle of waiting for operating systems to boot? In the fast-paced DevOps world, speed is crucial. Traditional OS installations are slow, but with Docker, we can launch environments in seconds, making deployments faster and more efficient.


πŸ—οΈ Understanding Docker Components

1️⃣ Docker Host – The Foundation

A Docker host consists of:

  • Physical hardware (CPU, RAM, storage)

  • Operating System

  • Docker Engine (software that runs and manages containers)

2️⃣ Docker Image – The Blueprint

A Docker Image is a lightweight, standalone, and executable software package that includes:

  • The application code

  • Necessary dependencies

  • Configurations required to run the application

3️⃣ Container – The Running Instance

A Docker Container is a live instance of a Docker image. It runs in isolation but shares the host OS kernel, making it lightweight and efficient.


πŸ”§ Basic Docker Commands

Here are some fundamental Docker commands to get started:

πŸ“Œ docker info – Get system-wide Docker information
πŸ“Œ docker images – List locally available images
πŸ“Œ docker run -it <image-name> – Launch a container with an interactive terminal
πŸ“Œ docker ps – List running containers
πŸ“Œ docker ps -a – List all containers (including stopped ones)
πŸ“Œ docker stop <container-id> – Stop a running container
πŸ“Œ docker rm -f <container-id> – Forcefully remove a container
πŸ“Œ docker attach <container-id> – Reattach to a running container


🌐 Docker Networking

Docker enables seamless communication between containers, but networking behavior varies based on the network type.

πŸ—οΈ Default Bridge Network (Docker’s Default)

By default, containers use the bridge network, but they do not automatically resolve each other's names. They communicate via IP addresses instead.

Workarounds:

  • Legacy Approach: --link (Deprecated)

  • Recommended Approach: Create a custom network to enable name resolution between containers.

Creating a custom bridge network allows containers to communicate using their names, making service discovery efficient. πŸš€


πŸš€ Deploying an Application in a Container

Why Do We Need an Operating System?

An application cannot run on its ownβ€”it needs an OS, runtimes, and dependencies. Here’s a typical deployment scenario:

  • An OS inside a container (provided by Docker)

  • An application written in Python

  • A requirement to run the application inside the container

Since the base OS inside a container doesn’t come preloaded with Python or libraries like Flask, Django, or FastAPI, we must manually set them up.

πŸ›  Steps to Deploy an Application Manually

A developer provides:

βœ… The application code (app.py) βœ… A list of required libraries (e.g., Flask)

As a DevOps Engineer, our responsibility is to:

1️⃣ Launch a container with an OS
2️⃣ Install Python runtime inside the container
3️⃣ Install necessary libraries like Flask
4️⃣ Create a workspace inside the container
5️⃣ Copy the application code inside the container
6️⃣ Run the application manually

πŸ”§ Step-by-Step Deployment

πŸ—οΈ Step 1: Launch a Container

To run our application, we need an OS. We can launch a container using a base image like Ubuntu or RedHat:

docker run -it redhat/ubi8:latest

This provides a fresh RedHat 8 OS inside a container.

πŸ”§ Step 2: Install Python

The base OS doesn’t include Python, so we install it manually:

yum install -y python36 python3-pip

Now, we have Python installed inside the container.

πŸ“¦ Step 3: Install Flask (or Other Libraries)

If our application requires Flask, we install it inside the container:

pip3 install flask

πŸ“‚ Step 4: Create a Workspace

We create a directory inside the container to store application files:

mkdir /code

πŸ‘¨πŸ’» Step 5: Copy the Application Code

Now, we transfer app.py from the host machine to the container:

docker cp app.py <container_id>:/code/

This keeps everything organized inside the /code directory.

▢️ Step 6: Run the Application

Once the code is inside the container, we manually start the application:

python3 /code/app.py

Now, our Flask application is running inside the container! πŸŽ‰


❗ The Problem with Manual Deployment

While this method works, it has limitations:

  • If the container stops, the application won’t start automatically.

  • Restarting requires manual intervention (python3 app.py).

  • Every new container requires us to repeat the setup process.

This is inefficient! 🚨


⚑ Automating Deployment with Custom Docker Images

Instead of setting up everything manually each time, we can create a custom Docker image that includes: βœ”οΈ Base OS
βœ”οΈ Python runtime
βœ”οΈ Required libraries (Flask, etc.)
βœ”οΈ The application code

This allows us to launch a fully configured container with a single command! πŸš€

Next Up: Learn how to create a custom Docker image to automate the entire process. Stay tuned! πŸ”₯

Β