How to Install Docker on Ubuntu and Run an Ubuntu Container (Step-by-Step Guide)

1. What Is Docker and Its Relationship with Ubuntu

Docker is a virtualization technology that packages application runtime environments into units called “containers,” allowing them to run consistently across different servers and computers. Unlike traditional virtual machines, Docker runs containers directly on the host OS, making it significantly lighter and faster.

Linux-based operating systems such as Ubuntu are especially well suited for Docker, which is why Docker is widely used by both enterprises and individual developers and server administrators. This is because Ubuntu itself is open source, widely adopted for server environments, easy to manage with package systems, and supported by extensive official documentation.

By using Docker, you can eliminate common issues such as “it works on my machine but not on the server.” Whether on a development PC or a production server, Docker containers allow you to reproduce the exact same environment anywhere. Ubuntu is also officially supported by Docker, so installation guides and troubleshooting resources are abundant.

In this article, we will clearly explain how to install Docker on Ubuntu and start an Ubuntu container. Whether you are new to Docker or already have experience, this guide will help you reinforce your foundational knowledge.

2. Required Environment and Prerequisites

Before using Docker on Ubuntu, several prerequisites and checks are necessary. This section summarizes the required environment and important points to confirm in advance to ensure a smooth installation.

Ubuntu Version

Docker is recommended for Ubuntu 18.04 LTS or later. Long Term Support (LTS) versions such as Ubuntu 20.04 LTS, Ubuntu 22.04 LTS, and Ubuntu 24.04 LTS are especially well supported. Older versions may encounter dependency or package issues, so using the latest LTS version is strongly recommended.

System Requirements

Although Docker is lightweight, running multiple containers or using it for development and testing requires sufficient system resources. The following minimum specifications are recommended:

  • 64-bit Ubuntu (32-bit is not supported)
  • CPU: 2 cores or more recommended
  • Memory: At least 2 GB (4 GB or more recommended)
  • Free disk space: At least 10 GB

Internet Connection

An internet connection is required to install Docker and download images. The initial setup may involve downloading large amounts of data, so a stable connection is recommended.

User Privileges

Installing Docker and managing system settings requires sudo (administrator) privileges. If you only have standard user permissions, request temporary access or assistance from an administrator.

Removing Old Docker Packages

If you previously installed packages such as docker or docker.io manually, it is recommended to uninstall them to avoid conflicts.

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

This prevents potential issues caused by conflicting legacy packages.

Summary

After confirming the prerequisites in this section, proceed to the installation and setup steps in the following chapters.
With proper preparation, installing Docker on Ubuntu is a very smooth process.

3. Installing and Setting Up Docker Engine

This section explains how to install Docker Engine on Ubuntu and perform basic setup. Using the official Docker repository ensures you get the latest stable version.

Adding the Official Repository and Preparation

First, add Docker’s official repository to apt. All steps are performed in the terminal.

  1. Install required packages
sudo apt-get update
sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
  1. Add the GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  1. Add the Docker repository
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

Installing Docker Engine

After adding the repository, install Docker Engine.

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

Once installation is complete, verify it by checking the version:

docker --version

If you see output such as Docker version 25.0.3, the installation was successful.

About Docker Desktop (Optional)

On Ubuntu, Docker Engine is usually sufficient. Docker Desktop for Linux is also available if you need a graphical interface, but for CLI-based workflows, Docker Engine alone is more than enough.

Troubleshooting Installation Issues

If you encounter errors such as “package not found,” double-check the repository configuration and GPG key setup. Conflicts with old Docker packages can also cause issues, so revisit the uninstall steps if necessary.

4. Using Docker Without sudo

By default, Docker commands require sudo privileges. Requiring sudo for every command is inconvenient and can be risky if commands are mistyped.
The recommended approach is to add your user account to the docker group, allowing you to use Docker safely without sudo.

Adding a User to the docker Group

  1. Add the current user to the docker group
sudo usermod -aG docker $USER
  1. Apply the changes

Log out and log back in to apply the new group membership.
Alternatively, you can apply it immediately using:

newgrp docker
  1. Verify
docker version

Security Notes

Users in the docker group have elevated privileges. In multi-user environments, manage docker group membership carefully. For personal or development use, this is generally not an issue.

5. Starting Docker Daemon and Enabling Auto-Start

Docker runs as a background service called the Docker daemon (dockerd). While it is usually started automatically after installation, understanding how to manage it is important.

Start, Stop, and Restart Docker

sudo systemctl start docker
sudo systemctl stop docker
sudo systemctl restart docker
sudo systemctl status docker

Enable or Disable Auto-Start on Boot

sudo systemctl enable docker
sudo systemctl disable docker

Checking Logs

journalctl -u docker

6. Starting an Ubuntu Container

This section explains how to actually start an Ubuntu container on Docker.

Pulling the Ubuntu Image

docker pull ubuntu:22.04

Running an Ubuntu Container

docker run -it --name myubuntu ubuntu:22.04 /bin/bash

Running in the Background

docker run -d --name myubuntu ubuntu:22.04 tail -f /dev/null

Stopping and Restarting Containers

docker stop myubuntu
docker start myubuntu

Checking Container Status

docker ps
docker ps -a

7. Essential Docker Commands for Container Management

This section summarizes commonly used Docker commands for daily operations.

Container Listing

docker ps
docker ps -a

Start, Stop, Restart

docker start [container]
docker stop [container]
docker restart [container]

Accessing Containers

docker exec -it [container] /bin/bash

Removing Containers and Images

docker rm [container]
docker rm -f [container]
docker images
docker rmi [image]

Logs and Disk Usage

docker logs [container]
docker system df

8. Troubleshooting and Frequently Asked Issues

This section covers common Docker issues and how to resolve them.

Cannot Connect to the Docker Daemon

Ensure Docker is running and that your user has proper permissions.

Package Conflicts

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

Container Startup Failures

Check image names, available memory, and disk space.

9. Summary and Next Steps

This article covered installing Docker on Ubuntu and running Ubuntu containers, from basic concepts to practical usage.

Next Steps

  • Building custom images with Dockerfile
  • Managing multiple containers with Docker Compose
  • Using volumes and networks
  • Exploring container orchestration with Kubernetes

10. FAQ

This section answers frequently asked questions related to Docker on Ubuntu.

Can Docker be used on both Ubuntu Desktop and Server?

Yes. Ubuntu Server is often preferred for production due to lower resource usage.

Is Docker Desktop required?

No. Docker Engine alone is sufficient on Ubuntu.

What is rootless mode?

Rootless mode allows Docker to run without root privileges, improving security with some limitations.

How do I persist data?

docker run -v /host/path:/container/path ubuntu:22.04

How do I auto-start containers?

docker run --restart=unless-stopped -d ubuntu:22.04