Skip to main content

Command Palette

Search for a command to run...

Setting Up Docker on Ubuntu

A Step-by-Step Guide

Updated
8 min read
Setting Up Docker on Ubuntu
C
DevOps enthusiast with strong data science and cloud skills (AWS, Azure and Google Cloud). Experienced in building predictive models, automating processes, and improving operational efficiency. Passionate about leveraging data-driven insights to drive innovation and deliver real business value. Always eager to learn, collaborate and solve problems in dynamic tech environments

Introduction

Welcome! In this guide, I will walk you through the process of installing Docker on an Ubuntu machine. Whether you are a developer looking to streamline your workflow or a curious beginner, this step-by-step tutorial is designed to get your environment up and running from scratch.

Beyond the initial installation, we will also dive into:

  • Essential Docker Commands: The core tools you need to manage containers effectively.

  • Layered Architecture: A brief explanation of how Docker’s efficient "stacking" system works behind the scenes.


By the end of this guide, you will not only have Docker installed but also a solid understanding of how it interacts with your host system.

Step 1: Clean the Slate

Before we dive into the fresh installation, it is best practice to remove any existing or conflicting Docker-related packages. This ensures a "clean slate" and prevents version conflicts that could cause headaches later on.

Run the following command in your terminal to purge any old versions of Docker, along with related tools like Podman or containerd:

Bash

sudo apt remove $(dpkg --get-selections docker.io docker-compose docker-compose-v2 docker-doc podman-docker containerd runc | cut -f1)

Note: Don't worry if the terminal tells you some of these packages aren't installed; the command is simply being thorough to ensure nothing interferes with our new setup.

Step 2: Set Up the Official Docker Repository

