Install Docker Engine on an Ubuntu VPS the Right Way (Official Repository)

There are three common ways to get Docker on Ubuntu: the docker.io package from Ubuntu, the snap, and Docker's own apt repository. This guide uses Docker's official repository, which gives you current releases of Docker Engine, the Compose plugin and Buildx, updated through normal apt upgrade. Installation is the short part. The rest of the guide covers three things that cause real incidents on Docker servers: the docker group, published ports that ignore your firewall, and logs that fill the disk.
Which package, and why
| Source | What you get | Rule of thumb |
|---|---|---|
| Docker's apt repository | Current Docker Engine, Compose v2 plugin, Buildx | Most servers, especially if you follow current Docker documentation |
| Ubuntu's docker.io | Older, Ubuntu-maintained version | Fine if you prefer Ubuntu's update policy and do not need new features |
| Snap | Confined package with its own paths | Avoid on servers; volume paths and daemon config behave differently |
The one firm rule: do not mix them. Two Docker installations on one server cause confusing errors about sockets, containerd versions and missing images.
Step 1: Remove conflicting packages
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do
sudo apt-get remove -y $pkg
done
On a fresh server, apt reports that none of these are installed. That is fine. Existing images and volumes in /var/lib/docker are not deleted by this step.
Step 2: Add Docker's signing key and repository
sudo apt-get update
sudo apt-get install -y 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
The key lets apt verify that packages really come from Docker. It is stored in /etc/apt/keyrings and linked to this one repository only, so it cannot be used to sign packages for other sources.
sudo tee /etc/apt/sources.list.d/docker.sources > /dev/null <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "$VERSION_CODENAME")
Components: stable
Signed-By: /etc/apt/keyrings/docker.asc
EOF
sudo apt-get update
The Suites line reads your Ubuntu codename from the system, noble on 24.04. After apt-get update you should see lines from download.docker.com with no errors.
Step 3: Install Docker Engine
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo docker run --rm hello-world
docker --version
docker compose version
The hello-world container pulls a tiny image, prints a message and exits. If you see "Hello from Docker!", the daemon, the image download and container start all work. Docker is enabled at boot automatically.
Note: unattended-upgrades on Ubuntu only installs updates from Ubuntu's own sources by default. Docker updates from this repository arrive when you run apt upgrade. Plan them, because upgrading the daemon restarts it.
The docker group is root access
Most guides tell you to add your user to the docker group so you can skip sudo:
sudo usermod -aG docker $USER
# log out and back in, then:
docker ps
Understand what this grants. Anyone who can talk to the Docker daemon can start a container that mounts the host's root filesystem:
docker run --rm -v /:/host -it ubuntu chroot /host
# you are now root on the host
So membership in the docker group equals root on the server, with no password and no sudo log entry. Practical rules:
Add only people who already have sudo on this server.
Never add the user a web application or CI job runs as, and never mount
/var/run/docker.sockinto a container you do not fully trust.If you need unprivileged users to run containers, look at Docker's rootless mode. It runs the daemon as a normal user, with some limits on networking and ports below 1024.
Published ports bypass UFW
This catches experienced administrators. You run a container, publish a port and block it with UFW:
docker run -d --name web -p 8080:80 nginx:stable-alpine
sudo ufw deny 8080
# port 8080 is still reachable from the internet
The port stays open. Docker writes its own firewall rules to route traffic to containers, and published ports are handled in the NAT and FORWARD paths, before UFW's INPUT rules ever see the packet. UFW reports the port as denied, and the internet reaches it anyway. Databases, admin panels and dev servers have been exposed exactly this way.
Check what is really listening:
sudo ss -tlnp | grep docker-proxy
# LISTEN 0 4096 0.0.0.0:8080 ... users:(("docker-proxy",...))
0.0.0.0 means every interface, including the public one. The fixes, in order of preference:
Do not publish ports that only other containers need. Containers in the same Compose project share a network and reach each other by service name.
Bind published ports to localhost and put a reverse proxy such as Nginx in front, with HTTPS:
# docker run
docker run -d --name web -p 127.0.0.1:8080:80 nginx:stable-alpine
# compose.yaml
services:
web:
image: nginx:stable-alpine
restart: unless-stopped
ports:
- "127.0.0.1:8080:80"
A typical application with a database then looks like this. Only the app is published, and only on localhost:
services:
app:
image: my-app:latest
environment:
DATABASE_URL: postgres://app:secret@db:5432/app
ports:
- "127.0.0.1:3000:3000"
db:
image: postgres:16
volumes:
- dbdata:/var/lib/postgresql/data
# no "ports:" section: only "app" can reach it
volumes:
dbdata:
If a port must be public but restricted (for example to an office IP), put the rule in the
DOCKER-USERiptables chain. Docker evaluates that chain before its own rules and never overwrites it.
Do not "fix" this by setting "iptables": false in the daemon config. It breaks container networking in ways that are harder to debug than the original problem. Always confirm from a different machine, for example with nc -zv to the server's public IP and port.
Rotate container logs
By default Docker uses the json-file log driver with no size limit. Everything a container prints to stdout and stderr is kept in /var/lib/docker/containers/ forever. A chatty application can fill a disk in weeks, and when the disk is full, databases stop writing and containers fail in odd ways.
Set limits for the whole daemon in /etc/docker/daemon.json:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
},
"live-restore": true
}
sudo nano /etc/docker/daemon.json
sudo dockerd --validate --config-file=/etc/docker/daemon.json
sudo systemctl restart docker
docker compose up -d --force-recreate
max-sizeandmax-filekeep at most 3 files of 10 MB per container. Adjust to your disk and how much history you need.The new settings apply only to containers created after the change. Existing containers keep their old log settings until you recreate them, which is why the last command uses
--force-recreate.live-restorekeeps containers running while the daemon restarts, for example during a Docker upgrade. It is not supported with Swarm mode.Docker also offers a
locallog driver that rotates and compresses by default. Use it unless a log shipper needs to read the JSON files directly.
To see where disk space is going:
docker system df
sudo du -sh /var/lib/docker/containers/*/*-json.log | sort -h | tail
docker image prune -a
docker image prune -a removes every image not used by a container. That is safe on most servers, but the next deploy has to pull images again. Never run docker system prune --volumes unless you are sure: it deletes unused volumes, and a volume is where your database data lives.
A short checklist
Installed from one source only, Docker's repository.
dockergroup limited to people who already have root.No published database ports; app ports bound to 127.0.0.1 behind a reverse proxy.
Exposure checked from outside the server, not from UFW status.
Log rotation in daemon.json, and containers recreated after the change.
Run it on IM HOST
Docker needs a KVM virtual machine with full root, not a container-based plan. IM HOST Linux VPS plans give you full root on KVM, a dedicated IPv4 plus an IPv6 /64 and DDoS protection, from $5/month. If you are deploying AI agents or bots, AI Agent Hosting comes ready with Docker, Node.js, Python and PostgreSQL with pgvector. There are no setup fees, support engineers are available 24/7, and every plan has a 30-day money-back guarantee.
More from our blog
Discover more practical guides and product insights from the IM Host team.
View all articles