How to Install and Use Docker on Ubuntu: Complete Beginner’s Guide for 2025

目次

1. What is Docker? Its Relationship with Ubuntu

Docker is a virtualization technology that packages application runtime environments into units called “containers,” allowing them to run consistently on any server or computer. Unlike traditional virtual machines, Docker runs containers directly on the host OS, making it possible to create a lighter and faster environment.

Linux distributions like Ubuntu have high compatibility with Docker and are widely used by developers and system administrators in both enterprise and personal settings. This is because Ubuntu is open source, popular for server use, features easy package management, and offers extensive official support.

By using Docker, you can eliminate problems like “it works on my machine but not in production.” Whether on your development PC or a production server, Docker containers allow you to quickly replicate the exact same environment anywhere. Ubuntu is also officially supported by Docker, making setup guides and resources easy to find.

This article provides a step-by-step guide to installing Docker on Ubuntu and launching an Ubuntu container. Whether you’re using Docker for the first time or want to review the basics, this guide is designed to help you succeed.

2. System Requirements & Prerequisites

There are several prerequisites and preparations needed to use Docker on Ubuntu. For a smooth installation and startup, here is a summary of the necessary environment and key points to check beforehand.

About Ubuntu Versions

It is generally recommended to use Docker on Ubuntu 18.04 LTS or newer. The most robust support is provided for long-term support (LTS) versions such as “Ubuntu 20.04 LTS,” “Ubuntu 22.04 LTS,” and “Ubuntu 24.04 LTS.” Using older versions may cause problems with required packages or dependencies, so it’s best to use the latest LTS release.

System Requirements

While Docker itself is a lightweight container technology, running multiple containers or using Docker for development and testing may require additional resources. The following minimum specifications are recommended:

  • 64-bit Ubuntu installed (32-bit is not supported)
  • CPU: 2 or more cores recommended
  • Memory: At least 2GB (4GB or more is preferred for comfortable use)
  • Available disk space: At least 10GB free

Internet Connection

An internet connection is required to install Docker and download images. The initial setup may require downloading a large amount of data, so ensure you have a stable connection.

About User Permissions

You need “sudo” (administrator) privileges to install and manage Docker. If you only have regular user rights, ask your administrator or temporarily obtain the necessary permissions beforehand.

Removing Old Docker Packages

If you have previously installed packages such as “docker” or “docker.io” manually, it is recommended to uninstall them first to avoid conflicts or unexpected issues.

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

This helps prevent troubles caused by mixed old and new packages.

Summary

Review the requirements in this section and ensure your environment is ready before proceeding with the installation and configuration steps in the next chapters.
If your setup meets these prerequisites, Docker installation will go smoothly.

3. Installing and Setting Up Docker Engine

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

Adding the Official Repository and Preparing Your System

First, add the official Docker repository to apt. All operations are performed in the terminal. Please follow these steps:

  1. Install the 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

Once the repository has been added, install Docker Engine itself:

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

After installation, verify that Docker is installed by checking the version:

docker --version

If you see output like “Docker version 25.0.3, build 1234567,” the installation was successful.

About Docker Desktop (Supplement)

On Ubuntu, you will primarily use “Docker Engine,” but “Docker Desktop for Linux” is now officially available as well. If you need a graphical interface, Docker Desktop can be a good option. However, for most command-line users, Docker Engine alone is sufficient.

If You Encounter Problems

If you get errors like “package not found,” review your repository settings and GPG key registration. If old Docker packages are installed, conflicts may occur, so double-check the uninstall steps in section 2.

4. Using Docker Commands Without sudo

Right after installation, Docker can only be used with administrator privileges (sudo). Running commands with sudo every time is inconvenient and can introduce risks if you make a typo.
A recommended approach is to add your user account to the “docker group,” allowing you to use Docker safely and comfortably without sudo.

How to Add Your User to the Docker Group

  1. Add your user to the docker group
    Run the following command to add your current user to the docker group:
    ($USER will automatically be replaced with your current username.)
sudo usermod -aG docker $USER
  1. Apply the changes
    You must log out and log back in for the group addition to take effect.
    The simplest way is to log out once and then log in again.

If you want the changes to take effect immediately, you can also use:

newgrp docker
  1. Verify
    Check if you can run Docker commands without sudo.
    For example, run the following and confirm the version is displayed:
docker version

Note on Security

While adding your user to the docker group removes the need for sudo, it also grants significant privileges. On multi-user systems, be careful with docker group membership.
For personal use or development environments, this is generally not an issue, but always follow your organization’s policies on production servers.

Completing these steps will make working with Docker much easier.

5. Starting and Managing the Docker Daemon

Docker runs as a background process called the “daemon” (dockerd).
After installation, the Docker daemon is set to start automatically, but you may want to manually start or stop it, or change its startup behavior on reboot. Here’s how to manage the Docker daemon on Ubuntu.

Starting, Stopping, and Restarting the Docker Daemon

