Docker has revolutionized how developers and system administrators build, ship, and run applications. By containerizing applications, you can create consistent and reproducible environments across development, testing, and production.

If you're setting up a fresh Linux server, such as Ubuntu, the following guide walks through the essential steps to install Docker Engine and configure it for everyday use.

Step 1: Prepare Your System and Add the Official Repository

Before installing Docker, it is good practice to remove older or conflicting Docker packages and make sure your package manager can securely retrieve software over HTTPS.

Remove Older Docker Packages

If older Docker packages are installed on the system, remove them to avoid package conflicts:

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

Removing these packages does not automatically remove Docker data stored under /var/lib/docker/.

Install Required Prerequisites

Update the package index and install the packages required to securely access Docker's repository:

Bash
sudo apt-get update
sudo apt-get install ca-certificates curl

Add Docker's Official GPG Key

Create the directory used to store repository signing keys:

Bash
sudo install -m 0755 -d /etc/apt/keyrings

Download Docker's official GPG key:

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

Make the key readable by the package manager:

Bash
sudo chmod a+r /etc/apt/keyrings/docker.asc

Add the Docker Repository

Add Docker's stable repository to the APT sources:

Bash
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 > /dev/null

Update the package index again:

Bash
sudo apt-get update

At this point, APT can retrieve Docker packages from Docker's official repository.

Step 2: Install Docker Engine

With the repository configured, install Docker Engine along with the Docker CLI, container runtime, Buildx plugin, and Docker Compose plugin:

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

Once the installation completes, verify that Docker is working correctly by running the hello-world container:

Bash
sudo docker run hello-world

If the installation is successful, Docker downloads the test image, creates a container from it, runs the container, and displays a confirmation message.

You can also verify that the Docker service is running with:

Bash
sudo systemctl status docker

On systems where Docker is not started automatically, you can start it with:

Bash
sudo systemctl start docker

To configure Docker to start automatically when the system boots:

Bash
sudo systemctl enable docker

Step 3: Manage Docker as a Non-Root User

By default, Docker commands require sudo because the Docker daemon is accessed through a Unix socket that is normally restricted to root and members of the docker group.

To run Docker commands without sudo, add your current user to the docker group:

Bash
sudo usermod -aG docker $USER

The group membership normally takes effect after you log out and log back in.

If you want to apply the new group membership immediately in the current terminal session, run:

Bash
newgrp docker

You can then verify that Docker commands work without sudo:

Bash
docker ps

The command displays the currently running containers. If no containers are running, the result may simply show an empty container list.

Important Security Consideration

Membership in the docker group provides highly privileged access to the Docker daemon. In practice, users who can control the Docker daemon can often gain root-level control over the host system.

Therefore, add users to the docker group only when that level of access is appropriate.

Conclusion

Docker is now installed and configured on your Ubuntu server. You have:

  • Removed potentially conflicting Docker packages.

  • Added Docker's official APT repository.

  • Installed Docker Engine and its supporting components.

  • Verified the installation using the hello-world container.

  • Configured Docker to run as a system service.

  • Enabled non-root Docker commands for your user.

You can verify your setup at any time with:

Bash
docker ps

From here, you can use Docker to run containers, deploy applications, host services, and build reproducible containerized environments on your Linux server.