Gegenfeld
6 min read

How to install Docker on Ubuntu

How to install Docker on Ubuntu

Docker simplifies development by running applications in lightweight, isolated containers that bundle all dependencies, making deployments faster and more portable than traditional virtual machines.

This guide explains how to install Docker CE on Ubuntu 20.04, work with images and containers, and push custom images to a Docker repository.

Prerequisites

To start, make sure you have the following this ready:

  • Ubuntu 20.04 server

  • Docker Hub account

Need a server? We're partnered with UpCloud — enjoy €25 in free credits to deploy cloud servers for self-hosting and more!

Installing Docker

Updating the Package List

Begin by refreshing your package list to ensure that your system retrieves the latest version information for all packages. Run the following command:

Updating Package List
bash
sudo apt update

This command connects to Ubuntu repositories and updates the local package database, setting the stage for installing Docker’s dependencies.

Installing Required Packages

Install the necessary packages that enable apt to use HTTPS for package downloads. Secure downloads and verify package integrity by executing:

Install core packages for HTTPS-enabled APT repositories
bash
sudo apt install apt-transport-https ca-certificates curl software-properties-common

These packages are essential for handling secure communication and software property management during the Docker installation process.

Adding Docker’s GPG Key

To maintain package authenticity, add Docker’s official GPG key. This step validates the Docker packages you will download and protects against potential tampering:

Download and add Docker GPG key to APT
bash
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Adding the Docker Repository

Incorporate Docker’s repository into your package sources to access the most recent Docker CE releases. Execute the following command to add the repository for Ubuntu 20.04 (focal):

Add Docker APT repository for Ubuntu
bash
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

This action updates your package sources and ensures that Docker packages are retrieved from Docker’s official repository.

Verifying the Repository Setup

Before installing Docker, confirm that the repository has been added correctly. Use the command below to display Docker CE’s candidate version:

Show available Docker CE versions and repository source
bash
apt-cache policy docker-ce

This verification step ensures that Docker packages will be installed from the correct source rather than the default Ubuntu repository.

Installing Docker

Now that the repository is set up, install Docker CE by running:

Install Docker CE
bash
sudo apt install docker-ce

This command installs both the Docker daemon and the Docker command-line interface, preparing your system for container management.

Checking Docker’s Status

After installation, verify that Docker is running and configured to start on boot. Check the service status with:

Check Docker service status
bash
sudo systemctl status docker

The output should indicate that Docker is active and running, confirming that the installation was successful.

Running Docker Without Sudo

Adding Your User to the Docker Group

By default, Docker commands require sudo privileges. To allow running Docker commands without sudo, add your user to the Docker group with:

Add user to the Docker group
bash
sudo usermod -aG docker ${USER}

Applying the Group Change

After modifying group membership, you need to apply the change. Either log out and log back in or run the following command to refresh your session:

Switch user to apply group changes
bash
su - ${USER}

Confirming Group Membership

Confirm that your user is now a member of the Docker group by executing:

Show groups of the current user
bash
groups

This output should list “docker” among your groups, enabling you to run Docker commands without sudo.

Using Docker Commands

Understanding Docker’s Command Structure

Docker commands follow a consistent structure that simplifies container management. The general syntax is:

Basic structure of a Docker command
bash
docker [option] [command] [arguments]

This structure makes it straightforward to explore Docker’s capabilities and execute specific commands.

Accessing Help for Commands

To view a list of all available Docker commands, type:

Launch Docker CLI and display help
bash
docker

For detailed help regarding any specific command, append the --help option, as shown below:

Display help and available options for docker run
bash
docker run --help

This approach provides comprehensive guidance on command usage and available options.

Working with Docker Images

Testing Docker with the Hello-World Image

Images form the foundation of Docker containers. Validate your installation by running a simple test with the hello-world image:

Testet die Docker-Installation mit dem „Hello World“-Container
bash
docker run hello-world

When executed, Docker pulls the hello-world image (if not already available), creates a container, and displays a welcome message that confirms your setup is correct.

