Chapter 2 · Lesson 1 of 20
Docker

Install Docker on Ubuntu

Docker Engine is installed on Ubuntu by adding Docker's official apt repository and installing the docker-ce packages.

By the end you will

Install Docker Engine from Docker's apt repository and run the hello-world test.

Time
22 min
Level
Beginner
Check
4 questions
You need
  • An Ubuntu server you can reach over SSH
  • A user with sudo rights

To install Docker on Ubuntu, add Docker’s apt repository and install the docker-ce packages. A fresh Ubuntu server cannot run containers until Docker Engine is installed. The install is not one command, because Ubuntu’s own docker.io package can lag behind, so Docker publishes its own apt repository. You add that repository once, and after that apt installs and updates Docker like any other package.

How it works#

Docker Engine is a background service (dockerd) with a command line client (docker) that talks to it. The two communicate through a Unix socket at /var/run/docker.sock. Whoever can write to that socket can tell the daemon to do anything, including mounting the host’s filesystem into a container. That is why access to the socket is effectively root access.

The install has three parts:

  • A signing key. You download Docker’s public key so apt can verify that packages really come from Docker.
  • A repository entry. A line in /etc/apt/sources.list.d/docker.list tells apt where to find packages. It uses your CPU architecture and your Ubuntu release codename, such as the name printed by lsb_release -cs.
  • The packages. docker-ce is the engine, docker-ce-cli the client, containerd.io the runtime underneath, and the buildx and compose plugins add docker buildx and docker compose.

After installing, the service is enabled and starts at boot. Only root and members of the docker group can use the socket.

These steps follow Docker’s official guide for Ubuntu. Check the source link at the bottom if your Ubuntu version is new.

Step 1: Remove old packages#

bash
sudo apt remove docker.io docker-doc docker-compose podman-docker containerd runc

What you should see: apt lists packages to remove, or says they are not installed. If none are installed, it continues without error. This removes packages only. Images, containers and volumes under /var/lib/docker are kept.

Step 2: Add Docker’s repository#

bash
sudo apt update
sudo apt install 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 "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update

What the key parts do: curl -fsSL downloads quietly, follows redirects and fails on HTTP errors. The echo line builds the repository entry from your architecture and release name and writes it with tee. What you should see: the final apt update lists lines for download.docker.com with no errors.

Step 3: Install Docker Engine#

bash
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

What you should see: apt unpacks the packages and starts the service. Confirm with sudo systemctl status docker, which should say active (running). Press q to leave the status view.

Step 4: Run the test container#

bash
sudo docker run hello-world

What you should see: Docker says it cannot find the image locally, pulls hello-world, and prints a message that starts with “Hello from Docker!”. That means the engine pulled an image and ran a container.

Step 5: Run Docker without sudo (optional)#

Members of the group have root-level power (see Linux file permissions explained for how groups work).

bash
sudo usermod -aG docker $USER

-aG appends your user to the docker group without removing other groups. Group changes apply to new login sessions, so log out and back in, then run docker run hello-world without sudo. For a quick test in the current terminal, newgrp docker starts a shell with the group active.

Check it worked#

bash
docker --version
docker compose version
docker run --rm hello-world

You should see a version line starting with Docker version, a line starting with Docker Compose version, and the “Hello from Docker!” message. The --rm flag removes the finished test container so it does not pile up. If the last command fails with a permission error, use sudo or finish step 5 and log in again.

