1.Introduction
Have you ever tried to run a project on your laptop only to get endless errors like “module not found” or “it works on my machine but not yours”?
Or maybe you’ve installed software that messed up your system because it needed a different version of Python, Java, or some library?
Now, imagine if you could take your entire application — code, tools, dependencies, and even the environment — pack it neatly into a small box, and then run that same box anywhere: your laptop, a colleague’s computer, a server in the cloud. No extra setup, no headaches, no “it doesn’t work here” issues.
That magical box is called a container. And the tool that makes it possible is Docker
- What is Docker
Docker is a platform that uses containerization to streamline software development. It packages applications into containers, which are lightweight and include only essential elements. This ensures applications run consistently across different environments, improving efficiency and portability in software deployment.
- Why use docker
Consistency: "It runs the same everywhere."
Easy setup: No more “works on my machine” problems.
Lightweight: Unlike virtual machines, Docker containers share the host system’s kernel.
Scalability: Containers can be spun up and down quickly.
2.Installing docker,
Docker can be installed on various operating systems, including Windows, macOS, and Linux. While the core functionality remains the same across all platforms, the installation process differs slightly depending on the system. Below, you'll find step-by-step instructions for installing Docker on your preferred operating system.
Installing Docker on Windows
Download Docker Desktop for windows
(https://docs.docker.com/desktop/setup/install/windows-install/)Installing Docker on Linux (Ubuntu)
- Update package lists:
sudo apt update && sudo apt upgrade -y
2.Install dependencies: sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
3.Add Docker’s official GPG key: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
4.Add Docker’s repository: echo "deb [signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
5.Install Docker engine: sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
6.Add Docker to your user group: sudo usermod -aG docker $USER
7.Check Installation: wsl --shutdown
8.Verify installation: docker –version
3.Docker Basic concepts
Image
A snapshot or template of an environment.
Built using a Dockerfile.
Container
A running instance of an image.
You can start, stop, and remove containers without affecting the image.
Docker Hub
A public registry where you can find and share images (like postgres, python, nginx).
Dockerfile
A text file that describes how to build a Docker image.
4.Running Your First Docker Container
Now that we’ve covered Docker's core concepts, it’s time to put them into action! Let’s start by running our first container to ensure Docker is installed correctly and working as expected.
To test your Docker installation, open PowerShell (Windows) or Terminal (Mac and Linux) and run
docker run hello-world
This pulls the hello-world image from DockerHub and runs it in a container.
5.Building Your First Docker Image
Creating a Docker image involves writing a Dockerfile, a script that automates image-building. This ensures consistency and portability across different environments. Once an image is built, it can be run as a container to execute applications in an isolated environment.
A Dockerfile is a script containing a series of instructions that define how a Docker image is built. It automates the image creation process, ensuring consistency across environments. Each instruction in a Dockerfile creates a new layer in the image. Here’s a breakdown of an example Dockerfile for a simple Python Flask app:
# Base image containing Python runtime
FROM python:3.9
# Set the working directory inside the container
WORKDIR /app
# Copy the application files from the host to the container
COPY . /app
# Install the dependencies listed in requirements.txt
RUN pip install -r requirements.txt
# Define the command to run the Flask app when the container starts
CMD ["python", "app.py"]
Breaking down the Dockerfile above:
FROM python:3.9: Specifies the base image with Python 3.9 pre-installed.
WORKDIR /app: Sets /app as the working directory inside the container.
COPY . /app: Copies all files from the host’s current directory to /app in the container.
RUN pip install -r requirements.txt: Installs all required dependencies inside the container.
CMD ["python", "app.py"]: Defines the command to execute when the container starts.
Building and running the image: docker build -t my-flask-app
6.Docker Compose
Docker Compose is a tool that simplifies the management of multi-container applications. Instead of running multiple docker run commands, you can define an entire application stack using a docker-compose.yml
file and deploy it with a single command.
Here’s how we define our multi-container setup in Docker Compose:
version: '3'
services:
web:
build: .
ports:
- "3000:3000"
depends_on:
- database
database:
image: mongo
volumes:
- db-data:/data/db
volumes:
db-data:
Once the docker-compose.yml file is ready, we can launch the entire application stack with a single command:
docker-compose up -d
Top comments (0)