FIELD NOTES / CONTAINERS

Introduction to Docker

A practical introduction to containers, images, Compose, networking, persistent storage, and troubleshooting, expanded from my original WordPress guide.

DockerLinuxBeginner guide

Docker is a simple and easy to use program once you learn the basic command structure. Learning docker can be complicated so I have composed this article to serve as a guide for some of the commands I use the most often when I am setting up sites with docker.

What is Docker?

Docker is a containerization platform that allows you to package applications and their dependencies into lightweight, portable containers. These containers run on any system that has Docker installed, eliminating the “works on my machine” problem. Unlike traditional virtual machines, Docker provides an efficient way to deploy, scale, and manage applications without the overhead of a full operating system for each instance.

Docker vs. Virtual Machines (Hypervisors vs. Containers)

To understand Docker, it’s helpful to compare it to traditional virtualization. A hypervisor, such as VirtualBox or VMware, creates and runs multiple virtual machines (VMs) on a host system. Each VM has its own full operating system, requiring significant resources.

Docker, on the other hand, uses containers, which share the host OS kernel while isolating applications and dependencies. This makes containers much lighter and faster than VMs. Instead of booting an entire OS for each instance, Docker only runs the necessary processes, reducing resource consumption and startup times.

FeatureVirtual Machines (Hypervisors)Docker (Containers)
Boot TimeMinutesSeconds
Resource UsageHigh (full OS per VM)Low (shared OS kernel)
IsolationFull OS isolationProcess-level isolation
PortabilityLimited by OS compatibilityHighly portable (works on any system with Docker)

Why Linux is Best for Docker

Linux is the preferred OS for Docker due to its native container support , better performance , and higher stability . Unlike Windows or macOS, which require a virtual machine to run Docker, Linux supports containers directly, reducing overhead and improving efficiency.

It also provides more control over system resources, networking, and storage configurations. Most cloud providers and production environments use Linux for Docker, making it the best choice for development and deployment. If you want a faster, more reliable Docker experience, Linux is the way to go.

Installing Docker on Linux

For this example I will be providing instructions for Debian/Ubuntu distributions of Linux.

Step 1: Update Your System

Before installing Docker, update your package list:

sudo apt update && sudo apt upgrade -y

Step 2: Install Required Dependencies

sudo apt install -y ca-certificates curl gnupg

Step 3: Add Docker’s Official GPG Key

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo tee /etc/apt/keyrings/docker.asc > /dev/null
sudo chmod a+r /etc/apt/keyrings/docker.asc

Step 4: Add Docker’s Repository

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

Step 5: Install Docker

sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Step 6: Start and Enable Docker

sudo systemctl start docker
sudo systemctl enable docker

Step 7: Verify the Installation

Run the following command to check if Docker is installed and running:

docker --version

Test with a simple container:

sudo docker run hello-world

docker compose VS docker run

If you're new to Docker, you might have seen people using long docker run commands to start containers. While this works, it’s not the best way to manage Docker applications, especially if you’re running multiple containers or want to easily share your setup with others.

Instead, Docker provides a better method: YAML configuration files

Here’s why using a YAML configuration files is a better practice:

  1. Easier Deployment - A single .yml file allows you to quickly set up and tear down containers without needing to remember long docker run commands.
  2. Better Maintainability - If you need to modify configurations, it's easier to edit a .yml file than rewriting commands.
  3. Multi-Container Management - Docker Compose allows you to define multiple containers in one file and start them all at once.
  4. Portability – You can share a .yml file with others, allowing them to deploy the same environment easily.
  5. Persistent Storage & Networking – It makes it simple to configure volumes, networks, and dependencies between containers.

Here is an example of a docker run command. This will install a nginx web server.

docker run -d -p 8080:80 --name myweb nginx:latest

in this example the -d means detached, this will make it so the logs do not run in the terminal you run the command into.

The -p 8080:80 sets the internal port for the nginx web service to port 80 and assigns the external port to 8080.

the --name tag makes it a little easier to identify the container when running commands like:

docker ps

the docker ps command will list all of the containers that are currently active.

As previously mentioned, docker run can be great for testing and temporary setups, however, its generally better practice to create a yaml file for the container.

Here is an example of a docker-compose.yml file that runs the same server with the same settings:

services:
  web:
    image: nginx:latest  
    ports:
      - "8080:80"  
    restart: unless-stopped

With restart: unless-stopped, Docker ordinarily restarts the service after a daemon or host restart unless it was deliberately stopped. With restart: always, Docker can restart a manually stopped service when the daemon starts again. Both policies deserve deliberate use.

If you want the container to always reboot, even after host system reboots. You can just change the restart tag to always. Your code would then look like:

services:
  web:
    image: nginx:latest  
    ports:
      - "8080:80"  
    restart: always

for the rest of this tutorial we will be using a yaml file.

Why Use Docker Networks?

