🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

Docker for Beginners: Your First Container in 15 Minutes

Docker for Beginners: Your First Container in 15 Minutes

Docker has revolutionized how we build, ship, and run applications. Instead of worrying about dependencies, library versions, and environment differences, you package everything your app needs into a container that runs identically everywhere — on your laptop, a test server, or in production.

This guide gets you from zero to running your first containerized application in 15 minutes, then takes you deeper into the concepts you need for real-world use.

What Is Docker and Why Should You Care?

Think of Docker containers as lightweight, isolated environments for running applications. Unlike virtual machines, containers share the host operating system's kernel, making them:

  • Fast to start: Containers launch in seconds, not minutes
  • Lightweight: A container image is typically 10-200 MB, not gigabytes
  • Portable: Build once, run anywhere — no "works on my machine" problems
  • Isolated: Each container has its own filesystem, network, and processes

Installing Docker

The easiest way to install Docker on a Linux server:

# Official installation script
curl -fsSL https://get.docker.com | bash

# Add your user to the docker group (avoid sudo)
sudo usermod -aG docker $USER

# Log out and back in, then verify
docker --version
docker run hello-world

For Ubuntu/Debian manual installation:

# Remove old versions
sudo apt remove docker docker-engine docker.io containerd runc

# Install prerequisites
sudo apt update
sudo apt install ca-certificates curl gnupg

# Add Docker GPG key and repository
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
  sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

echo "deb [arch=$(dpkg --print-architecture) \
  signed-by=/etc/apt/keyrings/docker.gpg] \
  https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list

# Install Docker Engine
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin

Your First Container

Let's run an Nginx web server in a container — it takes one command:

# Run Nginx, map port 8080 on host to port 80 in container
docker run -d --name my-web -p 8080:80 nginx

# Verify it is running
docker ps

# Visit http://localhost:8080 in your browser
# You should see the Nginx welcome page!

# View container logs
docker logs my-web

# Stop and remove the container
docker stop my-web
docker rm my-web

That's it — you just ran a production-grade web server in an isolated container with a single command.

Essential Docker Commands

CommandWhat It Does
docker run IMAGECreate and start a new container
docker psList running containers
docker ps -aList all containers (including stopped)
docker stop NAMEStop a running container
docker rm NAMERemove a stopped container
docker imagesList downloaded images
docker logs NAMEView container output
docker exec -it NAME bashOpen a shell inside a running container
docker build -t NAME .Build an image from a Dockerfile

Building Your Own Image with a Dockerfile

A Dockerfile is a recipe that tells Docker how to build your application image. Here is a practical example for a Python web application:

# Dockerfile
FROM python:3.12-slim

# Set working directory
WORKDIR /app

# Copy requirements first (for better caching)
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY . .

# Expose the port your app uses
EXPOSE 5000

# Start the application
CMD ["python", "app.py"]

Build and run it:

# Build the image
docker build -t my-python-app .

# Run a container from your image
docker run -d --name my-app -p 5000:5000 my-python-app

# Check the logs
docker logs -f my-app

Dockerfile Best Practices

  • Use slim base images: python:3.12-slim instead of python:3.12 (90% smaller)
  • Copy dependency files first: Changes to app code won't rebuild dependencies
  • Use .dockerignore: Exclude .git, node_modules, __pycache__ from the image
  • Don't run as root: Add USER appuser for security
  • One process per container: Don't run multiple services in one container

📚 Recommended Reading

Go deeper with Docker — from fundamentals to production:

Docker Compose — Multi-Container Applications

Real applications often need multiple services: a web server, a database, a cache. Docker Compose lets you define and manage multi-container setups in a single YAML file.

Example — a WordPress site with MySQL:

# docker-compose.yml
services:
  wordpress:
    image: wordpress:latest
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wp_user
      WORDPRESS_DB_PASSWORD: secure_password
    volumes:
      - wp_data:/var/www/html
    depends_on:
      - db

  db:
    image: mysql:8.0
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wp_user
      MYSQL_PASSWORD: secure_password
      MYSQL_ROOT_PASSWORD: root_password
    volumes:
      - db_data:/var/lib/mysql

volumes:
  wp_data:
  db_data:
# Start all services
docker compose up -d

# View running containers
docker compose ps

# View logs for all services
docker compose logs -f

# Stop everything
docker compose down

# Stop and remove volumes (data)
docker compose down -v

Data Persistence with Volumes

Containers are ephemeral — when you remove a container, its data is gone. Use Docker volumes to persist data:

# Create a named volume
docker volume create my-data

# Run container with volume mounted
docker run -d --name my-db \
  -v my-data:/var/lib/postgresql/data \
  -e POSTGRES_PASSWORD=secret \
  postgres:16

# List volumes
docker volume ls

# Inspect volume details
docker volume inspect my-data

Networking Basics

Docker creates isolated networks for your containers. Containers on the same network can communicate by container name:

# Create a custom network
docker network create my-app-network

# Run containers on the same network
docker run -d --name my-db --network my-app-network postgres:16
docker run -d --name my-app --network my-app-network my-python-app

# my-app can now connect to my-db using hostname "my-db"

📚 Scale Up Your Container Skills

Ready for multi-container and orchestration?

Cleaning Up

Docker can consume significant disk space over time. Regular cleanup keeps your system healthy:

# Remove all stopped containers
docker container prune

# Remove unused images
docker image prune -a

# Remove unused volumes
docker volume prune

# Nuclear option — remove everything unused
docker system prune -a --volumes

# Check disk usage
docker system df

Conclusion

Docker transforms how you develop, test, and deploy applications. You have learned how to run containers, build custom images, manage multi-container setups with Compose, and handle data persistence. These skills apply whether you are developing locally, deploying to a VPS, or working with cloud platforms.

Start by containerizing one of your existing applications. Once you experience the consistency and portability Docker provides, you will never want to go back to "install everything on the host" deployments.

Share this article:

Stay Updated

Subscribe to our newsletter for the latest tutorials, tips, and exclusive offers.