How to Install Docker and Run a Docker Container on Ubuntu

Docker Engine lets Ubuntu run an application in an isolated container without creating a full virtual machine. This installation uses Docker’s apt repository, then confirms that the daemon can download and run the hello-world image.

Before you install Docker

Use a supported Ubuntu release and an account that can run sudo. Docker’s post-install steps explain why the daemon socket is privileged, so keep sudo for the first verification instead of adding your account to the docker group without reviewing that security boundary.

Read Linux containers if the isolation model is unfamiliar, and compare the container runtimes before choosing Docker or Podman for a production workflow.

Install Docker Engine from Docker’s apt repository

The repository uses a keyring under /etc/apt/keyrings and a deb822 source file. That keeps Docker’s signing key separate from Ubuntu’s system-wide trusted keyring.

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

sudo tee /etc/apt/sources.list.d/docker.sources > /dev/null <<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

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

The last command installs the Engine, command-line client, container runtime, Buildx, and the Docker Compose plugin. Docker documents this repository path for current Ubuntu releases and package upgrades.

Verify that Docker can run a container

Use sudo docker run hello-world for the initial verification because it asks the daemon to fetch an image, create a container, run its default process, print a confirmation message, and exit.

sudo docker run hello-world

The hello-world request checks that the client can reach the Docker daemon, that the daemon can contact the registry, and that a container can run its default process.

Run an Ubuntu shell in a container

Use the Ubuntu image with -it when you need an interactive shell because those flags keep standard input open and allocate a terminal for bash.

sudo docker run -it --rm ubuntu bash

Exit ends this container because –rm removes it after its main bash process stops, then read container image architecture and security before you build or pull larger workloads.

Use Docker without sudo only when the access tradeoff fits

Docker’s post-install guide allows membership in the docker group so that you can omit sudo, but that group grants privileges equivalent to root and is unsuitable for accounts that should not control the host.

sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker
docker run hello-world

Use this only for an account you trust with administrative control, then follow a Docker Compose deployment to describe a multi-container application in compose.yaml.

Where to go after hello-world

Start with an image you can discard and learn how a Dockerfile controls the image you build before moving to a cluster through the Kubernetes on Ubuntu setup.

For Debian hosts, use the matching Docker installation instructions for Debian because the repository codename and package setup must match the host that runs Docker.