# Installing Docker: Desktop, Engine on Linux, and rootless

Install Docker the right way for your machine: Docker Desktop on macOS and Windows, Docker Engine from Docker's repository on a Linux server, and rootless mode when the docker group is too much power.

Course: [Docker](https://devops.rajeev.pro/courses/docker), lesson 3. About 25 minutes, beginner. Source: https://devops.rajeev.pro/courses/docker/03-installation

## What you will learn

- Explain the docker CLI, the daemon, containerd and runc, and how they talk
- Choose between Docker Desktop and Docker Engine, and know Desktop's licence terms
- Install Docker Engine on Ubuntu from Docker's repository, with Compose and Buildx
- Run docker without sudo, and understand why the docker group equals root
- Set up rootless mode, and verify any install with hello-world, docker version and docker info

## What you're installing

Before Docker was installed through a standard package or desktop app, developers pieced together a CLI, a
background service and a container runtime by hand. The commands could look correct while the daemon was
missing, so `docker run` failed before the container even started.

"Docker" is several programs. The `docker` command you type is a client; it sends requests to a background
service called the **daemon**. The daemon manages images and containers, then delegates process setup to
lower-level runtime tools. Desktop runs that service inside a Linux VM on macOS and Windows; on a Linux host,
it can run directly on the host kernel.

This lesson installs the right pieces for your operating system, checks that the client can reach the daemon,
and explains what changes when you allow a regular user to control Docker.

> **Figure.** Left, Docker Engine on Linux: the docker command talks over /var/run/docker.sock to dockerd, which asks containerd to run containers; containerd starts runc, which creates the namespaces and cgroups, and the containers run directly on the host kernel. Access to the socket, through the docker group, is equivalent to root. Right, Docker Desktop on macOS or Windows: the docker command runs on the laptop, but dockerd, containerd and the containers run inside a small Linux VM, WSL 2 on Windows, because containers need a Linux kernel.
>
> The CLI is a remote control. dockerd, containerd and runc do the work, on the host's Linux kernel or in Desktop's VM.

`docker version` shows both halves. If the **Server** part is missing, the CLI can't reach the daemon:

```sh
$ docker version
Client:
 Version:           29.1.3
 API version:       1.52
 Context:           default

Server:
 Engine:
  Version:          29.1.3
  API version:      1.52 (minimum version 1.44)
 containerd:
  Version:          2.2.2
 runc:
  Version:          1.4.0-0ubuntu1
```

## Docker Desktop: macOS and Windows

On a laptop, install **Docker Desktop** from docker.com. It brings the CLI, Compose, Buildx, a small Linux
VM to run containers in (WSL 2 on Windows, which you enable first), and a GUI to see containers, images and
logs. Files, ports and DNS are forwarded between your laptop and the VM, so `localhost:8080` works as you'd
expect.

Two things to know:

- **Licensing:** Docker Desktop is free for personal use, education, non-commercial open source and small
  businesses (fewer than 250 employees **and** under $10 million in yearly revenue). Larger companies need
  a paid subscription. Docker Engine on Linux has no such licence.
- **Architecture:** on Apple silicon Macs, images are `arm64` by default. An image built only for `amd64`
  runs slowly under emulation, and a laptop build may not match an `amd64` server. Build with
  `--platform linux/amd64` when the target is a typical cloud server.

## Docker Engine on a Linux server

On servers, install **Docker Engine** from Docker's own repository (the Linux course's _Packages_ lesson
explains repositories and signing keys):

```sh
$ sudo install -m 0755 -d /etc/apt/keyrings
$ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /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-get update
$ sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
$ sudo systemctl enable --now docker
```

Ubuntu's own `docker.io` package is an alternative (`sudo apt-get install docker.io docker-buildx
docker-compose-v2`): simpler, sometimes a version or two behind. Note that Compose is a **separate package**
either way; without it, `docker compose` answers `unknown command`.

Check the install:

```sh
$ docker run --rm hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
$ docker info | grep -E "Storage Driver|Cgroup Version|Docker Root Dir"
 Storage Driver: overlayfs
 Cgroup Version: 2
 Docker Root Dir: /var/lib/docker
```

Those four steps are the diagram above, in words.

### Running docker without sudo

```sh
$ ls -l /var/run/docker.sock
srw-rw---- 1 root docker 0 Oct 14 09:03 /var/run/docker.sock
$ sudo usermod -aG docker "$USER"      # then log out and in again
$ docker ps                            # works without sudo in a new login
```

Until you log in again, your shell keeps its old groups and gets `permission denied while trying to connect
to the docker API at unix:///var/run/docker.sock`.

> **The docker group is root.** Anyone who can talk to the Docker socket can run `docker run -v /:/host alpine` and change any file on the host. Add only people you'd give `sudo`, never service accounts that don't need it, and never expose the socket over the network without TLS.

## Rootless mode

The Docker daemon normally runs with root privileges, so control of it can become control of the host. **Rootless
mode** runs the daemon and containers as an unprivileged user. User namespaces map container IDs to IDs owned
by that user on the host; container root therefore does not become host root through that mapping.

