Docker for Beginners: A Step-by-Step Guide

Docker has revolutionized the way developers build, ship, and run applications. If you’re new to Docker, this guide will walk you through…

Docker for Beginners: A Step-by-Step Guide
Photo by Rubaitul Azad on Unsplash

Docker has revolutionized the way developers build, ship, and run applications. If you’re new to Docker, this guide will walk you through the basics step by step, helping you understand how to containerize your applications efficiently.

What is Docker?

Docker is an open-source platform that enables developers to package applications into lightweight, portable containers. These containers include everything the application needs to run — code, runtime, libraries, and dependencies — ensuring consistency across different environments.

Why Use Docker?

Consistency — Works the same on any system
Lightweight — Uses fewer resources than virtual machines
Fast Deployment — Speeds up application development and testing
Scalability — Easily scale applications with Docker Swarm or Kubernetes

Step 1: Install Docker

To get started, install Docker on your system:

  • Windows & Mac: Download Docker Desktop and install it.
  • Linux: Use the following commands to install Docker:
sudo apt update 
sudo apt install docker.io -y 
sudo systemctl start docker 
sudo systemctl enable docker

Verify the installation:

docker --version

Step 2: Run Your First Docker Container

Docker containers are based on images. Let’s run a simple container using the official hello-world image.

docker run hello-world

This command downloads the hello-world image (if not already available) and runs it inside a container. If everything is set up correctly, you’ll see a message confirming that Docker is working!

Step 3: Understanding Docker Images & Containers

  • Images are templates for containers.
  • Containers are running instances of images.

List all downloaded images:

docker images

List all running containers:

docker ps

List all containers (including stopped ones):

docker ps -a

Stop a running container:

docker stop <container-id>

Remove a container:

docker rm <container-id>

Step 4: Pull & Run an Image from Docker Hub

Docker Hub is a public repository where you can find thousands of pre-built images. Let’s pull and run an Nginx web server.

docker pull nginx 
docker run -d -p 8080:80 nginx

Now, open http://localhost:8080 in your browser. You should see the default Nginx welcome page.

Step 5: Build Your Own Docker Image

Let’s create a simple Dockerfile to containerize a Python application.

1️⃣ Create a Python Script

Create a file named app.py:

print("Hello, Docker!")

2️⃣ Create a Dockerfile

Inside the same directory, create a file named Dockerfile (without any extension):

# Use an official Python image 
FROM python:3.9 
 
# Set the working directory 
WORKDIR /app 
 
# Copy the script into the container 
COPY app.py . 
 
# Define the command to run the script 
CMD ["python", "app.py"]

3️⃣ Build and Run the Image

docker build -t my-python-app . 
docker run my-python-app

You should see “Hello, Docker!” printed in the terminal.

Step 6: Docker Compose — Managing Multi-Container Applications

Docker Compose allows you to define and run multi-container applications using a YAML file.

Example: Running a Python App with Redis

Create a docker-compose.yml file:

version: '3' 
services: 
  app: 
    image: python:3.9 
    volumes: 
      - .:/app 
    working_dir: /app 
    command: python app.py 
  redis: 
    image: redis:latest

Run the application:

docker-compose up

This will start both the Python app and a Redis database.

Step 7: Sharing Images with Docker Hub

To share your image, push it to Docker Hub:

docker login   
docker tag my-python-app your-dockerhub-username/my-python-app   
docker push your-dockerhub-username/my-python-app

Now, others can pull and use your image:

docker pull your-dockerhub-username/my-python-app

Conclusion

Docker makes it easy to develop, deploy, and scale applications consistently across environments. By mastering these basic commands and workflows, you’re on your way to becoming proficient with Docker.

🚀 What’s Next? Explore advanced topics like Docker Networking, Volumes, and Kubernetes for container orchestration!

Got questions? Drop them in the comments! 🚢🔥