Docker on a VPS: A Practical Starting Point

Cover graphic: Docker on a VPS: A Practical Starting Point

Core takeaway

  • What Docker does: it runs each program in its own self-contained package. You can start, stop or delete one without affecting the rest of the server.
  • Biggest beginner mistake: accidentally exposing a service to the whole internet. The guide explains the firewall trap and why the example binds to 127.0.0.1.
  • Keep data outside the container: use volumes. A container is disposable; your data should not be.
  • What you build: install Docker, run your first containers, then run a real two-part application with one Docker Compose file.

Disclosure: con1ainer.com participates in affiliate programs. This article contains no affiliate links. See the Affiliate & Commercial Disclosure.

The 15-second version of the mistake that catches most Docker beginners.

Most software does not arrive as one tidy file. It arrives as a program, plus a database, plus the right version of a language, plus a list of settings. Installing two such programs on one server can mean they fight over the same libraries, and removing one can leave junk behind.

Docker exists to stop that.

It lets you run each program inside its own sealed, self-contained package, with everything the program needs already included. You start the package, you stop it, you delete it, and the rest of the server is unaffected.

This guide takes you from a blank VPS to a working Docker setup. You will install Docker, run your first containers, learn how to keep data safe, run a real two-part application with one file, and avoid the most common mistake new Docker users make: accidentally exposing a service to the whole internet.

What Docker Is

Docker is software that runs applications inside containers. A container is an isolated process, or a small group of processes, that has its own view of the file system, its own network settings, and its own set of installed software, while sharing the server’s operating system core (the kernel) with every other container.

That last part is the difference between a container and a virtual machine. Your VPS is already a virtual machine: a hypervisor gave it its own operating system and its own slice of hardware, as explained in What Is a VPS and How Does It Work? A container sits one level lower and is much lighter.

FeatureVirtual machine (your VPS)Container
Has its own operating system kernelYesNo, shares the host’s
Typical start timeTens of seconds to minutesUsually a second or two
Typical sizeGigabytesTens to hundreds of megabytes
IsolationStrong, enforced by the hypervisorGood, but weaker than a virtual machine
Best forA whole server environmentOne application and its dependencies

You will usually run containers inside your VPS, not instead of it.

The Words You Will See Constantly

Docker has its own vocabulary, and most beginner confusion comes from mixing these terms up.

TermWhat it means
ImageA read-only template containing an application and everything it needs. Think of it as the installer.
ContainerA running copy of an image. Think of it as the installed, running program. You can start many containers from one image.
RegistryA website that stores images for download. Docker Hub is the default and largest one.
VolumeA storage area managed by Docker that survives when a container is deleted.
Port mappingA rule that connects a port on the server to a port inside a container.
Docker ComposeA tool that starts several containers together from one text file.
Reverse proxyA web server in front of your containers that receives visitors and passes requests to the right container.

What You Need Before You Start

  • A VPS running Ubuntu 24.04 LTS (long-term support, meaning security updates for years). Other Debian-based systems are nearly identical.
  • A non-root user with sudo, and a firewall. Read next: How to Secure a VPS: Essential Setup Checklist
  • Enough memory. Docker itself is light. What costs memory is the applications inside the containers.
WorkloadTypical starting point
A few small single-purpose containers1 GB RAM
A WordPress site with its database2 GB RAM
Several applications plus databases4 GB RAM
Many containers or heavy applications8 GB RAM or more, sized from measured use

These are starting points, not promises. For the reasoning behind them, read How Much RAM Does a Web Server Need?

The commands below use 203.0.113.25 as a stand-in for your VPS IP address. Replace it with yours.

Step 1: Install Docker

Ubuntu’s own software list contains an old copy of Docker. Use Docker’s official repository instead, so that you receive current versions and updates.

First, install the tools needed to download and verify Docker’s signing key. The key lets your server confirm that packages really come from Docker.

sudo apt update
sudo apt install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

Next, tell the system where Docker’s packages live:

sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Signed-By: /etc/apt/keyrings/docker.asc
EOF

Then install it:

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

Check that both Docker and Compose are present:

docker --version
docker compose version

