How to install
Docker and Docker Compose on
Ubuntu
Install from Docker's own repository, run it without sudo, and fix the usual errors
On this page
Installing Docker from Docker's official repository gives you a current Engine and Compose in one go. These steps work on Ubuntu 22.04 and 24.04 LTS and follow the official documentation (opens in a new tab).
Install#
-
Remove old packages that conflict. If one is not installed, apt says so; carry on.
Terminal for p in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt remove -y $p; done -
Add
Docker's repository
Terminal sudo apt update sudo apt 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 echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list -
Install Engine and Compose
Terminal sudo apt update sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -
Test it.
hello-worldprints "Hello fromDocker!" and the second command prints the Compose version.
Terminal sudo docker run --rm hello-world docker compose version
Use
Docker without sudo#
sudo usermod -aG docker $USER
newgrp docker
docker psRun a web server with
Docker Compose#
services:
web:
image: nginx:alpine
restart: unless-stopped
ports:
- "127.0.0.1:8080:80"docker compose up -d
curl -I http://127.0.0.1:8080The port is bound to 127.0.0.1 because ports that Docker publishes skip your ufw rules. Put Nginx or
Caddy in front as the public entrance with SSL.
Cap the log size#
By default Docker never rotates logs, which can fill the disk. Create this file and restart Docker; it applies to containers created afterwards.
{
"log-driver": "json-file",
"log-opts": { "max-size": "10m", "max-file": "3" }
}sudo systemctl restart dockerIf the file already exists, add these keys to it rather than overwriting it.
Common problems#
| Message | Fix |
|---|---|
permission denied ... docker.sock |
Log out and back in, or run newgrp docker |
port is already allocated |
Find the program holding the port with sudo ss -tulpn |
no space left on device |
Check docker system df, then clear unused data with docker system prune |
| Container keeps restarting | Read docker logs <name> and check memory with free -h |
Frequently asked questions#
What is the difference between docker-compose and docker compose?
docker-compose with a hyphen is the first version, which is no longer maintained. Today you use docker compose, a plugin of the Docker CLI.
How much RAM does a VPS need for
Docker?
Docker itself uses little memory; it depends on the containers you run. Start at 2 GB for one or two services and 4 GB or more if you run a database.
Can I run containers without looking after a server?
Yes. With Container Apps you push an image and get a URL without managing an operating system, and you pay for what you use.
Docker on a Cloud Server
You control the OS and every port, run several services on one machine, and pay by the hour.
Container Apps
No OS to maintain, and instances follow the traffic. See the Container Apps page.