Now that your system is clear of old installations, we need to tell Ubuntu exactly where to find the official, most up-to-date Docker packages. We do this by adding Docker’s GPG key (to verify the software's authenticity) and registering their official repository.

Run the following blocks of code in your terminal:

1. Add Docker's Official GPG Key

These commands ensure your system trusts the software we're about to download.

Bash

# Update your package index and install necessary certificates
sudo apt update
sudo apt install ca-certificates curl

# Create a directory for the keyring and download the official GPG key
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

2. Add the Repository to APT Sources

This tells Ubuntu to look at Docker's servers whenever you run an update.

Bash

# Register the repository in your sources list
sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: \((. /etc/os-release && echo "\){UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Signed-By: /etc/apt/keyrings/docker.asc
EOF

# Update the package index again to include the new Docker repository
sudo apt update

Step 3: Install the Docker Engine

With the repository now in place, we are ready to install the actual Docker software. This step installs the core Docker Engine**,** the Command Line Interface (CLI), and essential plugins like Buildx and Compose.

Run the following command to download and install the latest versions:

Bash

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

Understanding the Components

It’s helpful to know what you just installed:

  • docker-ce: The Community Edition of the Docker engine.

  • docker-ce-cli: The tool that lets you talk to Docker from your terminal.

  • containerd.io: The industry-standard runtime that manages the container lifecycle.

  • Docker Plugins: Tools for building advanced images and managing multi-container setups.

Step 4: Verify Your Installation

Everything is now installed! To confirm that the Docker Engine is correctly set up and ready for use, run the following command in your terminal:

Bash

docker --version

You should see an output confirming the version installed, similar to:Docker version 29.3.0, build [build-id]


A Brief Note on Docker’s Architecture

Now that you have it running, it helps to understand how it works. Docker uses a client-server architecture.

  • The Docker Client: This is what you just used. When you type docker --version or any other command, the client sends these requests to the Docker Daemon (dockerd).

  • The Docker Daemon: This is the "brain" of the operation. It listens for API requests and manages Docker objects, such as images, containers, networks, and volumes.

  • Layered Architecture: Docker images are built using a series of layers. Each layer represents an instruction in your Dockerfile. These layers are stacked on top of each other, and each one is a read-only filesystem. This layering makes Docker incredibly efficient because it caches layers that haven't changed, speeding up builds and reducing disk space.

Step 5: Check the Docker Service Status

While the version command confirms the software is installed, checking the service status ensures that the Docker daemon (the engine that does all the heavy lifting) is actually running in the background.

Run the following command:

Bash

sudo systemctl status docker

What to Look For

You should see an output that includes a line similar to this:

● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since...

Key Indicators:

  • Active (running): This is the "green light." It means Docker is currently alive and waiting for your instructions.

  • Enabled: This means Docker is set to start automatically whenever you turn on or reboot your computer.

Step 6: The Ultimate Test (Running "Hello World")

Now that we’ve confirmed Docker is installed and the service is active, it’s time for the "moment of truth." We will run a test container to ensure Docker can successfully pull images from the cloud and execute them.

Run the following command:

Bash

sudo docker run hello-world

What is happening behind the scenes?

When you run this, Docker follows a specific sequence of events:

  1. The Request: The Docker client contacts the Docker daemon.

  2. The Search: The daemon looks for the "hello-world" image locally.

  3. The Download (Pull): Since you don't have it yet, the daemon "pulls" the image from Docker Hub.

  4. The Execution: The daemon creates a new container from that image and runs the executable inside.

Expected Output:

The Secret Sauce: Docker’s Layered Architecture

To understand why Docker is so fast and efficient, we need to look at its Layered Architecture. Unlike a traditional Virtual Machine (VM) that carries a whole operating system, Docker uses a Union File System to stack layers on top of each other.

How it Works:

  1. The Base Layer: This is usually a slimmed-down operating system (like Ubuntu or Alpine Linux).

  2. Intermediate Layers: Every time you add a command (like apt install python), Docker creates a new "layer."

  3. The Read-Only Image: All these layers are "Read-Only." This means they are frozen in time and can be shared across multiple containers.

  4. The Writable Layer (Container Layer): When you finally run a container, Docker adds a thin "Read-Write" layer on top. Any changes you make while the app is running happen only in this top layer.


Why is this better?

  • Storage Efficiency: If you have 10 containers based on Ubuntu, Docker only stores the Ubuntu base layer once on your disk.

  • Speed: Since the layers are already built and cached, starting a new container takes seconds, not minutes.

  • Consistency: Because layers are "Immutable" (unchanging), the environment on your laptop will be identical to the environment in production.


Essential Docker Commands to Get You Started

Now that you're set up, here are the tools you'll use every day:

Command

What it does

docker ps

Lists all running containers.

docker ps -a

Lists all containers (even the stopped ones).

docker images

Shows all the images you have downloaded.

docker stop <ID>

Safely shuts down a running container.

docker rm <ID>

Deletes a container (must be stopped first).

docker rmi <Name>

Deletes an image from your hard drive.

Step 7: Building Your First Custom Image (The Dockerfile)

Now that you understand the layers, let's actually build one. To do this, we use a Dockerfile a simple text file that contains the "recipe" for your environment.

1. Create a Project Directory

First, let's create a space for our project:

Bash

mkdir my-docker-app && cd my-docker-app

2. Create the Dockerfile

Create a file named Dockerfile (no extension) and paste the following:

Dockerfile

# Step 1: Use an existing image as a base
FROM ubuntu:22.04

# Step 2: Install a simple tool (like 'figlet' for fun text)
RUN apt-get update && apt-get install -y figlet

# Step 3: Tell the container what to do when it starts
CMD ["figlet", "Docker is Awesome!"]

3. Build the Image

Run the following command to turn that text file into a real Docker image. The . at the end tells Docker to look in the current folder:

Bash

sudo docker build -t my-custom-app .

4. Run Your Custom App

Now, run the image you just created:

Bash

sudo docker run my-custom-app

You should see "Docker is Awesome!" printed in large ASCII art in your terminal!


Summary of What You've Learned

  • Installation: You've set up the official Docker repository on Ubuntu.

  • Service Management: You know how to check if the Docker daemon is running.

  • Architecture: You understand that Docker images are made of Read-Only layers with a Writable layer on top.

  • Automation: You've built your own image using a Dockerfile.

13 views
Setting Up Docker on Ubuntu