Finally, run the official test image:

sudo docker run hello-world

Docker downloads a tiny image, starts a container from it, prints a message confirming that everything works, and exits. If you see that message, the installation is complete.

Run Docker without typing sudo

By default only the administrator can use Docker. To let your normal user run it:

sudo usermod -aG docker $USER

Log out and back in, then test with docker ps. One warning applies here: anyone in the docker group can effectively become root on the server, because Docker can mount any folder and run commands inside it. Add only users you would trust with full control.

Step 2: Run Your First Real Container

Start a web server in a container:

docker run -d --name web -p 127.0.0.1:8080:80 nginx

Every part of that command matters:

  • docker run creates and starts a container.
  • -d means detached. The container runs in the background instead of holding your terminal.
  • --name web gives the container a name you choose. Without it Docker invents a random one.
  • -p 127.0.0.1:8080:80 is the port mapping. Read it from the right: port 80 inside the container is connected to port 8080 on the server, and only to requests coming from the server itself (127.0.0.1).
  • nginx is the image name. Docker downloads it from Docker Hub the first time.

Test it:

curl -I http://127.0.0.1:8080

A response starting with HTTP/1.1 200 OK means the container is serving the default Nginx welcome page.

The firewall trap: why 127.0.0.1 is in that command

This is the most important safety fact in the guide.

If you had written -p 8080:80 without 127.0.0.1, Docker would have published the port on every network interface, including the public one. Docker does this by adding its own rules to the server’s packet filter (iptables), and those rules are processed before the rules created by UFW, the simple firewall tool most Ubuntu guides recommend.

The result is that UFW can say a port is blocked, while the port is open to the world anyway.

The safe habit is simple: publish ports on 127.0.0.1 unless you deliberately want that service reachable from the internet. Put a reverse proxy in front for anything public (Step 5 shows how).

Step 3: The Commands You Will Use Every Day

CommandWhat it does
docker psLists running containers
docker ps -aLists all containers, including stopped ones
docker logs webShows what the container has printed. Add -f to follow new lines live
docker stop webStops the container
docker start webStarts a stopped container
docker restart webStops, then starts it
docker rm webDeletes a stopped container
docker imagesLists images downloaded to the server
docker pull nginxDownloads or updates an image
docker exec -it web shOpens a shell inside the running container
docker statsShows live CPU and memory use per container

When a container misbehaves, the first command is almost always docker logs. Containers that exit immediately usually print the reason before they die.

Step 4: Keep Your Data Safe With Volumes

A container’s own file system is disposable. When you delete the container, everything written inside it is deleted too. That is a feature for software and a disaster for databases, uploads, and settings.

A volume solves this. It stores data outside the container and attaches it at a folder path inside.

docker rm -f web
docker run -d --name web -p 127.0.0.1:8080:80 -v web_content:/usr/share/nginx/html nginx

The new part is -v web_content:/usr/share/nginx/html. It means: attach the Docker-managed volume called web_content at the folder where Nginx keeps its pages. Delete and recreate the container as many times as you like. The volume and its files remain.

There are two kinds of storage you will meet:

TypeHow it looksBest for
Named volume-v web_content:/pathDatabases and application data. Docker manages the location
Bind mount-v /srv/site:/pathFiles you want to edit directly on the server, such as a config file or a website folder

Docker stores named volumes under /var/lib/docker/volumes/. Know that they exist, because your backups must include them. Read next: VPS Backup Strategy: How to Back Up a VPS and Actually Restore It

Step 5: Run a Real Application With Docker Compose

Typing long docker run commands is error-prone, and you cannot easily review or repeat them. Docker Compose replaces them with one file that describes your whole setup.

This example runs WordPress and its MariaDB database as two connected containers. Create a folder and the file:

mkdir -p ~/wordpress && cd ~/wordpress
nano compose.yaml
services:
  db:
    image: mariadb:11
    restart: unless-stopped
    environment:
      MARIADB_DATABASE: wordpress
      MARIADB_USER: wp_user
      MARIADB_PASSWORD: ${DB_PASSWORD}
      MARIADB_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db_data:/var/lib/mysql

  wordpress:
    image: wordpress:6
    restart: unless-stopped
    depends_on:
      - db
    ports:
      - 127.0.0.1:8080:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wp_user
      WORDPRESS_DB_PASSWORD: ${DB_PASSWORD}
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - wp_data:/var/www/html

