Introduction to Docker: Simplifying Deployment with Containers
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! π₯