Beginner

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
  1. Install
  2. Use Docker without sudo
  3. Run a web server with Docker Compose
  4. Cap the log size
  5. Common problems
  6. Frequently asked questions

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#

  1. 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
  2. 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
  3. 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
  4. Test it. hello-world prints "Hello from Docker!" and the second command prints the Compose version.

    Terminal
    sudo docker run --rm hello-world
    docker compose version

Use Docker without sudo#

Terminal
sudo usermod -aG docker $USER
newgrp docker
docker ps

Run a web server with Docker Compose#

compose.yaml
services:
  web:
    image: nginx:alpine
    restart: unless-stopped
    ports:
      - "127.0.0.1:8080:80"
Terminal
docker compose up -d
curl -I http://127.0.0.1:8080

The 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.

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

If 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.

Follow the guide on your own server

Create a Cloud Server with Ubuntu, Debian or Windows Server, billed by the hour in Thai baht. Support answers 24/7 in Thai and English.