volumes:
  db_data:
  wp_data:

What the important lines do:

  • services lists the containers. Here there are two, named db and wordpress.
  • image: mariadb:11 names the image and a version tag. Pinning a version means an update never changes the major version without your decision. Avoid the latest tag for anything you care about, because it can change under you.
  • restart: unless-stopped brings the container back after a crash or a server reboot, unless you stopped it yourself.
  • WORDPRESS_DB_HOST: db works because Compose puts both containers on a private network where each can reach the other by its service name. The database has no published port at all, so nothing outside can touch it.
  • ${DB_PASSWORD} reads the password from a separate file instead of writing it into this one.
  • volumes at the bottom declares the two named volumes, so the database and the WordPress files survive container deletion.

Create the password file next to it. Use a long random password:

nano .env
DB_PASSWORD=replace-with-a-long-random-password

Start everything:

docker compose up -d
docker compose ps

Compose downloads the images, creates the network and volumes, and starts both containers in the right order. docker compose ps should show both as running.

The daily Compose commands:

CommandWhat it does
docker compose up -dCreates and starts everything in the file
docker compose psShows the status of each service
docker compose logs -fFollows the output of every service
docker compose pullDownloads newer images for the versions you pinned
docker compose downStops and removes the containers. Volumes stay
docker compose down -vStops and removes the containers and deletes the volumes. Your data is gone

The last row is the dangerous one. Read the command twice before you press Enter.

Reaching the application

Because the port is published on 127.0.0.1, you cannot open it from your own computer yet. That is intentional. You have two ways in.

For testing: an SSH tunnel. This forwards a port on your own computer to the server through your encrypted SSH connection. Run this on your own computer:

ssh -L 8080:127.0.0.1:8080 youruser@203.0.113.25

Leave it running, then open http://localhost:8080 in your browser. You are viewing the container, and nobody else can.

For visitors: a reverse proxy. Install Nginx on the VPS itself, and let it receive public traffic on ports 80 and 443 and pass requests to the container. Inside your Nginx site configuration, the key block is:

location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

proxy_pass sends the request on to the container. The proxy_set_header lines pass along the original domain name, the visitor’s real address, and whether they used HTTPS, because otherwise the application sees every visitor as coming from the proxy. The full server block, the firewall rules, and the HTTPS certificate steps are the same ones used in How to Move a Website to a VPS Without Downtime.

Step 6: Housekeeping That Prevents Surprises

Containers are tidy. Docker itself slowly accumulates clutter. Three habits stop it from filling your disk.

Check disk use.

docker system df

It reports the space used by images, containers, volumes, and the build cache.

Remove what you no longer use.

docker system prune

This deletes stopped containers, unused networks, unreferenced images, and the build cache. It does not delete volumes unless you add --volumes. Docker shows what it will remove and asks you to confirm.

Limit log growth. By default Docker keeps every line a container prints, forever, in a file on the disk. A noisy container can fill a small VPS. Create a settings file:

sudo nano /etc/docker/daemon.json
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}

That keeps at most three log files of 10 MB each per container. Apply it with sudo systemctl restart docker. The setting affects containers created afterwards, so recreate existing ones with docker compose up -d --force-recreate.

Limit memory if a container is greedy. Add mem_limit: 512m under a service in the Compose file. If that container exceeds its limit, Docker stops it instead of letting it starve everything else on the server. Watch live use with docker stats.

To see which containers are using resources and why a VPS feels slow, read next: Monitor a VPS: Uptime, Resources, and Alerts

Docker Security Rules Worth Following

