How to Install Docker on Ubuntu 24.04 or 22.04

Docker is a pioneering force in containerization technology. This guide will show you how to install Docker on Ubuntu, helping you leverage this open-source platform that has revolutionized how developers package, deploy, and manage applications, ensuring consistency and efficiency across various environments.

Below is a list of features that have made Docker the force it is today:

  • Containerization: Docker encapsulates applications and their dependencies within isolated containers, ensuring consistent performance across diverse environments.
  • Portability: With Docker, applications encapsulated in containers can be effortlessly transitioned between environments, be it development, testing, or production.
  • Resource Efficiency: Docker’s lightweight design ensures minimal resource consumption, optimizing performance and cost.
  • Enhanced Security: Docker fortifies containers with robust security features, including isolated filesystems and controlled network access.
  • Broad Compatibility: Docker seamlessly integrates with many programming languages, frameworks, and tools, granting developers the flexibility to employ their preferred resources.

Docker harnesses containerization by diverging from conventional virtualization methods that utilize hypervisors for virtual machine management. This methodology enables developers to craft lightweight deployments, simplifying application management and scalability.

This guide will demonstrate the installation process using the APT package manager and Docker’s APT repository.

Pre-installation Steps

Before you install Docker on Ubuntu, it’s important to remove any previous Docker installations to avoid conflicts. These steps ensure a clean environment for your new Docker setup.

Run the following command to eliminate these older versions:

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

Note: If you have not installed any of these packages, apt will send a message indicating nothing to remove.

Warning: The following commands will permanently delete all Docker images, containers, volumes, and networks stored in /var/lib/docker/ and /var/lib/containerd/. Only run these if you want a completely clean Docker installation.

Uninstalling Docker does not automatically remove images, containers, volumes, and networks stored in /var/lib/docker/. To start with a clean installation and delete all existing data, use the following commands:

 sudo rm -rf /var/lib/docker
 sudo rm -rf /var/lib/containerd

After removing any older versions of Docker, you should update your system to ensure that all packages are up-to-date and avoid potential conflicts. You can do this by running the following command:

sudo apt update && sudo apt upgrade

This command updates the list of available packages and upgrades any ones that need updating.

Import Docker APT Repository for Docker Installation on Ubuntu

Before installing Docker, import the Docker repository and GPG key into your system. This ensures that your system can verify the downloaded packages from the repository and prevent unauthorized changes.

To do this, you can first install the required packages by running the following command:

sudo apt install ca-certificates curl gnupg lsb-release dirmngr software-properties-common apt-transport-https

This command installs the necessary packages for importing the Docker repository.

Next, you can import the GPG key using the following command:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker.gpg > /dev/null

This command downloads the GPG key from the Docker repository and saves it in the /usr/share/keyrings/ directory.

Finally, you can import the Docker repository by running the following command:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list

This command adds the Docker repository to your system’s sources list and configures it to use the GPG key downloaded earlier.

Install Docker on Ubuntu

Now you’re ready to install Docker on Ubuntu using the official Docker repository. This section covers the commands and steps required to install Docker on Ubuntu efficiently and securely.

Update your system before installing Docker to ensure your source lists accurately reflect the newly imported repository. Run the following command to do this:

sudo apt update

Once your system is up-to-date, you can install Docker using the following command:

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

This command will install Docker and some additional plugins that you may find helpful.

After you install Docker on Ubuntu, you can verify the installation by running a test container. This helps confirm that Docker is set up correctly on your Ubuntu system.

Example of using hello-world on Docker with Ubuntu:

It’s important to note that the docker command requires root privileges to run. However, you can configure Docker to run as a non-root user, which provides a more secure way to manage containers and images.

If you experience any problems while working with Docker images going forward, try restarting your system, as sometimes this can fix issues related to path generation.

reboot

Managing Docker with systemd on Ubuntu