You can easily start or stop Docker using the systemctl command.

  • Start
sudo systemctl start docker
  • Stop
sudo systemctl stop docker
  • Restart
sudo systemctl restart docker
  • Check Status
sudo systemctl status docker

This command displays the current status and any error messages.

Enabling/Disabling Auto-Start (on Boot)

On Ubuntu, you can set Docker to start automatically on server reboot.
This is usually enabled by default after installation, but here’s how to check and change this setting:

  • Enable auto-start
sudo systemctl enable docker
  • Disable auto-start
sudo systemctl disable docker

Use this if you want to start Docker manually, for example, in a development or testing environment.

Note: You Can Also Use the service Command

On older Ubuntu versions or if you prefer, you can use commands like service docker start. However, systemctl is now recommended.

Troubleshooting Tips

  • If the Docker daemon won’t start, check for errors with sudo systemctl status docker.
  • Errors may also be caused by missing dependencies or incorrect kernel settings.
  • Check logs with journalctl -u docker.

With this, you’ve covered the basics of starting and managing the Docker daemon on Ubuntu.

6. How to Launch an Ubuntu Container

Now, let’s walk through the process of launching an “Ubuntu container” on Ubuntu.
After installing Docker, no containers are running yet. The standard workflow is to obtain a “container image” of your desired OS or application, then launch it. Here, we’ll use the official Ubuntu image as a simple example.

1. Download the Ubuntu Image (pull)

First, download the Ubuntu image from Docker Hub (the official image repository).
You can specify a version like “22.04” or use the latest (if unspecified).

docker pull ubuntu:22.04

or

docker pull ubuntu

Both are valid, but it’s recommended to specify the version explicitly.

2. Launch a New Ubuntu Container (run)

Use the downloaded image to start an Ubuntu container.
The most basic command is:

docker run -it --name myubuntu ubuntu:22.04 /bin/bash
  • -it: Interactive mode (for shell access)
  • --name myubuntu: Assign a name to the container (optional)
  • ubuntu:22.04: Image to use
  • /bin/bash: Command to run when starting (bash shell)

This command lets you enter the Ubuntu environment in the container using bash.

3. Run in the Background

For server use or if you want to keep the container running, use the background option (-d):

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

In this example, the container will remain running, doing nothing.

4. Stopping and Restarting Containers

  • To stop a container
docker stop myubuntu
  • To start it again
docker start myubuntu

5. Checking Status

  • List running containers
docker ps
  • Show all containers, including stopped ones
docker ps -a

Summary

This chapter explained the flow from “downloading the Ubuntu image” to “launching a container” and basic management. Try it out yourself to experience the convenience of Docker.

7. Essential Docker Commands for Container Management

The key advantage of Docker is its flexible operation: you can easily create, stop, and remove containers.
This chapter summarizes the basic commands you’ll frequently use to manage and operate Ubuntu containers.
Practice these commands regularly to get comfortable.

Listing Containers

  • List running containers
docker ps
  • List all containers, including stopped ones
docker ps -a

Starting, Stopping, and Restarting Containers

  • Start a container
docker start [container name or ID]
  • Stop a container
docker stop [container name or ID]
  • Restart a container
docker restart [container name or ID]

Connecting to and Operating Containers

  • Execute a command in a running container (exec)
docker exec -it [container name or ID] /bin/bash

Removing Containers

  • Remove a container
docker rm [container name or ID]
  • Force remove (even if running)
docker rm -f [container name or ID]

Removing Images

  • Check unused images
docker images
  • Remove an image
docker rmi [image name or ID]

Other Useful Commands

  • Check logs
docker logs [container name or ID]
  • Check disk usage
docker system df

Key Points

  • Knowing basic commands like docker ps, docker start, docker stop, and docker exec will streamline daily operations and troubleshooting.
  • Regularly remove unused containers and images to manage disk space effectively.

8. Troubleshooting & Frequently Asked Questions

When operating Docker or Ubuntu containers, you may encounter unexpected problems or errors. This chapter covers common issues related to “starting Ubuntu Docker containers” and how to resolve them.

Common Issues and Solutions

1. “Cannot connect to the Docker daemon” Error

This error often occurs if the Docker daemon (dockerd) is not running or if there are permission issues.

  • Solution:
  1. Check if the Docker daemon is running:
    sudo systemctl status docker
  2. If it’s not running, start it with:
    sudo systemctl start docker
  3. If you need sudo, add it, or add your user to the docker group as described in section 4.

2. Conflicts with Old Docker Packages

If you have previously installed packages such as “docker,” “docker.io,” or “docker-engine,” conflicts may occur and cause issues.

  • Solution:
    Uninstall old packages.
  sudo apt-get remove docker docker-engine docker.io containerd runc

3. Container Fails to Start

This may be due to typos in the image name or command, or resource shortages (memory/disk).

  • Solution:
  • Check for typos in the image name or command.
  • Use docker images to verify the image exists.
  • Check the server’s available disk space and memory.

