Skip to main content

How to Install Docker: Ubuntu, Debian, Alpine, NixOS

RAFSuNX
6 mins to read

Introduction

Docker has become the industry standard for deploying containerized applications, offering a consistent environment across development, testing, and production. Whether you’re setting up a personal development workstation or provisioning containers in production clusters, installing Docker correctly is the foundation of a secure, efficient container lifecycle.

However, the installation process and configuration nuances vary significantly between distributions. In this guide, we offer an expert-driven walkthrough on how to install Docker across four distinct Linux distributions: Ubuntu, Debian, Alpine, and NixOS. Each system demands a tailored approach, especially when it comes to handling repository configuration, package management, user privileges, and service lifecycle mechanisms.

This post delivers in-depth, distribution-specific instructions to ensure Docker is installed securely and optimally for both development and production use cases.

Installing Docker on Ubuntu

Ubuntu remains one of the most popular Linux distributions for both desktop and server environments, making it a common Docker hosting platform.

Step 1: Uninstall Old Versions

Docker has gone through multiple name changes and legacy packages:

sudo apt-get remove docker docker.io containerd runc

Step 2: Set Up the Repository

Update the package index and install necessary prerequisites:

sudo apt-get update
sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Add Docker’s official GPG key and repository:

sudo mkdir -p /etc/apt/keyrings

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
    sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

echo \
  "deb [arch=$(dpkg --print-architecture) \
  signed-by=/etc/apt/keyrings/docker.gpg] \
  https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Step 3: Install Docker Engine

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

Step 4: Verify Docker Installation

sudo docker run hello-world

This confirms your Docker Engine is running and set up correctly.

Step 5: Manage Docker as a Non-Root User

Add your regular user to the docker group:

sudo usermod -aG docker $USER
newgrp docker

Be cautious: granting Docker group access allows root-level privileges over the host.

Installing Docker on Debian

Debian is structurally similar to Ubuntu but uses a more conservatively curated package base. The Docker installation process mirrors Ubuntu but with slight differences in naming and behavior.

Step 1: Remove Legacy Versions

sudo apt-get remove docker docker-engine docker.io containerd runc

Step 2: Configure the Repository

Install required dependencies:

sudo apt-get update
sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Add Docker’s official key and set up the APT repository:

sudo mkdir -p /etc/apt/keyrings

curl -fsSL https://download.docker.com/linux/debian/gpg | \
    sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
  https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Step 3: Install Docker

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

Step 4: Test Docker Engine

sudo docker run hello-world

Step 5: Enable Non-Root Access

sudo usermod -aG docker $USER
newgrp docker

Ensure you exit and re-login to apply group membership changes.

Installing Docker on Alpine Linux

Alpine’s minimalism and musl-libc design approach require a unique setup pathway. While Docker is included in the Alpine package repository, nuances exist around storage drivers, init systems, and service management.

Step 1: Update and Install Docker

sudo apk update
sudo apk add docker

Step 2: Start and Enable Docker Daemon

sudo rc-update add docker boot
sudo service docker start

Alpine uses OpenRC instead of systemd, so traditional systemctl commands will not work.

Step 3: Add User to Docker Group

addgroup youruser docker

You’ll need to re-login or use su - youruser to activate the change.

Step 4: Run Hello World

docker run hello-world

Be mindful that Alpine’s kernel and filesystem behavior (e.g., device-mapper, overlayfs) can affect certain Docker runtimes; consider these constraints if running Kubernetes (K3s) or containers dependent on full system support.

Installing Docker on NixOS

NixOS introduces a declarative, immutable approach to service management via nix expressions. Docker installation and configuration must conform to the system’s configuration model.

Step 1: Enable Docker via Configuration

Edit /etc/nixos/configuration.nix:

{
  virtualisation.docker.enable = true;
  users.users.youruser.extraGroups = [ "docker" ];
}

Optional Docker-specific tweaks:

{
  virtualisation.docker.daemon.settings = {
    log-driver = "json-file";
    log-level = "info";
    storage-driver = "overlay2";
  };
}

Step 2: Apply Configuration

sudo nixos-rebuild switch

This rebuilds the system and configures Docker as a service.

Step 3: Verify Daemon

systemctl status docker
docker run hello-world

Because of NixOS’ design, changes outside /etc/nixos/configuration.nix are ephemeral. Always use the declarative configuration model to ensure reproducibility.

Step 4: Managing Docker Permissions

Docker’s Unix socket resides at /run/docker.sock and can only be accessed by users in the docker group. Ensure group membership in config, or verify with:

groups youruser

Run su - youruser to reload the group context without rebooting.

Advanced Tips and Best Practices

Common Mistakes

  • Installing via Snap (Ubuntu/Debian): Avoid installing Docker via snap, as it introduces confinement issues that restrict volume mounting and Docker-in-Docker scenarios.
  • Overlooking CGroup Driver Mismatches: Ensure that Docker’s CGroup manager matches your system’s setup (systemd vs cgroupfs) for better compatibility with orchestration tools.
  • Not Adding User to Docker Group: Running Docker as sudo every time is cumbersome and leads to permission issues in scripting and CI tasks.
  • Assuming Persistent Changes on Alpine: Alpine’s default services may not persist across container reboots unless explicitly configured.

Troubleshooting: Common Issues & Solutions

Issue Solution
Docker service not starting Check journal (journalctl -u docker or rc-status) and storage driver
Permission denied /run/docker.sock Ensure user is in docker group; re-login or use newgrp docker
Overlay2 errors (Alpine) Alpine kernels may lack required modules; verify kernel configs
Can’t access Docker socket (NixOS) Confirm user group via declarative config, then rebuild NixOS

Best Practices Checklist

  • Always install from Docker’s official repository
  • Use overlay2 storage driver unless compatibility dictates otherwise
  • Add users to Docker group with caution; be aware of privilege escalation
  • Monitor the Docker service post-install using systemctl or distro-equivalent
  • Consider using docker-compose v2+ (plugin-based) for modern workflows
  • Avoid using latest tags in production workloads

Resources & Next Steps

Conclusion

Installing Docker on Ubuntu, Debian, Alpine, and NixOS reflects the broader diversity in the Linux ecosystem, each requiring tailored techniques to achieve a secure and functional container runtime. By understanding the underlying package systems, service managers, and user permission models, teams can align Docker deployment with OS-specific best practices.

Key Takeaways:

  • Docker installation varies greatly by distribution; use the right method for each system
  • Always install from official Docker repositories for stability and security
  • Use storage drivers and init systems compatible with your distro
  • Don’t forget to securely configure user access with the docker group
  • Verify your setup with docker run hello-world and docker info

With Docker set up the right way, you’re ready to build and run containerized applications on any supported Linux platform.

Happy coding!