Systemd, a system and service manager, simplifies Ubuntu’s process and service management. Upon Docker installation on Ubuntu, it establishes a systemd unit to oversee the Docker service. Systemd commands can control this unit, offering a convenient method for starting, stopping, and managing Docker containers and images.

Below are some frequently used systemd commands for Docker management on Ubuntu:

systemctl start docker.service

This command starts the Docker service, enabling it to run on system boot.

systemctl stop docker.service

The above command stops the Docker service from running on the system boot.

systemctl restart docker.service

This command restarts the Docker service.

systemctl status docker.service

The command displays the status of the Docker service and whether it is currently running.

systemctl enable docker.service

This command enables the Docker service to start on system boot.

systemctl disable docker.service

This command disables the Docker service from starting on the system boot.

Rundown of Docker Configuration Setup on Ubuntu

Manage Docker as a non-root user on Ubuntu

When running Docker, avoid using the root user to prevent security risks and accidental changes to the host system; instead, manage Docker as a non-root user.

If you want to add a new user for Docker management, use the following command. Note: If the user already exists, skip this step.

sudo useradd -m dockeruser

To add an existing user (such as your current user) to the Docker group, use:

sudo usermod -aG docker 

After adding a user to the Docker group, log out and back in or reboot your system for the changes to take effect.

To ensure the user can run Docker commands, use the following command:

docker ps

This command displays a list of running containers, confirming the correct installation of Docker.

Configure default logging driver

By default, Docker logs to the JSON file format. However, you can configure the default logging driver to use a different format or send logs to a remote log management system.

To change the default logging driver, you can create a new file called daemon.json in the /etc/docker/ directory using a text editor such as nano. For example, you can use the following command to create the file:

sudo nano /etc/docker/daemon.json

Once the file is open, you can add the following contents:

{
  "log-driver": "syslog",
  "log-opts": {
    "syslog-address": "tcp://logs.example.com:514",
    "syslog-facility": "daemon",
    "tag": "{{.Name}}"
  }
}

In this example, we configure Docker to use the syslog driver and to send logs to a remote syslog server. Replace “logs.example.com” with your syslog server’s address.

After creating the file, you can use the following command to restart the Docker daemon and ensure that the changes made to the logging driver take effect:

sudo systemctl restart docker.service

It’s important to note that if you change the daemon.json file, you must restart the Docker daemon again to reload the changes.

Using the Docker Command after Installing Docker on Ubuntu

When working with Docker, one must be familiar with the docker command used to manage Docker containers, images, networks, and volumes. Here are some of the most commonly used docker commands:

CommandDescription
docker runRun a new container from an image.
docker psList all running containers.
docker imagesList all available images.
docker buildBuild a new image from a Dockerfile.
docker stopStop a running container.
docker rmRemove a container.
docker rmiRemove an image.
docker networkManage Docker networks.
docker volumeManage Docker volumes.

docker run

The docker run command runs a new container from an image. For example, to run a container from the ubuntu image, you can use the following command:

docker run -it ubuntu:latest /bin/bash

This command will start a new container from the ubuntu image and open a shell inside the container.

docker ps

The docker ps command lists all running containers and provides information about each, including container ID, image name, and status.

docker ps

This command will display a list of all running containers.

docker images

The docker images command lists all available images and supplies information about each image, including image ID, repository, and tag.

docker images

This command will display a list of all available images.

docker build

The docker build command builds a new image from a Dockerfile, a script containing instructions for building an image.

docker build -t myimage:latest .

This command will build a new image called myimage using the Dockerfile in the current directory.

docker stop

The docker stop command stops a running container. For example, to stop a container with the ID abcdefg, use the command:

docker stop abcdefg

This command will stop the container with the ID abcdefg.

docker rm

Use the docker rm command to remove a container. For example, to eliminate a container with the ID abcdefg, execute the following command:

docker rm abcdefg

This command will remove the container with the ID abcdefg.

docker rmi

The docker rmi command removes an image. For example, to eliminate an image with the ID 1234567, use the following command:

