DevOps & Containers

What Is Docker? A Beginner's Guide to Images, Containers and Compose

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

Docker is a platform for building, shipping and running applications in containers. Instead of installing a web server, a database and a language runtime directly on your server and hoping their versions get along, you run each one in its own isolated container, started from an image that already contains everything it needs. If you are new to the idea, this guide walks through the core concepts, the commands you will use every day and a first multi-container setup.

The core concepts

Image

An image is a read-only template: a filesystem with an application, its libraries and a default command. Images are identified by a name and a tag, such as nginx:1.27 or postgres:16. You download them from a registry, Docker Hub being the default, or build your own.

Container

A container is a running instance of an image. You can start many containers from the same image. Each one gets its own filesystem layer, process space and network interface, but they all share the host's Linux kernel, which is why containers start quickly and use little overhead. For the theory behind this, read containerization explained.

Dockerfile

A Dockerfile is a text file with instructions to build an image.

Volume

Container filesystems are disposable. A volume is storage managed by Docker that survives when containers are removed, used for databases and uploaded files.

Network

Docker creates virtual networks so containers can reach each other by name, while only the ports you choose are exposed to the outside.

Installing Docker on Ubuntu or Debian

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo docker version

The script adds Docker's official repository and installs Docker Engine, the CLI and the Compose plugin. Review scripts before running them. To use Docker without sudo, add your user to the docker group (sudo usermod -aG docker $USER) and log in again; be aware that membership in this group is effectively root access.

Essential commands

TaskCommand
Download an imagedocker pull nginx:1.27
Run a container in the backgrounddocker run -d --name web -p 8080:80 nginx:1.27
List running containersdocker ps
List all containersdocker ps -a
View logsdocker logs -f web
Open a shell insidedocker exec -it web sh
Stop and removedocker stop web && docker rm web
List imagesdocker images
Live resource usagedocker stats
Clean unused datadocker system prune

In -p 8080:80, the first number is the host port and the second is the port inside the container.

Building your own image

A minimal Dockerfile for a Node.js application:

FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
USER node
CMD ["node", "server.js"]

Build and run it:

docker build -t myapp:1.0.0 .
docker run -d --name myapp -p 3000:3000 myapp:1.0.0

Copying package*.json and installing dependencies before copying the rest of the code lets Docker reuse the cached dependency layer when only your source code changes, which makes rebuilds much faster. Add a .dockerignore file to exclude node_modules, .git and secrets from the build context.

Docker Compose: several containers together

Real applications usually need more than one service. Docker Compose describes them in a single compose.yaml file:

services:
  app:
    image: myapp:1.0.0
    restart: unless-stopped
    environment:
      DATABASE_URL: postgres://app:change-me@db:5432/app
    ports:
      - "127.0.0.1:3000:3000"
    depends_on:
      - db

  db:
    image: postgres:16
    restart: unless-stopped
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: change-me
      POSTGRES_DB: app
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  db-data:

Start everything with docker compose up -d, follow logs with docker compose logs -f, and stop with docker compose down. The app reaches the database at the hostname db because both share the network Compose creates. The database has no published port, so it is not reachable from the internet. In a real setup, keep passwords in an .env file that is not committed to Git.

Running Docker on a VPS: practical tips

  • Put a reverse proxy in front. Bind apps to 127.0.0.1 and let Nginx or Caddy handle ports 80 and 443 with TLS.
  • Mind the firewall. Docker writes its own iptables rules, and published ports can bypass ufw. Binding to 127.0.0.1 avoids accidental exposure.
  • Use restart policies (unless-stopped) so containers return after a reboot.
  • Limit log size in /etc/docker/daemon.json:
{
  "log-driver": "json-file",
  "log-opts": { "max-size": "10m", "max-file": "3" }
}
  • Back up volumes, not just the server. A database dump (docker compose exec db pg_dump -U app app > backup.sql) is easier to restore than raw files.
  • Pin versions and update deliberately: docker compose pull && docker compose up -d.
  • Watch disk space. Old images accumulate; prune them periodically.

A Linux VPS with 2 GB of RAM is a comfortable starting point for a small app and database; check the plans on our pricing page and size up as your stack grows.

FAQ

Is Docker free to use?

Docker Engine, the open-source runtime used on Linux servers, is free. Docker Desktop, the application for Windows and macOS, has licensing terms that require a paid subscription for some larger commercial organizations, so check its current terms if that applies to you.

What is the difference between Docker and a virtual machine?

A virtual machine runs a complete operating system with its own kernel on top of a hypervisor. A Docker container is an isolated process that shares the host kernel. Containers are lighter and start faster, while VMs offer stronger isolation. Running Docker inside a VPS combines both.

Should I run my database in Docker?

It works well for many small and medium projects, provided data lives in a volume and you take regular dumps. For large or critical databases, some teams prefer a dedicated server or a managed service for easier tuning and recovery.

Related guides