Edge cases and gotchas#

  • Docker bypasses ufw for published ports. Docker writes its own iptables rules, so a container started with -p 8080:80 can be reachable from the internet even if ufw has no rule for 8080. Bind to 127.0.0.1 (for example -p 127.0.0.1:8080:80) when a port should stay private.
  • The docker group is root in disguise. Anyone in it can start a container that mounts / and change any file. Add only people you would give root to.
  • New Ubuntu releases can lag. The repository line uses your release codename. If Docker has not published packages for it yet, apt update shows a 404 for that repository. Use an LTS release, or follow Docker’s current instructions for your version.
  • ARM servers need ARM images. The packages install fine on arm64, but an image built only for amd64 fails with exec format error. Look for multi-architecture images.
  • Snap or other installs can conflict. If Docker came from a snap or another source, remove it first. Two daemons fight over the socket and the data directory.
  • Docker needs disk space. Images and layers live under /var/lib/docker. A small root disk can fill up, so check df -h and see Free disk space with Docker prune safely.
  • Group changes need a new login. After usermod, existing terminals keep the old groups, so a permission error right after step 5 is expected.

Troubleshooting#

SymptomLikely causeFix
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?The service is not runningsudo systemctl start docker, then check systemctl status docker
permission denied while trying to connect to the Docker daemon socketYour user is not in the docker group, or you did not log in againFinish step 5, log out and in, or use sudo
E: Unable to locate package docker-ceThe repository was not added, or apt update was skippedRedo step 2 and run sudo apt update
404 Not Found for download.docker.comDocker has no packages for your release codename yetUse a supported Ubuntu LTS, or check Docker’s install guide
NO_PUBKEY or a signature warningThe key file is missing or unreadableRedo the curl and chmod a+r lines in step 2
exec format error when running an imageThe image does not match your CPU architectureChoose an image published for your architecture
Got permission denied after adding the groupThe shell has the old group listLog out and back in, or run newgrp docker
docker: command not found after installThe shell has not picked up the path, or the install failedOpen a new shell and run which docker
docker compose says unknown commandThe compose plugin is missingInstall docker-compose-plugin from step 3
apt reports a conflict with containerdAn older containerd package is installedRemove it as in step 1, then install again

Real-world scenario#

You rent a new Ubuntu server and want to run a web app in a container. You log in, run the removal command from step 1 (nothing is installed), add the repository and install the five packages.

sudo docker run hello-world prints the greeting. You add your user to the docker group, log out and in, and docker run --rm hello-world now works without sudo.

You then start an app with -p 8080:80 and find it open on the public address even though ufw has no rule for 8080. That is Docker writing its own iptables rules. From then on you bind private ports to 127.0.0.1.

Quick reference#

TaskCommand
Remove old packagessudo apt remove docker.io docker-doc docker-compose podman-docker containerd runc
Installsudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Testsudo docker run hello-world
No sudosudo usermod -aG docker $USER
Statussudo systemctl status docker
Versionsdocker --version and docker compose version

Security and safety notes#

  • Treat the docker group like root access.
  • Published container ports can bypass ufw, so check what is exposed with sudo ss -tlnp.

Next#

Start something real with Run your first container.

Try it: Install Docker on Ubuntu

Refresh apt, install the packages and run the hello-world test.

practice, nothing runs on your machine

Want a real machine? Free browser labs: Killercoda Play with Docker

Check what you learned4 questions
1Why does the lesson add Docker's own apt repository instead of installing docker.io?
2What does a message starting with Hello from Docker! after sudo docker run hello-world prove?
3After sudo usermod -aG docker $USER, docker still gives permission denied on docker.sock. What now?
4apt update fails with a 404 for Docker's repository on a brand-new Ubuntu release. What is the likely cause?

Frequently asked questions

Should I install Docker with apt install docker.io?

That package is maintained by Ubuntu and can lag behind. Docker's own repository gives current releases, so the official guide recommends it.

Is it safe to add my user to the docker group?

Members of the docker group have root-level power on the machine. Do it only for users you trust, or use sudo instead.

Does Docker work on a server with an ARM processor?

Yes. The repository line uses your architecture (amd64, arm64 and others are published). The catch is that an image must also exist for that architecture, otherwise it fails with an exec format error.

Will removing the old packages delete my containers and images?

No. The remove command in step 1 removes packages, not the data under /var/lib/docker, so existing images and volumes stay in place.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios