How to Install Docker on Ubuntu

Installing Docker on Ubuntu is a relatively easy process. This step-by-step guide provides sample command lines, tips, and more.

Brien Posey

September 20, 2022

4 Min Read
How to Install Docker on Ubuntu

Docker is a container platform that you can use to create containers on top of Ubuntu or other Linux platforms. For those who might not have worked with containers before, they are similar to virtual machines. However, containers consume far fewer resources because they can share a common base image.

In this article, I will show you how to install Docker and create an interactive Ubuntu container on an Ubuntu server.

How To Install Docker on Ubuntu Using the Command Line

Installing Docker on Ubuntu is a relatively easy process. As with other application installation processes, it uses the Application Package Installer (APT).

Here is the command used to install Docker:

Sudo apt install

You can see what this process looks like in Figure 1. Notice that Docker will consume just under 300 megabytes of disk space. However, keep in mind that you will need additional storage space for image files and for any containers that you may create.

screenshot shows Sudo apt install command and result

Docker 1

Figure 1. The Docker installation process leverages the Application Package Installer.

Once Docker has been installed, the next thing to do is install its dependencies. These dependencies exist in the form of a snap bundle. A snap bundle is essentially a collection of applications and/or dependencies that are designed to work with a variety of Linux distributions, including Ubuntu.

Related:How To Create Ubuntu Virtual Machines the Easy Way

To install the Docker dependencies, use this command:

Sudo snap install docker

Docker Ubuntu screenshot

Docker 2

Figure 2. The Docker dependencies must be installed from a snap bundle.

Testing Your Docker Installation

At this point, Docker should be installed and functional. It’s a good idea to try running the “Hello World” image just to ensure Docker works properly.

To do so, enter the following command:

Sudo docker-run hello-world

When you run this command, Ubuntu should display a message that says, “Hello from Docker”, which indicates that Docker works properly. However, since Docker was just recently installed, the Hello World image likely will not exist on your machine. This means that you will initially see an error message telling you that the image does not exist locally. After that, you should get a message that Docker has pulled the Hello World image from the library. You should then see the “Hello from Docker” message. You can see what this looks like in Figure 3.

Screenshot shows use of the Hello World image to test Docker

Docker 3

Figure 3. You can use the Hello World image to test Docker.

Modifying the Docker Group

You might have noticed in the previous figure that I had to type sudo prior to entering the Docker Run command. Sudo tells Linux that a command must run with elevated privileges. However, most of the Docker tutorials that exist online show the Docker command being used without the aid of sudo.

Related:How to Undelete Files in Ubuntu: 3 Tools for Linux Data Recovery

If you would prefer not to enter sudo each time you execute a Docker command, you must add your username to the Docker group. Here is a command for adding the account that you are currently using to the Docker group:

Sudo usermod -aG docker ${USER}

Next, you will either need to log out and log back in or run the following command to make the change take effect:

Su - ${USER}

If you type the word "groups," you should see confirmation that you are now a member of the Docker group.

Working With Docker Images

Before you can create a container, you must install a base image. Assuming that the image will be based on Ubuntu, you should begin by searching for Ubuntu images.

Here is the command for searching for Ubuntu images:

Docker search ubuntu

Once you find an image that you want to use, enter the Docker pull command, followed by the image name. In Figure 4, for example, I found an image named “Ubuntu” and then pulled that image by typing “docker pull ubuntu”.

Screenshot shows the commands docker search ubuntu and docker pull ubuntu

Docker 4

Figure 4. This is how you search for and pull a Docker base image.

How To Use Ubuntu Images in Docker Containers

Now that you have a Docker base image, you can use that image to create an Ubuntu container.

We already created one container using the Hello World image. As you saw in that example, we simply entered the Docker Run command, followed by the name of the image (Hello-World).

While that technique can be used to run a container, you can also make the container interactive (giving you shell access) by specifying the -it switch. Hence, if we want to create an interactive Ubuntu container, this is the command for doing so:

Docker run -it ubuntu

screenshot shows command docker un -it ubuntu

Docker 5

Figure 5. This is how you create an interactive Ubuntu container.

As you can see, Ubuntu makes it simple to install Docker. It is equally as easy to search for and pull base images and then create containers from those images.

About the Author(s)

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like