TechBuzzPro

Docker Mistakes I Made When First Starting Out

· 5 min read · Apps & Browsers

In this article
  1. Background: Why Docker Feels Different
  2. Mistake 1: Treating Containers Like VMs
  3. Mistake 2: Ignoring Image Size
  4. Mistake 3: Not Using Docker Compose for Multi-Container Apps
  5. Mistake 4: Forgetting About Persistent Data
  6. Mistake 5: Hardcoding Environment Variables
  7. Mistake 6: Not Understanding the Build Context
  8. Mistake 7: Running Containers as Root
  9. Frequently Asked Questions

The biggest mistakes I made when I first started using Docker were treating containers like lightweight virtual machines and ignoring image size, which led to bloated images, slow builds, and confusing debugging sessions. Below, I break down the most common pitfalls I encountered, along with practical advice on how to avoid them.

Background: Why Docker Feels Different

Docker packages an application and its dependencies into a container, which runs on a shared operating system kernel. Unlike a virtual machine, a container doesn’t include a full OS—it just has the binaries, libraries, and configuration files needed for your app. This makes containers fast to start and resource-efficient, but it also means they are ephemeral and stateless by default. Understanding this mental model is the first step to avoiding mistakes.

Mistake 1: Treating Containers Like VMs

I used to SSH into a running container to debug, install tools, and make changes—just like I would with a server. That’s a trap. Containers are designed to be disposable; any changes you make inside a running container are lost when the container stops and is removed. Instead, you should:

  • Use docker exec -it <container> /bin/bash only for quick inspection, not for permanent changes.
  • Define all configuration in a Dockerfile or in a configuration management tool.
  • Use volumes for persistent data (like databases or uploaded files).

If you find yourself manually installing packages inside a container, it’s a sign your image is incomplete. Update the Dockerfile and rebuild.

Mistake 2: Ignoring Image Size

My first Dockerfile used FROM ubuntu:latest and installed a ton of unnecessary packages. The image was over 1 GB, and every build took minutes. I didn’t realize that smaller images mean faster pulls, less disk usage, and fewer security vulnerabilities.

Here’s what I learned:

  • Use a minimal base like alpine or a slim variant (e.g., python:3.12-slim).
  • Combine RUN commands to reduce layers (e.g., RUN apt-get update && apt-get install -y ...).
  • Clean up package caches in the same RUN step (e.g., rm -rf /var/lib/apt/lists/*).
  • Use .dockerignore to exclude local files like node_modules or .git from the build context.

For a simple Python app, switching from python:3.12 to python:3.12-slim reduced my image from ~900 MB to ~150 MB.

Mistake 3: Not Using Docker Compose for Multi-Container Apps

When my app needed a database and a cache, I started running separate docker run commands for each container. I had to remember ports, network names, and environment variables. It was a nightmare. Docker Compose lets you define all services in a single docker-compose.yml file and start them with docker compose up.

Example snippet:

services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: redis:alpine

Compose also sets up a default network so containers can talk to each other by service name. This is the standard way to run multi-container apps locally and in production (though in production you might use Kubernetes or Docker Swarm).

Mistake 4: Forgetting About Persistent Data

I once ran a PostgreSQL container, put some test data in it, and then stopped the container. When I restarted it, the data was gone. I had forgotten that containers are ephemeral. The fix is to use a volume or a bind mount:

  • Named volume: docker run -v mydata:/var/lib/postgresql/data postgres
  • Bind mount: docker run -v /host/path:/container/path ...

In Docker Compose, you can declare volumes under the service. Always plan where your data lives before you start a stateful service.

Mistake 5: Hardcoding Environment Variables

I used to put database passwords and API keys directly in my Dockerfile or in the docker run command. That’s insecure and inflexible. The correct approach is to use environment variables at runtime:

  • Pass them with -e MY_VAR=value or --env-file .env.
  • In Compose, use the environment key or the env_file directive.
  • For secrets, use Docker secrets (in Swarm) or a tool like Vault.

Never bake secrets into an image—they can be extracted from the image layers.

Mistake 6: Not Understanding the Build Context

The docker build command sends the entire directory (the build context) to the Docker daemon. I once had a folder with gigabytes of logs and temporary files, and every build was painfully slow. The solution is a .dockerignore file, which works like .gitignore. List files and directories you don’t need in the build, such as node_modules, .git, and *.log. This speeds up builds and avoids accidentally copying sensitive files.

Mistake 7: Running Containers as Root

By default, containers run as root, which is a security risk. If an attacker compromises your container, they may gain root access to the host (though Docker does add some isolation). I now create a non-root user in my Dockerfile:

RUN useradd -m appuser
USER appuser

This is a simple step that significantly reduces the attack surface. Also, avoid mounting the Docker socket (/var/run/docker.sock) into a container unless absolutely necessary—it gives the container full control over the host.

Frequently Asked Questions

How do I debug a container that won't start?

First, check the logs with docker logs <container>. If the container exits immediately, run it in the foreground with docker run --rm -it <image> /bin/sh to see errors interactively. Also, inspect the configuration with docker inspect to see environment variables, mounts, and network settings.

What's the difference between an image and a container?

An image is a read-only template with instructions for creating a container. A container is a runnable instance of an image—it has a writable layer, a process, and network interfaces. You can have many containers from the same image.

Should I use Docker in production?

Yes, but with orchestration. Running a single Docker container on a server is fine for small apps, but for reliability and scaling, use Docker Compose (for a single host) or Kubernetes (for multiple hosts). Docker itself is just the runtime; orchestration handles restarts, scaling, and load balancing.

How do I reduce Docker image size?

Use a slim or alpine base image, combine RUN commands, remove package caches, and use multi-stage builds. Multi-stage builds let you compile code in one stage and copy only the artifacts to a smaller final image.

Is Docker free?

Docker Engine is open source and free. Docker Desktop (for Windows and Mac) is free for small businesses and personal use, but larger companies may need a paid subscription. The core command-line tools and daemon are free.

How do I persist data when using Docker Compose?

Define a volume under the service in your docker-compose.yml. For example:

services:
  db:
    image: postgres
    volumes:
      - db-data:/var/lib/postgresql/data
volumes:
  db-data:

This creates a named volume that survives container restarts and re-creations.