Installing and using Docker on a VPS
Docker lets you run apps in containers: packaged bundles that include the app and everything it needs, so you do not have to install and configure each piece by hand. Many self-hosted apps are published as Docker images, and a short Compose file can start a whole app with one command.
This guide installs Docker on Ubuntu or Debian, runs a first app safely, and covers the everyday commands, where your data lives, how to update, and the two security surprises that catch most beginners. It also says when Docker is not worth it, because on a very small plan it can cost more memory than it saves effort.
2 GB and 4 GB VPS plans in stock, comfortable sizes for Docker
- 2 GB KVM VPS Special2 CPU · 2 GB RAM · 35 GB disk · 5 TB bandwidth · New York, Los Angeles, Dallas$35.99/yrabout $3.00/moView at RackNerd
- 4 GB KVM VPS Special3 CPU · 4 GB RAM · 60 GB disk · 7 TB bandwidth · New York, Los Angeles, Dallas$59.99/yrabout $5.00/moView at RackNerd
- Ryzen-2GB (Linux)2 CPU · 2 GB RAM · 20 GB disk · 2 TB bandwidth · New York, Chicago, Seattle$222.37/yrabout $18.53/moView at RackNerd
- KVM-2GB3 CPU · 2 GB RAM · 75 GB disk · 2 TB bandwidth · Los Angeles, Dallas, New York$247.08/yrabout $20.59/moView at RackNerd
In stock when this page loaded. See all 6 matching plans
What is in this guide
- A few words you will see
- When Docker is worth it
- How much memory you need
- Before you start
- 1. Install Docker
- 2. Decide who can run Docker
- 3. Stop logs filling your disk
- 4. Run your first app with Compose
- 5. Everyday commands
- 6. Where your data lives
- 7. Updating apps
- 8. Making an app public
- Security rules for Docker
- Troubleshooting
- Limits
- When to step up
- What next
A few words you will see
| Term | What it means |
|---|---|
| Image | A packaged, read-only copy of an app and everything it needs. You download it from a registry. |
| Container | A running copy of an image. You can stop, remove and recreate it without losing data kept in volumes. |
| Tag | The version label on an image, such as stable or a version number. |
| Registry | Where images are published, such as Docker Hub. |
| Volume | Storage that lives outside the container, so data survives updates. |
| Bind mount | A folder on the server shared with a container. |
| Compose | A tool that starts one or more containers from a single file, usually compose.yaml. |
| Publish a port | Make a port inside the container reachable on the server. |
When Docker is worth it
Docker is a good fit when:
- The app you want is published mainly as a Docker image, which is common for self-hosted apps. See self-hosted apps you can run on a VPS.
- You run several apps and want each one kept separate, with its own versions.
- You want to rebuild or move an app easily, because the Compose file describes it completely.
It is less useful when:
- You run one simple thing that your package manager already provides, such as nginx or WireGuard. Installing it directly is lighter.
- Your plan is very small. Docker and each container add memory overhead. See what you can do with a 512 MB VPS.
How much memory you need
Rough figures. Every app is different, so check its documentation and watch docker stats once it runs.
| Plan | What usually fits |
|---|---|
| 1 GB | Docker plus one or two light containers |
| 2 GB | A few light apps, or one app with a small database |
| 4 GB | Several apps with databases, comfortably |
| 6 GB to 8 GB | Heavier apps, such as a large Nextcloud, a search engine or small AI models |
A small swap file helps when memory briefly runs short. It is a safety net, not extra RAM.
Before you start
- A VPS running Ubuntu or Debian, already secured with SSH keys, updates and a firewall. Your first hour on a new RackNerd VPS walks through it.
- A normal user with sudo. The examples use
alex. - A few GB of free disk space. Images add up.
1. Install Docker
There are two ways to install Docker. Pick one, not both.
- From your system's own packages. The simplest choice, updated with the rest of your system. The version may be a little older.
- From Docker's own repository. Gives the newest version. Follow the install steps for your distribution in Docker's official documentation. They involve adding Docker's package source, so read each step rather than running a script you have not seen.
On Ubuntu, the system packages are:
sudo apt update
sudo apt install docker.io docker-compose-v2
Package names differ between releases, and on Debian the Compose package may not provide the newer docker compose command. If a package is not found, or docker compose version fails after installing, use Docker's own repository instead.
Make sure Docker starts at boot, and test it:
sudo systemctl enable --now docker
sudo docker run --rm hello-world
docker compose version
The test container prints a short welcome message and is removed straight after.
2. Decide who can run Docker
By default you need sudo for every Docker command. Many guides tell you to add your user to the docker group to skip that. Before you do, know that membership of the docker group is the same as root access: anyone in it can take full control of the server.
- On a server only you use, adding yourself is a common convenience:
sudo usermod -aG docker alex, then log out and back in. - Never add other users, web apps or shared accounts to the group.
- If you would rather keep the boundary, just use
sudo docker. The examples below work either way.
3. Stop logs filling your disk
Docker keeps every line a container prints, and by default it never deletes them. On a small disk, a chatty container can fill it. Set a size limit once, for all containers. Create or edit Docker's settings file:
sudo nano /etc/docker/daemon.json
Put this in it:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
That keeps up to three files of 10 MB for each container. Restart Docker with sudo systemctl restart docker. The limit applies to containers created after the change, so set it before you start your apps.
4. Run your first app with Compose
This example serves a tiny web page with nginx, to show how Compose, ports and folders fit together. Make a folder for the app:
mkdir -p ~/apps/web/site
cd ~/apps/web
echo "Hello from Docker" > site/index.html
nano compose.yaml
Put this in compose.yaml:
services:
web:
image: nginx:stable
restart: unless-stopped
ports:
- "127.0.0.1:8080:80"
volumes:
- ./site:/usr/share/nginx/html:ro
What each part means:
imageis the app and its version tag.restart: unless-stoppedbrings the container back after a crash or reboot.portspublishes the container's port 80 as port 8080 on the server, but only on 127.0.0.1, so the internet cannot reach it directly. Section security rules explains why this matters.volumesshares yoursitefolder with the container, read-only.
Start it and check it:
docker compose up -d
docker compose ps
curl -I 127.0.0.1:8080
The last command should show HTTP/1.1 200 OK. To see the page in your own browser, open an SSH tunnel from your computer:
ssh -L 8080:127.0.0.1:8080 alex@SERVER_IP
While that session is open, visit port 8080 on localhost in your browser. When you are done experimenting, stop and remove the app with docker compose down.
Most self-hosted apps work the same way: the project's documentation gives you a Compose file. Read it before starting it, and change any ports lines to begin with 127.0.0.1: unless the port truly needs to be public.
5. Everyday commands
Run these from the folder with the app's compose.yaml.
| Command | What it does |
|---|---|
docker compose up -d | Start, or apply changes to, the app in the background |
docker compose ps | Show the app's containers and their state |
docker compose logs -f | Follow the app's logs. Press Ctrl+C to stop |
docker compose restart | Restart the app |
docker compose down | Stop and remove the containers. Named volumes and bind-mounted folders are kept |
docker compose exec web sh | Open a shell inside a container, here the one called web |
docker ps -a | List every container on the server |
docker stats | Live memory and CPU use for each container |
docker system df | How much disk images, containers and volumes use |
4 GB VPS plans in stock, for several apps with databases
- 4 GB KVM VPS Special3 CPU · 4 GB RAM · 60 GB disk · 7 TB bandwidth · New York, Los Angeles, Dallas$59.99/yrabout $5.00/moView at RackNerd
- Ryzen-4GB (Linux)2 CPU · 4 GB RAM · 30 GB disk · 3 TB bandwidth · New York, Chicago, Seattle$265.57/yrabout $22.13/moView at RackNerd
- KVM-4GB4 CPU · 4 GB RAM · 130 GB disk · 3 TB bandwidth · Los Angeles, Dallas, New York$295.08/yrabout $24.59/moView at RackNerd
In stock when this page loaded. See every plan
6. Where your data lives
Containers are meant to be thrown away and recreated. Anything you want to keep must live outside them, in one of two places:
- Bind mounts, like
./sitein the example: folders next to your Compose file. Easy to see, copy and back up. - Named volumes, which Docker manages itself, usually under
/var/lib/docker/volumes. Common in app Compose files.
A simple habit makes backups easy: keep each app in its own folder under ~/apps, with its Compose file, its .env file and, where you can, its data as bind-mounted subfolders. Then backing up the app means backing up the folder.
Databases need care. Copying a running database's files can give a broken backup. Either stop the app first with docker compose stop, or use the database's own export tool through docker compose exec, as the app's documentation explains. Using a storage VPS for backups shows how to send backups off the server.
7. Updating apps
cd ~/apps/web
docker compose pull
docker compose up -d
docker image prune
That downloads newer images, recreates the containers that changed, and removes old images to free space. A few habits keep updates safe:
- Read the release notes before big updates. Some apps need steps between major versions.
- Back up first, especially the app's database.
- Choose your tags. A tag like
latestalways moves to the newest version, which can bring surprises. A specific version tag only changes when you edit it, which is safer for important apps. - Update regularly. Images contain their own copies of system libraries, and old images miss security fixes. Your system updates do not cover them.
8. Making an app public
To put an app on the web with a domain and HTTPS, the usual pattern is a reverse proxy: one web server, such as Caddy or nginx, listens on ports 80 and 443, handles certificates and passes visits to each app on its 127.0.0.1 port. This keeps the apps themselves private and puts one well-maintained program in front of the internet.
- Point a domain or subdomain at your server. See domains and DNS explained.
- Install a reverse proxy, either from your package manager or as its own container. Web servers on a VPS compares the options.
- Configure it to send the domain to
127.0.0.1:8080, or whichever port the app uses. - Open only ports 80 and 443 in the firewall:
sudo ufw allow 80,443/tcp. - Make sure the app has a login, and turn off open sign-up unless you want public users.
For apps only you use, consider not making them public at all. Reach them through an SSH tunnel or a private VPN instead. See setting up a WireGuard VPN.
Security rules for Docker
- Published ports can bypass UFW. Docker edits the server's firewall rules directly. A port published as
8080:80can be open to the whole internet even whenufw statusdoes not list it. Publish on127.0.0.1unless a port must be public, and check withsudo ss -tulpn. - The docker group is root. See step 2.
- Use trusted images. Prefer official images and those published by the app's own project. Unknown images can contain anything.
- Do not share the Docker socket. Some tools ask you to mount
/var/run/docker.sockinto a container. That gives the container control of the whole server. Only do it for tools you trust completely. - Never expose the Docker API over the network.
- Keep secrets in .env files next to the Compose file, readable only by you with
chmod 600 .env, and keep them out of Git. - Update images. See step 7.
The wider basics, from SSH to backups, are in a beginner's guide to securing your VPS.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| "permission denied" on the Docker socket | Your user is not in the docker group | Use sudo docker, or add yourself as in step 2 and log in again |
docker compose not found | The Compose plugin is not installed | Install it, or use Docker's own repository as in step 1 |
| "port is already allocated" | Another container or program uses that port | Change the first number in the ports line, or stop the other program |
| Container keeps restarting | The app is crashing at start-up | Run docker compose logs and read the last lines |
| Container stops with code 137 | Out of memory | Check docker stats and free -h. Run fewer apps or step up a size |
| App reachable from the internet unexpectedly | Port published on all addresses | Add 127.0.0.1: to the ports line and run docker compose up -d |
| Disk filling up | Old images, logs or build cache | Check docker system df, run docker image prune, and set the log limit in step 3 |
| Data gone after an update | Data was stored inside the container, not a volume | Restore from backup, and add a volume for the app's data folder |
Limits
- Docker adds overhead. On very small plans, installing apps directly is often better.
- Containers are not a security wall. They separate apps, but a flaw in a container or a careless setting can still affect the server.
- Images age. An image you never update is software you never patch.
- Licences apply. Check the licence of each app and image you run, especially for business use.
- Fair use applies. Unmetered does not mean unlimited. Apps that download or stream a lot, or keep the CPU busy all day, can hit fair-use limits. RackNerd's acceptable use policy covers what your containers do.
When to step up
Watch docker stats and free -h after adding each app. If swap is in use most of the time, or containers are killed for using too much memory, it is time for a bigger plan. Databases and search tools are usually the hungriest parts.
6 GB and 8 GB VPS plans in stock, for heavier Docker setups
- 6 GB KVM VPS Special6 CPU · 6 GB RAM · 100 GB disk · 12 TB bandwidth · New York, Los Angeles, Dallas$89.99/yrabout $7.50/moView at RackNerd
- 8 GB KVM VPS Special7 CPU · 8 GB RAM · 150 GB disk · 20 TB bandwidth · New York, Los Angeles, Dallas$119.99/yrabout $10.00/moView at RackNerd
- KVM-6GB5 CPU · 6 GB RAM · 170 GB disk · 4 TB bandwidth · Los Angeles, Dallas, New York$268.59/yrabout $22.38/moView at RackNerd
- Ryzen-6GB (Linux)3 CPU · 6 GB RAM · 45 GB disk · 4 TB bandwidth · New York, Chicago, Seattle$297.97/yrabout $24.83/moView at RackNerd
In stock when this page loaded. See all 6 matching plans
What next
- Pick some apps to run: self-hosted apps you can run on a VPS.
- Use Docker for a staging server: a VPS as a dev and staging server.
- Try a self-hosted AI app: your first self-hosted AI in 15 minutes.
To compare what is available now, start with the cheapest RackNerd VPS guide and check the sales page.