By default, Docker containers cannot talk to each other unless they are in the same network. If you define a custom network in your docker-compose.yml file, containers can easily find and communicate with each other using their service names instead of IP addresses.

Here is an example of a yaml file that utilizes docker networks:

services:
  web:
    image: nginx:latest  
    ports:
      - "8080:80"  
    networks:
      - mynetwork  

  database:
    image: mysql:latest  
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
    networks:
      - mynetwork  

networks:
  mynetwork:
    driver: bridge

The bridge driver is used to create an isolated virtual network.

The networks tag at the bottom defines a custom network called mynetwork .

Each service ( web and database ) is assigned to mynetwork , allowing them to communicate.

You will also notice that I have changed the password to use a format that allows you to create a .env file with login credentials

you can create a .env file by running this command in a terminal:

sudo nano .env

.env files are formatted as follows:

MYSQL_ROOT_PASSWORD=examplepassword
DB_USER=admin
DB_NAME=mydatabase

Key Benefits of Using networks

Better Security – Containers in the same network can talk, while others cannot.

Easier Communication – Containers can reference each other by name instead of IP addresses.

Scalability – You can add more services to the network without reconfiguring connections.

Portability – The entire setup is defined in YAML and can be deployed anywhere.

Using the networks tag in Docker Compose is a best practice for managing multi-container applications.

Conclusion

Docker is a powerful tool that simplifies application deployment by using lightweight containers instead of traditional virtual machines. We covered the basics of how Docker works, why Linux is the preferred environment, how to install it, and the advantages of using docker-compose.yml files for managing multi-container setups.

By leveraging Docker, you can create scalable, portable, and easily deployable applications with minimal effort. Whether you're hosting a website, setting up a database, or running a cloud service, Docker makes development and deployment more efficient.

How I organize an actual Docker project

A container is only one part of a reliable deployment. I find it helpful to maintain one folder per application with a Compose file, a small configuration file, and clearly identified storage paths. That way, when something fails, I know which services belong together and what must be backed up. In my own projects, the separation between application files, persistent data, and deployment settings has saved considerable troubleshooting time.

Here is a minimal Compose example suitable for experimenting with a static web server. It binds the preview to the local interface so the service is not automatically exposed to the entire network. A reverse proxy can be added later when remote access is necessary.

services:
  website:
    image: nginx:stable-alpine
    ports:
      - "127.0.0.1:8080:80"
    volumes:
      - ./site:/usr/share/nginx/html:ro
    restart: unless-stopped

Save the file as compose.yaml, create a site directory with an index.html file, and run docker compose up -d from the project directory. You can then open http://127.0.0.1:8080 on the host. The files in site remain on the host and can be edited without treating the container as the permanent storage location.

Volumes, bind mounts, and persistence

One of the easiest mistakes for new users is assuming that files created inside a container will always survive when the container is removed. Containers have writable layers, but those layers are not the same thing as a deliberate backup strategy. A database, uploaded photographs, and application settings should live in persistent storage that is managed independently of any one container instance.

A bind mount maps a chosen host directory into the container. This is convenient for website source files and configuration you want to inspect in an editor. A named volume is managed by Docker and is often a useful choice for application data. Neither automatically replaces the need for backups: you still need copies of important data and a way to test that restoration works.

Networking and reverse proxies

Most Compose applications receive an application-specific network on which services can discover one another by service name. That is convenient for a website container talking to its database, but it does not mean the database should publish its port publicly. Typically only the web-facing service or reverse proxy needs an exposed port. Keeping database ports private reduces accidental exposure.

For services hosted on a home server, I often put a reverse proxy in front of applications so each site has its own hostname and certificate. The proxy needs a route it can actually reach, whether that means a shared Docker network or the host’s LAN address and published port. Binding an upstream service only to 127.0.0.1 on the host will not automatically make it reachable from a different container or remote proxy.

A useful debugging routine

When a container does not behave as expected, I start by checking its state, its logs, the Compose configuration, and the response of the service at the nearest reachable point. That is much faster than changing several network or proxy settings at once.

docker compose config --quiet
docker compose ps
docker compose logs --tail 100
docker ps -a
curl -I http://127.0.0.1:8080

If the local test works but the public website does not, the problem is probably between the reverse proxy, firewall, DNS, TLS configuration, and the service’s published address, not necessarily inside the application. Conversely, if the local test fails, begin with the container and the process inside it before diagnosing the wider network.

Updates, security, and backups

For production deployments, avoid treating a floating image tag such as latest as a reproducible version. Know which image version is running, read its release notes, and back up persistent data before upgrading. It is also worth avoiding unnecessary privileged containers, minimizing exposed ports, and keeping secrets out of publicly committed Compose files. Environment files are a convenience, not secure secret storage by themselves.

My favorite aspect of Docker is that the running service can be recreated from a small collection of documented files. The goal is not merely to run a container once; it is to understand what the service depends on and be able to repair, move, or restore it without guessing. That principle has become central to how I host websites and maintain my own systems.