Docker For Beginners

You're building an app that works perfectly on your computer. But when you send it to your friend or deploy it on a server, it crashes. Sound familiar? 😩 In today’s fast-paced world of app development, consistency is everything. That’s where Docker comes in. Like a magic box 📦 that packs your app with everything it needs to run anywhere, without surprises. Docker has completely changed the game in how we develop, ship, and deploy applications using something called containerization.

What is Docker?

Docker is a platform that allows you to package an application and all its dependencies (like libraries and settings) into a single unit called a container. This ensures that your application runs the same way on any computer, regardless of differences in operating systems or installed software. It includes:

  • Your application code
  • Dependencies (libraries, frameworks)
  • Configuration files

Think of Docker like a shipping container:

  • Standardizes deployment across environments
  • Runs identically on laptops, servers, or the cloud

Why Use Docker?

Benefit Description
Consistency No more works on my machine issues
Portability Run anywhere Docker is installed
Efficiency Lightweight (shares OS resources)
Scalability Spin up multiple containers in seconds
Isolation Apps run separately without conflicts

Containers vs. Virtual Machines

Docker Containers Virtual Machines (VMs)
🚀 Lightweight & fast 🐢 Heavier & slower
⚡ Shares host OS kernel 🔄 Requires full OS per VM
📦 Process-level isolation 🛡️ Hardware-level isolation
💾 Minimal resource usage 💽 High storage/memory consumption

Core Docker Components

  1. Image
    • Blueprint for containers (e.g., python:3.11-slim)
  2. Container
    • Running instance of an image
  3. Dockerfile
    • Recipe to build images (see example below)
  4. Docker Hub
    • Public registry for sharing images

Install Docker

  1. Windows/Mac: Download and install Docker Desktop from Docker Desktop.
  2. Verify that Docker is installed by opening a terminal or command prompt and running:
1docker --version
  1. Linux: Install Docker using your package manager. For e.g in Ubuntu:
1sudo apt-get update
2sudo apt-get install docker.io
  1. Verify that Docker is installed by running:
1docker --version

Step-by-Step Docker Example Using Python

1. First create a folder called docker-for-beginners

2. Inside the folder, create a python file called app.py with the following content:

1print("Hello, Docker!")

3. Create a file called Dockerfile with the following content:

 1# Use the official Python image from Docker Hub
 2FROM python:3.11-slim
 3
 4# Set the working directory in the container
 5WORKDIR /app
 6
 7# Copy the current directory contents into the container at /app
 8COPY . /app
 9
10# Command to run when the container starts
11CMD ["python", "app.py"]

4. Build the Docker image:

1docker build -t my-python-app .
  • docker build tells Docker to build an image.
  • -t my-python-app tags the image with a name.
  • . refers to the current directory, which contains your Dockerfile and app.

5. Run the Docker container:

1docker run my-python-app
6. Verify that the container is running:

You will get following output:

1Hello, Docker!

Essential Commands Cheatsheet

Command Description Example
docker build Build image docker build -t myapp .
docker run Start container docker run -d -p 8000:8000 myapp
docker ps List containers docker ps -a
docker images List images docker images
docker pull Download image docker pull ubuntu
docker exec Run command in container docker exec -it myapp bash