docker rmi 1234567

This command will remove the image with the ID 1234567.

docker network

The docker network command manages Docker networks. It offers options to create, list, and remove networks.

docker network create mynetwork

This command will create a new network called mynetwork.

docker volume

The docker volume command manages Docker volumes, providing options to create, list, and remove volumes.

docker volume create myvolume

This command will create a new volume called myvolume.

Running a Docker Container on Ubuntu

To run a container from an image, you can use the docker run command followed by the image name. For example, to run a container from the ubuntu image, you can use the following command:

docker run ubuntu

This command will download the ubuntu image and run a container from it. You can also specify the version of the image by adding a tag. For example, to run a container from the ubuntu image version 20.04, you can use the following command:

docker run ubuntu:20.04

This command will download the ubuntu image version 20.04 and run a container from it.

When you run a container, Docker initiates a new image instance as a container. Each container operates in isolation, possessing its own file system, networking, and resources. You can interact with the running container using a shell or command prompt. For instance, execute the command below to open a shell inside the container:

docker run -it ubuntu /bin/bash

Managing Docker Containers on Ubuntu

To manage Docker containers, you can use the docker ps command to list all running containers. For example, to list all running containers on your system, you can use the following command:

docker ps

This command will display a list of all running containers, including their container ID, image name, and status.

To stop a running container, you can use the docker stop command followed by the container ID or name. For example, to stop a container with the ID abcdefg, you can use the following command:

docker stop abcdefg

This command will stop the container with the ID abcdefg.

To remove a container, you can use the docker rm command followed by the container ID or name. For example, to remove a container with the ID abcdefg, you can use the following command:

docker rm abcdefg

This command will remove the container with the ID abcdefg.

When you remove a container, you lose all changes made to that container. To save changes as a new image, use the docker commit command, which creates a new image from the container.

Committing Changes in a Container to a Docker Image on Ubuntu

When working with Docker containers, changing the container you want to save as a new image is common. You can use the docker commit command to commit changes in a container to a Docker image.

First, start a new container from the base image and make any necessary changes to the container. For example, to start a new container from the ubuntu image and open a shell inside the container, you can use the following command:

docker run -it --name mycontainer ubuntu:latest /bin/bash

This command will start a new container from the ubuntu image and open a shell inside the container. You can make any necessary changes to the container, such as installing new software or modifying configuration files.

Once you have made the necessary changes, you can use the docker commit command to create a new image from the container. For example, to create a new image called myimage with the changes made in the mycontainer container, you can use the following command:

docker commit mycontainer myimage:latest

This command will create a new image called myimage with the changes made in the mycontainer container. You can now use this new image to create and run new containers with the updated software or configuration.

It’s important to note that the docker commit command only saves changes made to the container’s file system and does not save changes to the container’s networking or storage. If you need to save changes to these areas, use other Docker commands, such as docker network or docker volume.

Docker Configuration Overview for Ubuntu

This command starts the Docker service. To have Docker start automatically at boot, use systemctl enable docker.service. To stop Docker, use systemctl stop docker.service. To prevent Docker from starting at boot, use systemctl disable docker.service.

Useful Links

Here are some valuable links related to using Docker:

  • Docker Official Website: Visit the official website for information about the container platform, features, and download options.
  • Docker Support: Access support resources for help with Docker, including documentation and community assistance.
  • Docker Documentation: Explore comprehensive documentation for detailed guides on installing, configuring, and using Docker.
  • Docker Training Resources: Access training resources and tutorials to learn how to use Docker effectively.

Conclusion

Getting Docker up and running on Ubuntu opens up a world of possibilities for experimenting, developing, and deploying applications with ease. Whether you’re building your first container or managing complex projects, Docker gives you the flexibility and control that modern Linux users appreciate. If you run into any issues or want to dive deeper, the Docker community and documentation are fantastic resources. Happy containerizing!

Leave a Comment