Mastering Docker in DevOps: A Comprehensive Guide
Written on
Chapter 1: Introduction to Docker
In this blog post, we will explore Docker, providing an overview before delving into detailed instructions on incorporating Docker into your DevOps workflow. Topics will include executing Docker containers, managing Docker networking, utilizing Docker volumes, and constructing Docker images with Dockerfile.
What Makes Docker Essential?
Reflecting on the last two to three decades, the traditional approach involved hardware with an operating system installed on top. Running an application required compiling code and managing all dependencies. When additional applications or increased capacity were necessary, new hardware purchases and configurations were essential.
The introduction of virtualization added a hypervisor layer between the hardware and the operating system, allowing for multiple isolated applications to run on virtual machines. However, each virtual machine still required software installations and dependency setups, limiting application portability.
Understanding Docker
Simply put, Docker allows software packaging for execution on any machine, whether it's Windows, macOS, or Linux. Docker has fundamentally transformed software development by enabling microservice-oriented application creation.
How Docker Operates
The Docker engine functions atop the host operating system, comprising a server process (dockerd) that manages Docker containers. Docker containers are crafted to isolate applications and their dependencies, ensuring consistent operation across various environments.
To effectively utilize Docker, it’s crucial to grasp three core concepts: Dockerfile, Docker image, and Docker container.
What is a Dockerfile?
A Dockerfile serves as a blueprint for constructing a Docker image.
What is a Docker Image?
A Docker image acts as a template for launching Docker containers and contains all dependencies necessary to run code within a container.
What is a Docker Container?
A container represents an active process. A single Docker image can spawn multiple processes across various locations, allowing easy sharing with others.
Getting Started with Docker
Prerequisites:
Ensure Docker is installed on your local machine or cloud VM.
Linux Installation:
For those running Linux locally, on a VirtualBox, or a cloud VM, package managers can be utilized for installation. Follow the instructions outlined in this blog post.
Mac and Windows:
Install Docker Desktop to run Docker locally.
Starting with Docker is straightforward. Execute the following command:
docker run -d -t --name Thor alpine
docker run -d -t busybox
This command will initiate two containers using the minimalist public Linux images, Alpine and BusyBox, available on Docker Hub.
The -d flag runs the container in detached mode (background), -t attaches a TTY terminal, and --name assigns a name to the container, defaulting to a random name if omitted.
Note: On first execution, Docker will download (pull) the images from Docker Hub to your local machine.
To list running and stopped containers, use:
docker ps # for running containers
docker ps -a # for all containers
To view Docker images on your local machine:
docker image ls
You’ll find the sizes of the images used for running Linux-based containers are significantly smaller than traditional Linux distributions like Ubuntu or CentOS.
Interacting with Running Containers
You can interact with active containers by either passing commands or opening an interactive session:
docker exec -it <container_name_or_id> sh
For instance, to retrieve container information:
docker exec -t Thor ls ; ps
To check memory usage, use:
docker exec -t <container_id> free -m
To start, stop, or remove containers, use:
docker stop <container_name_or_id>
docker start <container_name_or_id>
docker rm <container_name_or_id>
For forced removal:
docker rm -f <container_name>
Docker Networking
Docker offers several types of networking:
Default Bridge:
To access a web server running in an Nginx container, ensure you expose the relevant ports.
User-defined Bridge Network:
Create a custom network to enhance service discovery and isolation among containers.
Host Network:
Running a container in the host network allows it to share the host's IP address.
Docker Volumes
Docker isolates content, code, and data within a container, which means deleting a container also deletes its contents. To persist data, you can utilize volumes.
Bind Mount:
A file or directory from the host can be mounted into a container.
Docker Volumes:
A managed storage location that exists independently of container lifecycles.
To create and inspect volumes, use:
docker volume create <volume_name>
docker volume inspect <volume_name>
Building a Docker Image
Now that you understand the basic concepts of Docker, let's build a Docker image containing a simple Flask application. Create a folder and add three files: app.py, requirements.txt, and Dockerfile.
Here is the content for each file:
app.py:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_docker():
return 'Hello, Docker!'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
requirements.txt:
Flask
Dockerfile:
# Use an official Python runtime as a parent image
FROM python:3.11
# Set the working directory to /app
WORKDIR /app
# Copy the python dependency file into the container at /app
COPY requirements.txt /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Copy the flask app file into the container at /app
COPY app.py /app
# Make port 5000 available to the world outside this container
EXPOSE 5000
# Run app.py when the container launches
CMD ["python", "app.py"]
To build the Docker image, run:
docker build -t flask-image .
After creating a Docker Hub repository, tag and push the image:
docker tag flask-image <your_dockerhub_username>/docker-demo-docker:1.0
docker push <your_dockerhub_username>/docker-demo-docker:1.0
To delete local images and pull from Docker Hub, execute:
docker pull <your_dockerhub_username>/docker-demo-docker:1.0
docker run -td -p 8080:5000 --name flask <your_dockerhub_username>/docker-demo-docker:1.0
I assist aspiring DevOps professionals with customized roadmaps, study materials, resume reviews, interview tips, and mock interviews.
In the first video, you'll find a comprehensive course titled "Free AWS Zero to Hero Course for DevOps Engineers," which is ideal for those looking to enhance their skills.
The second video offers a complete course on "Azure DevOps Zero to Hero," providing further insights into DevOps practices.