← ALL POSTS
CLOUD SETUP & MIGRATION STEP-BY-STEP GUIDE

Install Docker on Ubuntu and run your first container

Docker packages an application and everything it needs into a container, a sealed box that runs the same on your laptop, your office server, and a cloud VM. It is how a lot of modern software expects to be run, and installing it on Ubuntu is a fifteen-minute job when you do it right. This guide installs Docker from Docker's own package repository, sets up your user so you can run it without sudo, and finishes with a real service: an nginx web server answering on a port you chose.

Prerequisites

One note before we start: Ubuntu's default repositories carry a package called docker.io, and there is also a snap version. Both tend to lag behind. Docker's own repository is what Docker documents and supports, it stays current, and it is what we install on every client machine. That is the route this guide takes.

Step 1: Remove anything that conflicts

If someone previously installed Docker from Ubuntu's repositories, clear it out so the packages do not fight:

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

If none of those are installed, apt says so and moves on. That is fine. Your images and containers, if you had any, are not deleted by this.

Step 2: Add Docker's apt repository

First, make sure the tools for fetching the repository key are present, then download Docker's signing key:

sudo apt-get update
sudo apt-get 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

The key is how apt verifies that packages really came from Docker. Now add the repository itself:

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
sudo apt-get update

The command substitutions fill in your CPU architecture and your Ubuntu release name automatically, so the same lines work on 22.04 and 24.04, Intel or ARM. If the final apt-get update runs without errors and mentions download.docker.com, the repository is in place. These commands follow the pattern in Docker's official Ubuntu install docs, which is the page to check if anything here has shifted since we wrote it.

Step 3: Install Docker

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

Five packages: the Docker engine itself, the command-line client, the container runtime underneath, and the build and compose plugins you will want sooner or later. The service starts automatically on Ubuntu. Confirm it:

sudo systemctl status docker

You want to see "active (running)". Press q to exit the status view. Docker is also enabled to start on boot by default, which is what you want on a server.

Step 4: Run Docker without sudo

Out of the box, only root can talk to Docker. Typing sudo in front of every command gets old fast, so add your user to the docker group:

sudo usermod -aG docker $USER

Group changes only apply to new sessions, so log out and back in, or run newgrp docker to pick it up in your current shell. One honest caveat: membership in the docker group is effectively root access on that machine, because Docker can mount any part of the filesystem into a container. On your own workstation that is a fair trade. On a shared server, be deliberate about who goes in that group.

Step 5: Run hello-world

docker run hello-world

This pulls a tiny test image from Docker Hub and runs it. If everything is wired up, it prints a message beginning "Hello from Docker!" along with a short explanation of what just happened: the client talked to the engine, the engine pulled the image, created a container from it, and ran it. If you get a permission error instead, your group change has not taken effect yet, so log out and back in.

Step 6: Run a real service

hello-world proves the install, but it exits immediately. Real containers stay running and serve things. Start an nginx web server:

docker run -d --name web -p 8080:80 nginx

Reading the flags: -d runs it in the background, --name web gives it a name you can refer to, and -p 8080:80 is the important one. Containers have their own isolated network, and nginx inside is listening on port 80 in there. The -p flag maps port 8080 on your actual machine to port 80 inside the container. The pattern is always host port first, container port second.

Check that it is running:

docker ps

You should see the web container with a status of "Up" and the port mapping listed. Now hit it:

curl http://localhost:8080

That returns the "Welcome to nginx!" page. If the machine has a desktop, a browser pointed at the same address shows it too. If it is a cloud server, you would also open port 8080 in its firewall or security group to reach it from outside, but localhost proves the container works.

Look at its logs, then clean it up:

docker logs web
docker stop web
docker rm web

The logs show each request nginx served, including your curl. Stop halts the container; rm deletes it. The nginx image stays cached on disk, so next time it starts instantly.

Wrap-up

You now have Docker installed the supported way, running without sudo, and you have run both a test container and a real service with a port mapping. That covers most of what day-to-day Docker use looks like. The natural next step is Docker Compose, already installed as part of this setup, which lets you describe an app, a database, and a proxy together in one file and start them with one command. If you want a server built out this way, containers, backups, updates, and all, that is exactly the kind of work we do.

Stuck on this, or want it done for you? That's the job.

Email us →
RELATED READING
First 30 minutes on a new Ubuntu server Step-By-Step Guide
Your first Terraform project: one server on AWS Step-By-Step Guide
Set up a pfSense firewall for a small office Step-By-Step Guide
Host a static site on Cloudflare Workers Step-By-Step Guide
Azure, AWS, or Google Cloud: an honest comparison Explainer
NO FORMS. JUST EMAIL.
mason@hurbs.io
or (832) 457-4317, LA and Houston