Docker is the standard container runtime for building, shipping, and running applications in isolated environments. It packages your application with all its dependencies into a container that runs consistently across any Linux system.
This guide covers installing Docker Engine 29.x from the official Docker repository on Ubuntu 24.04 or 22.04, then walks through building images, running containers, and managing multi-container applications with Docker Compose.
Prerequisites
- A server or desktop running Ubuntu 24.04 or 22.04 LTS
- Root or sudo access
- Internet access to pull images from Docker Hub
Step 1: Update System Packages
Update your package index and install any available upgrades:
sudo apt update && sudo apt -y upgrade
Reboot if a kernel update was applied:
[ -f /var/run/reboot-required ] && sudo reboot -f
Step 2: Add Docker APT Repository
Ubuntu’s default repositories include an older docker.io package. For the latest Docker Engine, add the official Docker repository.
Install the prerequisites and download Docker’s GPG signing key:
sudo apt install -y 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
Add the Docker repository. This command automatically detects your Ubuntu version codename:
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 to include packages from the new repository:
sudo apt update
Step 3: Install Docker Engine
Install Docker Engine, the CLI, containerd, and the Buildx and Compose plugins:
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Verify the installed versions:
docker --version
You should see Docker Engine 29.x confirmed:
Docker version 29.3.0, build 5927d80
Check the Docker Compose plugin version:
docker compose version
Compose v5.x should be available:
Docker Compose version v5.1.1
Step 4: Allow Non-Root User to Run Docker
By default, Docker commands require sudo. Add your user to the docker group to run Docker without sudo:
sudo usermod -aG docker $USER
newgrp docker
The newgrp command activates the group membership in your current shell without requiring a logout. Verify it works by running a test container:
docker run --rm hello-world
If Docker is working correctly, you will see “Hello from Docker!” in the output confirming the installation is successful.
Step 5: Run Your First Container
Pull and run an Nginx web server container, mapping port 8080 on the host to port 80 inside the container:
docker run -d --name web -p 8080:80 nginx:alpine
The -d flag runs the container in the background. Check that it is running:
docker ps
The container list shows Nginx running on port 8080:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
a1b2c3d4e5f6 nginx:alpine "/docker-entrypoint.…" 5 seconds ago Up 5 seconds 0.0.0.0:8080->80/tcp
Test access to the container with curl:
curl -s localhost:8080 | head -5
You should see the default Nginx welcome page HTML, confirming the container is serving traffic.
Step 6: Build a Custom Docker Image
Create a project directory and a Dockerfile for a simple Apache web server:
mkdir ~/docker-demo && cd ~/docker-demo
Create the Dockerfile:
vi Dockerfile
Add the following instructions that build an Apache image from the official Debian base:
FROM debian:bookworm-slim
LABEL maintainer="[email protected]"
RUN apt-get update && \
apt-get install -y --no-install-recommends apache2 && \
rm -rf /var/lib/apt/lists/*
RUN echo "Hello from Docker on Ubuntu" > /var/www/html/index.html
EXPOSE 80
CMD ["apachectl", "-D", "FOREGROUND"]
Build the image and tag it:
docker build -t my-apache:latest .
List your local images to confirm it was built:
docker images
The output shows your new image alongside the Nginx image pulled earlier:
REPOSITORY TAG IMAGE ID CREATED SIZE
my-apache latest abc123def456 5 seconds ago 180MB
nginx alpine def456abc789 2 weeks ago 50MB
Run a container from your custom image:
docker run -d --name my-web -p 8081:80 my-apache:latest
Test it:
curl localhost:8081
The response should return the custom message from the Dockerfile:
Hello from Docker on Ubuntu
Step 7: Multi-Container Apps with Docker Compose
Docker Compose lets you define and run multi-container applications from a single YAML file. Create a project that runs Nginx with a Redis cache:
mkdir ~/compose-demo && cd ~/compose-demo
mkdir html
Create a simple HTML page:
echo '<h1>Docker Compose is running</h1>' > html/index.html
Create the Compose file:
vi compose.yaml
Define two services – a web server and a Redis instance:
services:
web:
image: nginx:alpine
ports:
- "8090:80"
volumes:
- ./html:/usr/share/nginx/html:ro
redis:
image: redis:alpine
ports:
- "6379:6379"
Start all services in the background:
docker compose up -d
Check the running containers:
docker compose ps
Both services should show as running:
NAME IMAGE SERVICE STATUS PORTS
compose-demo-redis-1 redis:alpine redis Up 3 seconds 0.0.0.0:6379->6379/tcp
compose-demo-web-1 nginx:alpine web Up 3 seconds 0.0.0.0:8090->80/tcp
Test the web service:
curl localhost:8090
The response confirms Compose is working:
<h1>Docker Compose is running</h1>
Stop and remove all containers when done:
docker compose down
Essential Docker Commands Reference
Here are the most common Docker commands you will use daily:
| Command | Description |
|---|---|
docker ps | List running containers |
docker ps -a | List all containers including stopped |
docker images | List local images |
docker logs CONTAINER | View container logs |
docker exec -it CONTAINER bash | Open a shell inside a running container |
docker stop CONTAINER | Stop a running container |
docker rm CONTAINER | Remove a stopped container |
docker rmi IMAGE | Remove a local image |
docker system prune -a | Remove all unused images, containers, and networks |
docker compose up -d | Start Compose services in background |
docker compose down | Stop and remove Compose services |
Conclusion
Docker Engine 29.x and Compose v5.x are running on your Ubuntu system. You can now build images, run containers, and orchestrate multi-container applications.
For production deployments, consider setting up a private Docker registry for your images, configuring log rotation for container logs, and exploring Ansible for managing Docker containers at scale.