RuleWhy
Publish ports on 127.0.0.1 unless the service must be publicDocker bypasses UFW, so a plain -p can expose a service you thought was blocked
Use official images or images from publishers you can verifyAn image is code running on your server. A bad one can include anything
Pin version tags, not latestA surprise major update can break the application or its data
Never use --privileged unless you can explain whyIt removes most of the isolation that makes containers safe
Never mount /var/run/docker.sock into a container casuallyAccess to that file is full control of Docker, which means full control of the server
Keep passwords in a .env file or secrets store, not in the Compose fileThe Compose file is the file you will copy, share, and paste into forums
Update images regularlyImages contain software with security fixes, just like the operating system

The update routine for a Compose setup is short:

cd ~/wordpress
docker compose pull
docker compose up -d

Compose recreates only the containers whose images changed. Take a backup first for anything important.

Backing Up Containers

The containers themselves are disposable, so you do not back them up. You back up three things: the Compose file, the .env file, and the data inside the volumes.

For a database, export it in a form that is safe to restore, rather than copying live database files:

docker compose exec -T db sh -c 'mariadb-dump -u"$MARIADB_USER" -p"$MARIADB_PASSWORD" "$MARIADB_DATABASE"' > ~/wordpress-backup.sql
chmod 600 ~/wordpress-backup.sql

Compose runs the dump command inside the db container and writes the result to a file on the server. The -T flag turns off the pseudo-terminal Compose normally allocates, which keeps the dump clean when you redirect it to a file. The password comes from the container’s own environment, so there is nothing to type. Then copy that file, and the Compose files, somewhere that is not this server. A backup on the same disk disappears together with the disk.

Common Problems and What They Mean

SymptomLikely causeFix
permission denied when running dockerYour user is not in the docker group, or you have not logged out and in since adding itRun the usermod command from Step 1, then log out and back in
port is already allocatedAnother container or program uses that portChoose a different left-hand port number, such as 127.0.0.1:8081:80
Container shows Exited straight after startingThe application failed on startupRun docker logs name and read the last lines
Browser cannot reach the container from the internetThe port is bound to 127.0.0.1, which is by designUse an SSH tunnel to test, or a reverse proxy to publish
Container keeps restartingA crash loop caused by a configuration error or a missing passwordRun docker compose logs -f and fix the error shown
no space left on deviceImages, logs, or volumes filled the diskRun docker system df, then docker system prune and set the log limits
Data vanished after recreating a containerThe data was inside the container, not on a volumeAdd a volume for that folder. Data that is already gone cannot be recovered from Docker

When Docker Is Not the Right Tool

Docker is excellent for self-hosted applications, development environments, and running several isolated services on one server. It is not always necessary.

  • A single WordPress site on a small VPS is often simpler without Docker. A normal Nginx, PHP, and MariaDB install has fewer layers to learn and debug.
  • A server with 512 MB of RAM or less leaves little room for the containers’ applications themselves.
  • Anything you do not understand well enough to back up should be understood first. Docker hides complexity, but it does not remove it.

Frequently Asked Questions

Is Docker free?

Docker Engine, the software installed in this guide, is open source and free to use on Linux servers. Docker Desktop, the version for Windows and macOS computers, has separate license terms for larger businesses.

What is the difference between Docker and Docker Compose?

Docker runs one container at a time from a command. Compose reads a file that describes several containers, their networks, and their volumes, and starts them together. For anything beyond a single quick test, use Compose, because the file doubles as documentation of how the application is set up.

Will Docker slow down my VPS?

The containers themselves add very little overhead, because they share the server’s kernel. What uses resources is the applications inside them. Running three containers costs about what running the same three programs directly would.

Can I run Docker on a managed VPS?

Often yes, but confirm with the provider. Some managed plans restrict software installation or use a virtualization type that does not support containers well. Read Managed VPS vs Unmanaged VPS: What Are You Responsible For? to understand what you can and cannot change.

Do I need to learn Kubernetes?

No. Kubernetes is a system for managing containers across many servers. A single VPS, or a handful of them, runs perfectly well on Docker and Compose.

Start Small and Keep the Data Outside

The path in this guide is the one that works: install Docker from the official source, publish ports on 127.0.0.1, put data on volumes, describe the setup in a Compose file, and put a reverse proxy in front of anything public.

Start with one container you understand. Add the next only when the first is running, backed up, and updated on purpose.

A container is disposable. Your data should not be.