Install the required user-namespace tools, run the setup command as your normal user, and switch the Docker
context to the rootless daemon. Each user then has a separate daemon and image store. Some networking and
storage features differ from rootful Docker, and low ports may need extra configuration.

```sh
$ sudo apt-get install -y uidmap docker-ce-rootless-extras
$ dockerd-rootless-setuptool.sh install           # as your normal user, not with sudo
$ docker context use rootless
$ docker info | grep -i rootless
```

Trade-offs: ports below 1024 need an extra setting, some storage and network features are slower, and each
user has their own images and containers. **Podman** runs rootless by default and accepts the same commands,
which makes it a common choice on shared hosts.

Use rootless mode on a shared development host when its feature limits fit the workload. On a dedicated
production host, use a carefully managed daemon account and restrict socket access; rootless is not a reason
to skip patching or container limits. A common mistake is running the setup utility with `sudo`, which creates
or configures a rootful daemon instead of the user's rootless one.

> **In production.** Pin the Docker version you run in production, update it on purpose like any package, and enable the service so containers with a restart policy come back after a reboot. On shared or sensitive hosts, prefer rootless mode or Podman over adding users to the docker group.

## Mission: Install Docker and prove every part works

On an Ubuntu VM, install Docker Engine from Docker's repository with the Compose and Buildx plugins, let your user run docker without sudo, and prove the client, daemon, containerd, storage driver, cgroups and image pulls all work.

- You need: An Ubuntu 24.04 or newer VM with sudo
- You'll have: hello-world output, docker version with both halves, and docker compose version

```sh
$ sudo install -m 0755 -d /etc/apt/keyrings
$ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /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-get update && sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
$ sudo usermod -aG docker "$USER" && exit                 # log in again
$ docker run --rm hello-world | head -2
Hello from Docker!
This message shows that your installation appears to be working correctly.
$ docker version --format 'client {{.Client.Version}} · server {{.Server.Version}}'
$ docker info --format 'storage={{.Driver}} cgroup={{.CgroupVersion}}'
storage=overlayfs cgroup=2
$ docker compose version && docker buildx version
```

If `docker version` prints only the client, run `systemctl status docker`; if it says `permission denied`,
your new group hasn't applied yet: log out and in again.

## Try it

- Run `docker version` and find the client version, the server version and the containerd version.
- Run `docker info` and find the storage driver, the cgroup version and the Docker root directory.
- Run `docker run --rm hello-world` and match each numbered step it prints to the diagram in this lesson.
- Run `docker context ls` and find which socket your CLI talks to.
- Check `docker compose version` and `docker buildx version`, and install the missing plugin if either fails.
- Read `ls -l /var/run/docker.sock` and explain who can use it.

## Quiz

1. Why does Docker Desktop on macOS run a Linux virtual machine?
   - For speed
   - **Containers use Linux kernel features (namespaces, cgroups), so they need a Linux kernel** (answer)
   - For licensing
   - It doesn't
   Why: The docker CLI runs on the Mac; dockerd and the containers run in a small Linux VM. On Windows, that VM is WSL 2.

2. Why is adding a user to the docker group a security decision?
   - It isn't
   - **Access to the Docker socket lets you start a container that mounts the host's / with root rights: it is effectively root** (answer)
   - It slows Docker down
   - It shares their files
   Why: docker run -v /:/host ... gives full control of the host. Grant it like sudo, or use rootless mode.

3. On an Ubuntu server, which install gives the newest Docker Engine plus the Compose and Buildx plugins?
   - Docker Desktop
   - **Docker's own apt repository: docker-ce, docker-ce-cli, containerd.io, docker-buildx-plugin, docker-compose-plugin** (answer)
   - snap install docker
   - pip install docker
   Why: Ubuntu's own docker.io package works too but can lag behind, and Compose comes as a separate package (docker-compose-v2).

4. What does rootless mode change?
   - Containers cannot run as root inside
   - **The daemon and containers run as your normal user, so escaping a container gives an attacker your rights, not root** (answer)
   - Docker no longer needs a kernel
   - Images become smaller
   Why: Inside the container you can still be root, mapped to your unprivileged user outside by user namespaces. Some features, like binding ports below 1024, need extra setup.

5. docker version shows a Client but an error for the Server. What is wrong?
   - The image is missing
   - **The CLI can't reach the daemon: it isn't running, or your user can't access the socket** (answer)
   - The internet is down
   - Docker is too old
   Why: Check systemctl status docker and the socket permissions. "permission denied ... docker.sock" means the group membership hasn't applied to your shell yet.

## Resources

- [Docker Docs: install Docker Engine on Ubuntu](https://docs.docker.com/engine/install/ubuntu/) (Docker Docs)
- [Docker Docs: Linux post-installation steps](https://docs.docker.com/engine/install/linux-postinstall/) (Docker Docs)
- [Docker Docs: rootless mode](https://docs.docker.com/engine/security/rootless/) (Docker Docs)
- [Docker Docs: Docker Desktop](https://docs.docker.com/desktop/) (Docker Docs)
- [Docker Docs: protect the Docker daemon socket](https://docs.docker.com/engine/security/protect-access/) (Docker Docs)
