Understanding Docker Containers for Beginners

Last edited: July 6, 2025

Introduction

Docker has revolutionized how developers build, ship, and run applications by introducing containerization to the mainstream. In this guide, we’ll help you understand Docker containers, their benefits, and how to start using them effectively.

What Are Docker Containers?

Containers are lightweight, standalone, executable packages that include everything needed to run an application:

Unlike virtual machines, containers:

Why Use Docker Containers?

Docker Architecture

Docker uses a client-server architecture:

  1. Docker Client: The command-line interface you interact with
  2. Docker Daemon: The background service that builds and runs containers
  3. Docker Registry: Stores Docker images (e.g., Docker Hub)

Essential Docker Concepts

Images vs. Containers

You can think of an image as a class and a container as an object of that class.

Dockerfile

A text file containing instructions to build a Docker image:

# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory
WORKDIR /app

# Copy requirements and install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy the current directory contents
COPY . .

# Make port 5000 available
EXPOSE 5000

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

Basic Docker Commands

Managing Images

# Pull an image from Docker Hub
docker pull ubuntu:20.04

# List downloaded images
docker images

# Build an image from a Dockerfile
docker build -t myapp:1.0 .

# Remove an image
docker rmi ubuntu:20.04

Managing Containers

# Run a container
docker run -d -p 8080:80 --name webserver nginx

# List running containers
docker ps

# List all containers (including stopped)
docker ps -a

# Stop a container
docker stop webserver

# Start a stopped container
docker start webserver

# Remove a container
docker rm webserver

Working with Containers

# Execute a command in a running container
docker exec -it webserver bash

# View container logs
docker logs webserver

# Copy files between host and container
docker cp ./local-file.txt webserver:/app/

Docker Compose

Docker Compose simplifies managing multi-container applications:

# docker-compose.yml
version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/app
    depends_on:
      - db
  db:
    image: postgres:13
    environment:
      POSTGRES_PASSWORD: example
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

Run with:

docker-compose up -d

Best Practices

  1. Keep Images Small: Use minimal base images like Alpine
  2. One Service Per Container: Follow the single responsibility principle
  3. Use Volume Mounts: For persistent data
  4. Leverage Build Cache: Order Dockerfile commands from least to most frequently changing
  5. Don’t Run as Root: Use the USER instruction to specify a non-root user
  6. Use .dockerignore: Exclude unnecessary files from the build context
  7. Tag Images Properly: Use meaningful version tags

Common Issues and Solutions

Container Won’t Start

Check logs for errors:

docker logs <container_id>

Permission Issues

Ensure proper file permissions or run as a specific user:

RUN chown -R user:user /app
USER user

Network Connectivity

Create a custom network for container communication:

docker network create mynetwork
docker run --network=mynetwork --name=container1 image1

Docker in Production

For production environments, consider:

Conclusion

Docker containers provide a powerful way to package, distribute, and run applications consistently across different environments. Now that we’ve covered the basics in this guide, you’re equipped to start containerizing your applications and exploring more advanced Docker features. We encourage you to experiment with the commands and concepts we’ve discussed to gain hands-on experience with Docker.

Further Resources