4. Port Conflict Errors

Errors will occur if you try to launch a container on a port already in use.

  • Solution:
  • Specify a different port number.
  • Stop the existing process.

5. Notes on Rootless Mode

Recent versions of Docker support “rootless” mode, which allows you to run Docker without root privileges, but there are limitations. Consult the official documentation and error messages for details.

Practical Checklist

  • Use docker ps -a to check container status
  • Use docker logs [container name] to check error details
  • Use journalctl -u docker to review system-wide Docker logs

Key Points

Most Docker issues are related to “permissions,” “version conflicts,” or “configuration errors.”
If you encounter problems, read the error messages carefully, and consider reinstalling or reviewing your setup if necessary.

9. Summary and Next Steps

This article has provided a comprehensive guide to “installing Docker on Ubuntu and launching Ubuntu containers.”
Let’s review the key points and suggest some tips for further learning and usage.

Summary of This Article

  • Overview and Benefits of Docker
    High compatibility with Ubuntu and the ability to quickly reproduce environments anywhere.
  • Preparation and Key Points Before Setup
    Ubuntu version, hardware requirements, permissions, and cleaning up old packages.
  • How to Install Docker Engine
    Install the latest and safest Docker using the official repository.
  • Operating Without sudo and Managing the Daemon
    Adding your user to the docker group and using systemctl for auto-start/stop.
  • How to Launch Ubuntu Containers and Use Key Commands
    Basic operations for creating, managing, and operating containers.
  • Common Issues and Solutions
    Dealing with permissions, version conflicts, and other frequent problems.

Next Steps & Advanced Usage

The first step in mastering Docker is getting used to launching containers and using basic commands. Once you’re comfortable, try more advanced operations:

  • Building Custom Containers with Dockerfile
    Beyond just using images, create customized images for your needs.
  • Managing Multiple Containers with Docker Compose
    Easily start and manage multiple services (e.g., web + DB) together.
  • Leveraging Volumes and Network Features
    Persist data and manage communication between multiple containers.
  • Using Orchestration Tools like Kubernetes
    Take your cloud operations to the next level with large-scale management and auto-scaling.

Final Thoughts

Docker isn’t just for development environments—it’s also useful for server management, service deployment, experimentation, and learning.
Try it out and experience its power and convenience.
If you have questions, consult the official documentation or community forums and keep exploring.

10. FAQ

This section covers frequently asked questions from readers about “starting Ubuntu Docker containers” and the content of this article.
We focus on common issues for beginners and practical concerns in real-world use.

Q1. Can I use Docker on both Ubuntu Desktop and Ubuntu Server?

A.
Yes, Docker works on both. The commands and steps are mostly the same, but Ubuntu Server is generally recommended for production because it has no GUI, resulting in lower resource usage.

Q2. Do I need Docker Desktop on Ubuntu?

A.
No, “Docker Engine” alone is sufficient for Ubuntu. Docker Desktop is a GUI management tool, but on Linux (Ubuntu), the command line is generally preferred and less prone to problems. If you prefer a GUI for learning or management, feel free to try Docker Desktop.

Q3. What is rootless mode?

A.
Rootless mode allows you to run Docker without root (administrator) privileges. It is useful for strengthening security or multi-user environments. However, some features may be restricted, so choose based on your needs and policies.

Q4. How do I provide persistent storage for containers?

A.
You can use “volumes” or “bind mounts” to ensure data is not lost when a container is deleted.

Example:

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

This command links a directory on your host to a directory inside the container.

Q5. How do I set containers or the Docker daemon to start automatically?

A.
To set Docker daemon to start automatically, use sudo systemctl enable docker as described in section 5.
To auto-start a specific container at boot, add options like --restart=unless-stopped when creating the container.

Example:

docker run --restart=unless-stopped -d --name myubuntu ubuntu:22.04 tail -f /dev/null

Q6. Can I run Docker images for other OSes on Ubuntu?

A.
Yes, as long as the image is for the same CPU architecture (Linux). Windows-specific or other-architecture images will not run on Ubuntu.

Q7. How can I clean up unused images or containers?

A.
Remove unused containers or images with docker rm or docker rmi. To clean up more thoroughly, use these commands:

  • Remove all stopped containers
  docker container prune
  • Remove unused images
  docker image prune
  • Clean up unused data all at once
  docker system prune

Q8. Where can I find support or information when I encounter issues?

A.
Refer to the official Docker documentation (https://docs.docker.com/) or check developer communities such as Stack Overflow and Qiita. Searching the error message directly is often effective.

Q9. How can I learn to use Docker Compose or other multi-container tools?

A.
Docker Compose is a handy tool for managing multiple containers. To learn it, refer to the official documentation, tutorials, and sample docker-compose.yml files, and try using them in practice.

Q10. Is Docker supported on the latest Ubuntu version (e.g., 24.04)?

A.
Yes, Docker officially supports the latest LTS versions. By using the latest repositories and packages, you can always enjoy stable operations.