How to Install Docker on Ubuntu
Docker is a container platform that allows you to build containers on Ubuntu or other Linux platforms. For those who may have never worked with containers, they are like 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 from the command line
Installing Docker on Ubuntu is a relatively simple process. As with other application installation processes, the Application Package Installer (APT) is used.
Here is the command to install Docker:
Sudo apt install docker.io
You can see what this process looks like in Figure 1. Note that Docker consumes just under 300 megabytes of disk space. Note, however, that you will need additional disk space for image files and for any containers you create.
Illustration 1. The Docker installation process uses the application package installer.
Now that Docker has been installed, the next step is to 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 designed to work with a variety of Linux distributions, including Ubuntu.
To install the Docker dependencies, use this command:
Sudo snap install docker
figure 2 The Docker dependencies must be installed from a snap bundle.
Testing your Docker installation
At this point, Docker should be installed and working. It’s a good idea to run the “Hello World” image just to make sure Docker is working properly.
To do this, enter the following command:
Sudo docker-run hello-world
When you run this command, Ubuntu should display a message that says “Hello from Docker”, indicating that Docker is working properly. However, since Docker was only recently installed, the Hello World image will likely not be present on your machine. This means that you will first see an error message that the image does not exist locally. After that, you should get a message that Docker 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.
figure 3 You can use the Hello World image to test Docker.
Changing the docker group
You may have noticed in the previous figure that I had to type sudo before entering the Docker Run command. Sudo tells Linux that a command needs to be run with elevated privileges. However, most of the Docker tutorials available online show using the Docker command without the help of sudo.
If you don’t want to type sudo every time you run a docker command, you need to add your username to the docker group. Here is a command to add the account you are currently using to the docker group:
Sudo usermod -aG docker ${USER}
Next, you must either log out and back in, or run the following command for the change to take effect:
Su - ${USER}
If you type the word “groups” you should see a 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 the image is based on Ubuntu, you should first search for Ubuntu images.
Here is the command to search for Ubuntu images:
Docker search ubuntu
When you find an image you want to use, enter the docker pull command followed by the image name. For example, in Figure 4, I found an image named “Ubuntu” and then pulled that image by typing “docker pull ubuntu”.
figure 4 How to find and drag a Docker base image.
How to use Ubuntu images in Docker containers
Now that you have a base Docker image, you can use that image to create an Ubuntu container.
We’ve already created a container with the Hello World image. As you saw in this example, we just typed the Docker Run command followed by the name of the image (Hello-World).
While this technique can be used to run a container, you can also make the container interactive (and give you shell access) by specifying the -it switch. So if we want to create an interactive Ubuntu container, this is the command for it:
Docker run -it ubuntu
Figure 5. How to create an interactive Ubuntu container.
As you can see, Ubuntu makes it easy to install Docker. It’s just as easy to find and retrieve base images, and then build containers from those images.