How to Install Docker on Ubuntu 22.04 or 20.04

Docker is an open-source platform enabling software developers to package and deploy their applications in a portable, lightweight container. The technology was first released in 2013 and has since become a popular choice for developers due to its ability to simplify application deployment and improve the scalability of their applications. Docker containers are designed to be lightweight, efficient, and easily portable, making them ideal for deploying applications across different environments.

Here are some key features of Docker:

  • Containerization: Docker allows developers to package their applications and dependencies into a single container. Containers are isolated from the host system and other containers, ensuring applications run consistently across different environments.
  • Portability: Docker containers can be easily moved between different environments, such as development, testing, and production. This makes it easier for developers to deploy and manage their applications, regardless of the underlying infrastructure.
  • Efficiency: Docker containers are lightweight and consume minimal resources, making them ideal for deploying and cost-effectively scaling applications.
  • Security: Docker containers are designed with security in mind, with features such as isolated filesystems and restricted network access to help protect applications and data.
  • Compatibility: Docker is compatible with a wide range of programming languages, frameworks, and tools, making it easy for developers to use their preferred tools and technologies.

In contrast to traditional virtualization technologies, which rely on a hypervisor to manage virtual machines, Docker uses containerization to package applications and their dependencies into isolated containers. This approach allows developers to create more lightweight and efficient deployments and make it easier to manage and scale their applications.

To start with Docker on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa, you can use the APT package manager with Docker’s APT repository to install the latest updated version. The guide will demonstrate how to do this step-by-step.

Step 1: Pre-installation

Before installing Docker on Ubuntu, it’s important to remove any older versions of Docker to prevent conflicts and ensure a smooth installation. To do this, you can run the following command to remove any older versions of Docker that may be installed on your system:

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

If none of these packages are installed, you’ll receive a message from apt indicating that there’s nothing to remove.

It’s important to note that when you uninstall Docker, any images, containers, volumes, and networks stored in /var/lib/docker/ are not automatically removed. If you want to start with a clean installation and remove any existing data, you can 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.

Step 2: Import Docker Repository

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

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 > /dev/null

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

Step 3: Install Docker

Before installing Docker, it’s a good practice to update your system to ensure that the newly imported Docker repository is reflected in your sources lists. You can do this by running the following command:

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 along with some additional plugins that you may find useful.

After the installation is complete, you can run the following command to confirm that Docker is working:

sudo docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints a confirmation message and exits.

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

Step 4: Manage Docker with systemd

Systemd is a system and service manager that provides a simple way to manage processes and services on Ubuntu. When Docker is installed on Ubuntu, it creates a systemd unit that manages the Docker service. This unit can be controlled using a variety of systemd commands, which provide a convenient way to start, stop, and manage Docker containers and images.

Here are some of the most commonly used systemd commands for managing Docker on Ubuntu:

systemctl start docker.service

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

systemctl stop docker.service

This command stops the Docker service and prevents it from running on the system boot.

systemctl restart docker.service

This command restarts the Docker service.

systemctl status docker.service

This 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.

Step 5: Docker Configuration Setup

Manage Docker as a non-root user

When running Docker, it is not recommended to use the root user as this can cause security risks and accidental changes to the host system. Therefore, it is better to manage Docker as a non-root user.

To create a new user and add it to the Docker group, the following commands can be used:

sudo useradd -m dockeruser
sudo usermod -aG docker dockeruser

Furthermore, you can add your current username to the Docker group. For instance, if your username is “joshua”, you can execute the following command:

sudo usermod -aG docker joshua

These commands will create a new user called dockeruser and add them to the Docker group. After adding the user to the Docker group, the user should log out and back in to apply the changes.

Note you may need to restart if logging out does not work.

To verify that the user can run Docker commands, the following command can be used:

docker ps

This command will display a running container list if Docker is installed correctly.

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, Docker is configured to use the syslog driver and send logs to a remote syslog server. Replace logs.example.com with the address of your syslog server.

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

When working with Docker, it’s essential to 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 is used to list all running containers. This command provides information about each container, such as the container ID, image name, and status.

docker ps

This command will display a list of all running containers.

docker images

The docker images command is used to list all available images. This command provides information about each image, such as the image ID, repository, and tag.

docker images

This command will display a list of all available images.

docker build

The docker build command is used to build a new image from a Dockerfile. A Dockerfile is a script that contains instructions for building an image.

docker build -t myimage:latest .

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

docker stop

The docker stop command is used to stop a running container. 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.

docker rm

The docker rm command is used to remove a container. 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.

docker rmi

The docker rmi command is used to remove an image. For example, to remove an image with the ID 1234567, you can use the following command:

docker rmi 1234567

This command will remove the image with the ID 1234567.

docker network

The docker network command is used to manage Docker networks. This command provides options for creating, listing, and removing networks.

docker network create mynetwork

This command will create a new network called mynetwork.

docker volume

The docker volume command is used to manage Docker volumes. This command provides options for creating, listing, and removing volumes.

docker volume create myvolume

This command will create a new volume called myvolume.

Running a Docker Container

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 creates a new instance of the image as a container. Each container is isolated from other containers and has its own file system, networking, and resources.

After the container runs, you can interact with it through a shell or a command prompt. For example, to open a shell inside the container, you can use the following command:

docker run -it ubuntu /bin/bash

Managing Docker Containers

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, any changes made to the container will be lost. If you want to save the changes made to a container as a new image, you can use the docker commit command to create a new image from the container.

Committing Changes in a Container to a Docker Image

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

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 the changes made to the container’s file system and does not save any 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.

Conclusion

In conclusion, this guide has provided a comprehensive overview of installing and managing Docker on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa. By following the steps outlined in this guide, you should now have a good understanding of how to use Docker to create, run, and manage containers on your Ubuntu system.

It’s important to remember that Docker is a powerful tool that requires careful configuration and management practices to ensure optimal performance and security. With this in mind, it’s recommended that you continue to explore the many features and capabilities of Docker and stay up-to-date with best practices for managing containers on Ubuntu. By doing so, you can take full advantage of Docker’s many benefits and simplify your application deployment process.

Additional Resources and Links for Installing Docker CE on Ubuntu

To further enhance your understanding of Docker CE and its installation on Ubuntu, the following official resources and documentation are provided:

  • Docker Official Website: Visit the official website to explore Docker’s features, updates, and news directly from the developers.
  • Docker GitHub Repository: Access the source code, contribute, or report issues on the Docker CE GitHub repository.
  • Ubuntu Official Documentation: Gain comprehensive knowledge about Ubuntu, its features, and system administration through the official documentation.
  • Ubuntu Wiki: The Ubuntu Wiki offers a wealth of information on various topics, including troubleshooting, community support, and development.
  • Docker Documentation: Consult the Docker documentation to understand its usage, options, and configuration in depth.

Share to...