basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    Docker on Ubuntu: from Docker’s repository, with a log limit

    Install Docker Engine and the Compose plugin from Docker’s repository — not docker.io from Ubuntu and not the snap — and cap container logs right away: with no limit, json-file grows until the disk is full. The ufw firewall doesn’t close ports that Docker publishes — that’s covered here too.

    Where to install from

    Source What you get When to use
    Docker’s repository, docker-ce a current Engine and Compose v2 from one place, plus Buildx; updates on release day almost always — this manual covers it
    docker.io from Ubuntu the version as of the Ubuntu release, security fixes only; unattended-upgrades updates it — restarting containers overnight policy requires Docker from the main archive
    the docker snap sandboxed: doesn’t see every directory, has its own data paths not for a server
    the get.docker.com script the same as Docker’s repository, in one command with no visible steps throwaway machines; on your own server go step by step to see what gets added

    Setup

    1. Remove conflicting packages

      sudo apt remove $(dpkg --get-selections docker.io docker-compose docker-compose-v2 docker-doc docker-buildx podman-docker containerd runc | cut -f1)
      snap list docker 2>/dev/null && sudo snap remove docker

      dpkg --get-selections keeps only what’s installed: on a fresh server the list is empty and there’s nothing to remove. If Docker was there, images and volumes in /var/lib/docker/ survive remove.

    2. Add Docker’s 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
      
      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 "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
      Components: stable
      Architectures: $(dpkg --print-architecture)
      Signed-By: /etc/apt/keyrings/docker.asc
      EOF
      
      sudo apt-get update

      The source is in deb822 format (.sources), like Ubuntu’s own ubuntu.sources since 24.04. Signed-By means apt uses this key to check Docker’s packages only, not everything. Suites comes from /etc/os-release: noble on 24.04, resolute on 26.04.

      The commands were checked against the install page on docs.docker.com on 2026-10-18; its list of supported releases includes 26.04 and 24.04.

    3. Install Engine and Compose

      sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
      sudo docker version --format '{{.Server.Version}}'
      # 29.4.1
      docker compose version
      # Docker Compose version v5.6.0

      The service starts and enables itself at boot: systemctl is-enabled docker says enabled.

    4. Cap container logs

      sudo tee /etc/docker/daemon.json >/dev/null <<'EOF'
      {
        "log-driver": "json-file",
        "log-opts": {
          "max-size": "10m",
          "max-file": "3"
        }
      }
      EOF
      sudo systemctl restart docker

      Each container keeps at most three 10 MB files — 30 MB per container. Keep json-file as the driver rather than switching to local: vector and other log shippers read the *-json.log files (Traefik logs).

      The limit applies to containers created after the change. If stacks are already running, run docker compose up -d --force-recreate in each.

      systemctl restart docker restarts every container. On a server with live sites, do it before the sites go up or at a quiet time.

    5. Decide who gets docker without sudo

      The docker group grants access to docker.sock, and through it root on the host: docker run -v /:/host opens the whole file system (docker.sock). A user in the docker group is root without a password.

      How What you get
      sudo docker … the sudo password stands between a stolen SSH key and root
      sudo usermod -aG docker example-user shorter commands; an SSH key gives root right away
      rootless Docker the daemon runs as a regular user, no root needed; ports below 1024 need extra setup, and some network modes don’t work

      On a server where one person logs in with a passphrase-protected key, people often choose the group — the call is yours, but make it knowing the cost.

    6. Check that it all works

      sudo docker run --rm hello-world | head -2
      #
      # Hello from Docker!

    What’s next on the server

    If it doesn’t work

    permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock

    Cause
    The user isn’t in the docker group, or was added but the session is old.
    Fix
    sudo docker …, or usermod -aG docker example-user and a fresh login — but first read what that group grants (step 5).

    docker-ce : Conflicts: docker.io during install

    Cause
    Docker from Ubuntu’s repository is already on the server.
    Fix
    Step 1: remove docker.io and its neighbours. Images and volumes in /var/lib/docker survive remove.

    Docker from snap: docker compose can’t see files in /opt or /srv

    Cause
    Snaps run sandboxed: Docker from snap sees files only in the home directory, and external disks only after snap connect docker:removable-media. The Ubuntu Server installer offers it in its list of snaps.
    Fix
    sudo snap remove docker and install from Docker’s repository. Snap data lives in /var/snap/docker/ — move the volumes before removing it.

    /var/lib/docker/containers/*/*-json.log files filled the disk

    Cause
    The json-file driver has no limit by default. A limit in daemon.json applies only to containers created after it was set.
    Fix
    Step 4, then recreate the containers: docker compose up -d --force-recreate in every stack.

    docker: 'compose' is not a docker command

    Cause
    The docker-compose-plugin isn’t installed; the old docker-compose (v1, with a hyphen) hasn’t been updated in years.
    Fix
    sudo apt install docker-compose-plugin.

    How to check

    Docker comes from the right repository:

    apt-cache policy docker-ce | sed -n '1,4p'
    # docker-ce:
    #   Installed: 5:29.4.1-1~ubuntu.24.04~noble
    #   Candidate: 5:29.4.1-1~ubuntu.24.04~noble
    #   Version table:

    The log limit applies to a new container:

    sudo docker run -d --name logtest alpine sh -c 'while true; do date; sleep 1; done'
    sudo docker inspect logtest --format '{{.HostConfig.LogConfig}}'
    # {json-file map[max-file:3 max-size:10m]}
    sudo docker rm -f logtest

    Ports open to the outside — check each one from a phone:

    sudo docker ps --format '{{.Names}}\t{{.Ports}}' | grep '0.0.0.0'