Searching for Docker Images

To explore images available on Docker Hub, you can search for specific images. For example, to look for an Ubuntu image, run:

Nach verfügbaren Ubuntu-Containern im Docker Hub suchen
bash
docker search ubuntu

This command returns a list of available images, providing details about each image’s origin and popularity.

Pulling an Image from Docker Hub

Download the Ubuntu image by executing the pull command:

Pull the official Ubuntu image from Docker Hub
bash
docker pull ubuntu

This command retrieves the image from Docker Hub and stores it locally for subsequent use.

Listing Downloaded Images

After pulling images, verify their presence on your system by listing them with:

List all locally available Docker images
bash
docker images

The output displays details such as image names, tags, sizes, and creation dates.

Running a Docker Container

Launching an Interactive Container

To launch a container based on the Ubuntu image with interactive shell access, execute:

Start an interactive Ubuntu container
bash
docker run -it ubuntu

The -it options allocate a pseudo-TTY and keep the standard input open, allowing you to interact directly with the container.

Updating the Container and Installing Applications

Once inside the container, update its package database with:

Update APT package lists
bash
apt update

Install applications such as Node.js to observe changes within the container environment:

Install Node.js
bash
apt install nodejs

Verify the installation by checking the Node.js version:

Show installed Node.js version
bash
node -v

Exiting the Container

When you have finished working inside the container, type exit to leave the interactive session. This action terminates the container’s interactive mode while preserving any modifications you may have made.

Managing Docker Containers

Viewing Active Containers

Monitor your running containers by listing only the active ones:

List running Docker containers
bash
docker ps

This command provides details such as container IDs, images used, and runtime status.

Viewing All Containers

To obtain a complete list that includes both running and stopped containers, use:

List all Docker containers, including stopped ones
bash
docker ps -a

This comprehensive view aids in tracking container history and usage.

Starting and Stopping Containers

Restart a stopped container by specifying its container ID or name with:

Start a stopped Docker container
bash
docker start [container_id_or_name]

Conversely, stop a running container with:

Stop a running Docker container
bash
docker stop [container_id_or_name]

These commands enable you to control container states effectively.

Removing Unnecessary Containers

When a container is no longer needed, remove it to free system resources. Use the command below, replacing the placeholder with the actual container identifier:

Remove a Docker container
bash
docker rm [container_id_or_name]

Removing unused containers helps maintain a clean and efficient environment.

Creating Docker Images

Committing Changes from a Container

After making modifications within a container, preserve the changes by committing them to a new image. Run the commit command along with a descriptive message and author information:

Save changes from a container as a new Docker image
bash
docker commit -m "Installed Node.js and updated packages" -a "Your Name" [container_id] new_image_name

This process creates a snapshot of your container that you can reuse for future deployments.

Confirming the New Image

Verify that the new image has been successfully created by listing your local images:

List all locally available Docker images
bash
docker images

This verification ensures that your custom image appears among the available images.

Pushing Images to Docker Hub

Logging into Docker Hub

To share your custom image, begin by logging into Docker Hub with your credentials:

Log in to a Docker registry account
bash
docker login

Successful authentication allows you to push images to your Docker Hub repository.

Tagging the New Image

If your Docker Hub username differs from your local username, retag your image appropriately:

Tag a Docker image for Docker Hub
bash
docker tag new_image_name your_dockerhub_username/new_image_name

Proper tagging ensures that the image is associated with your Docker Hub account.

Pushing the Image to the Repository

Upload your image to Docker Hub with the push command:

Push a Docker image to Docker Hub
bash
docker push your_dockerhub_username/new_image_name

This command transmits your image to the repository, making it available for use on other systems. The process may take several minutes, depending on your network speed and image size.

Conclusion

You have now installed Docker on Ubuntu 20.04, explored fundamental Docker commands, managed images and containers, and pushed a custom image to Docker Hub.

Written by

G

Gegenfeld Team

Gegenfeld Team