Introduction
A programme called Docker makes managing application processes in containers easier. Your programmes can be executed in resource-isolated processes using containers. Containers are similar to virtual machines but are more mobile, resource-friendly, and dependent on the host operating system.
In this blog, we’ll learn How To Install Docker on Ubuntu.
We’ll perform a step-by-step Docker installation guide for Ubuntu 20.04.
Installing and using Docker Community Edition (CE) on Ubuntu 20.04 is covered in this guide. You will set up Docker, use containers and images, and publish an image to a Docker Repository.
Prerequisites
You will require the following in order to continue this tutorial:
One Ubuntu 20.04 server set up using the initial server setup instructions for Ubuntu 20.04, with a sudo non-root user and a firewall.
If you want to generate your own images and publish them to Docker Hub, as detailed in Steps 7 and 8, you'll need a Docker Hub account.
Step 1: Install Docker
It's possible that the Docker installation package included in the official Ubuntu repository is out of date. We'll install Docker from the official Docker repository to make sure we have the most up-to-date version. To do that, we will first create a new package source, then install the package after adding the GPG key from Docker to confirm the downloads are legitimate.
Update your current list of packages first:
Install the following prerequisites to enable apt to use packages through HTTPS:
Then, add the official Docker repository's GPG key to your system:
APT sources should include the Docker repository:
Additionally, this will add the Docker packages from the recently added repository to our package database.
Verify that you are about to install from the Docker repository rather than the standard Ubuntu repository:
The result will look like this, albeit Docker's version number can be different:
Note that docker-ce is not installed, but Ubuntu 20.04 (jammy)'s Docker repository is the installation candidate.
Install Docker lastly:
The daemon should now be running, Docker should be installed, and the process should be set to launch upon startup. Check that it’s running
To verify that the service is operational and operating, the output should resemble the following:
These days, installing Docker also includes the Docker client, in addition to the Docker service (daemon). Later in this lesson, we'll examine the docker command's use.
Step 2: Optionally, run Docker command without sudo
By default, only the root user and members of the docker group—which is automatically formed during Docker's installation process—can execute the docker command. You will see a result similar to this if you attempt to run the docker command without using the sudo prefix or while not a member of the docker group:
Add your account to the docker group to avoid entering sudo each time you execute the docker command:
Log out of the server and back in again to apply the updated group membership, or enter the following:
To proceed, you will be required to enter your user's password.
By typing: Verify that your user has been added to the Docker group.
In order to specifically add a user to the docker group as whom you are not currently logged in, use the following syntax:
The remainder of this article will presume that the docker command is being executed by a user who belongs to the docker group. Please omit the sudo prefix if you don't want to.
Next, let's examine the docker command.
Step 3: Utilizing the Docker Command
After each command and set of parameters, Docker is utilized by providing it a sequence of arguments. The syntax is as follows:
Type: to display all of the possible subcommands.
docker
The full list of accessible subcommands as of Docker 19 includes:
Type: to examine the options available to a particular command.
docker docker-subcommand --help
Use: To display Docker system-wide information.
docker info
Investigate a few of these commands. We'll begin with using photos.
Step 4 — Working with Docker Images
Docker containers are made using Docker images. By default, Docker gets these images from Docker Hub, a Docker file registry managed by Docker, the company in charge of the Docker project. Since anybody may submit their Docker images there, the bulk of the software and Linux distributions you'll need have images hosted on Docker Hub.
To check if you can access the Docker Hub and download images, type.
docker run hello-world
The outcome will demonstrate that Docker is operating correctly:
Docker downloaded the hello-world image from the default repository, Docker Hub, after first being unable to find it locally. The image was downloaded, converted into a container by Docker, which then executed the software and showed the message.
You may search the Docker Hub for images using the docker command and the search subcommand. To find the Ubuntu image, for instance, type the following:
docker search ubuntu
The script will search Docker Hub using the search phrase supplied and return a list of all images with that name. In this case, the outcome will look like this:
A depiction shown as OK in the OFFICIAL column has been developed and approved by the company that is supporting the campaign. Once you have found the image you wish to use, you can download it to your computer by using pull subcommand.
Use the following command to download the official Ubuntu image to your computer:
The following output will appear:
Once the image is downloaded, you may start a container utilizing the downloaded image by using the run subcommand. If an image is not downloaded when calling docker using the run subcommand, the Docker client will download the image before launching a container using it, as you saw with the hello-world example.
To view the images that has been downloaded on your computer, type.
The outcome will look like this:
As you'll see later in this article, the images you use to run containers may be modified and utilized to build new images. Then, these fresh images may be "pushed" (technically) to Docker Hub or other Docker registries.
Let's examine running containers in more detail.
Step 5 — Running a Docker Container
One example of a container that runs and terminates after issuing a test message is the hello-world container that you ran in the previous step. Containers can be interactive and much more helpful than that. After all, they are comparable to virtual machines but use fewer resources.
Let's run a container using the most recent image of Ubuntu as an example. You may access the container interactively by using the -i and -t options together:
You should now be operating within the container, thus your command prompt should alter to reflect this and take the following form:
In the command prompt, take note of the container id. The value in this instance is d9b100f2f636. Later, when you wish to delete the container, you will need the container ID to locate it.
Any command may now be executed inside the container. Let's, for illustration, update the package database located within the container. Since you are running commands within the container as the root user, you don't need to prefix them with sudo:
Install any programme in it after that. Installing Node.js is next:
Node.js is installed in the container using this method from the official Ubuntu repository. Check sure Node.js is installed when the installation is complete:
The version number will be shown in your terminal:
Any modifications you make inside the container only affect that container.
Enter the word exit at the prompt to leave the container.
Next, let's examine how to manage the containers on our system.
Step 6 — Managing Docker Containers
You'll have a lot of active (running) and idle containers on your PC after using Docker for a while. Use: to display only the active ones.
docker ps
You'll observe results resembling these:
You launched two containers in this tutorial, one from the hello-world image and the other from the ubuntu image. Despite not being active anymore, both containers are still there on your machine.
Docker ps with the -a switch may be used to see all containers, both active and inactive:
docker ps -a
You’ll see output similar to this:
Give it the -l option to see the most recent container you've created:
To restart a stalled container, type docker start followed by the container ID or name. Let's start the Ubuntu-running container with ID 1c08a7a0d0e4:
The container will start, and Docker may be used to verify its status. ps:
Use docker stop, followed by the container ID or name, to terminate a running container. This time, we'll use the name quizzical_mcnulty that Docker gave the container:
Once you've made the decision that you no longer require a container, delete it using the docker rm command, once more specifying the container ID or name. To delete the container connected to the hello-world image, use the docker ps -a command to locate its container ID or name.
Using the --name switch, you may launch a new container and give it a name. Additionally, you may make a container that detaches itself when terminated by using the --rm flag. For further details on these and other settings, see the docker run help command.
Step 7 — Committing Changes in Container to Docker Image
You can create, change, and remove files when a Docker image is running, just like you do with a virtual machine. Only that container will be affected by the modifications you make. You may start and stop it, but if you use the docker rm command to remove it, the modifications will be permanently lost.
You may learn how to create a new Docker image by following the instructions in this section.
You now have a container operating off an image, but the container is different from the image you used to build it after installing Node.js inside the Ubuntu container. But you might wish to utilise this Node.js container again in the future as the foundation for fresh images.
Use the command below to commit the modifications to a fresh Docker image instance.
The -m switch lets you and other users understand what modifications you made, while the -a switch identifies the author. The container_id is the one you took note of when you opened the interactive Docker session earlier in the lesson. The repository is typically your Docker Hub account username unless you set up multiple repositories.
For instance, the command would be as follows for the user sammy and the container ID of d9b100f2f636:
The new image is saved locally on your computer when you commit an image. You'll discover how to upload an image to a Docker registry like Docker Hub later on in this course so that others may access it.
Relisting the Docker images will display both the new image and the original one from which it was derived:
docker images
You'll see the following output:
In this case, the new image, ubuntu-nodejs, was created by deriving it from the Ubuntu image that was previously available on Docker Hub. The size discrepancy reflects the modifications. The addition of NodeJS was the difference in this instance. Therefore, you can just use the modified image the next time you need to launch a container on Ubuntu with NodeJS already installed.
You may automate the software installation process in a new image by building images from a Dockerfile. That, however, is not covered in this course.
Let's now distribute the modified image to others so they may use it to make containers.
Step 8 — Pushing Images to Docker Repository
It makes sense to share your newly created image with a small group of your friends, the entire globe on Docker Hub, or another Docker registry that you have access to after producing it from an existing image. You need to sign in to Docker Hub or another Docker registry in order to push an image there.
You may learn how to upload a Docker image to Docker Hub in this section. Visit How To Set Up a Private Docker Registry on Ubuntu 18.04 to discover how to set up your own private Docker registry.
Log into Docker Hub first before pushing your image.
When asked, enter your Docker Hub password to log in. If you used the right password, authentication ought to be successful.
Note:You must tag your image with your registry username if your Docker registry username differs from the local username you used to generate the image. You would enter the following for the last step's example:
Then you may push your own image using:
To push the ubuntu-nodejs image to the sammy repository, the command would be:
As the images are uploaded, the procedure may take some time to finish, but when it is, the result will be as follows:
An image should appear on your account's dashboard after being submitted to a registry, as shown in the screenshot below.
Docker Hub has posted a new Docker image.
If a push attempt encounters this kind of problem, you probably did not log in:
Retry the push attempt after logging in with the docker login. Following that, make sure it is listed on your Docker Hub repository page.
You can now pull the image to a new computer and use it to launch a new container by using the command docker pull sammy/ubuntu-nodejs.
Conclusion
In conclusion, this guide provides a step-by-step process for installing Docker and using it on Ubuntu 20.04. Docker is a powerful tool for managing application processes in containers, offering resource isolation and mobility. The guide starts with the installation of Docker from the official Docker repository to ensure the latest version is obtained. It covers prerequisites, such as having an Ubuntu 20.04 server with a sudo non-root user and firewall. The guide then proceeds to explain how to use Docker, including working with Docker images, running containers, managing containers, committing changes to images, and pushing images to Docker repositories like Docker Hub. Each step is explained in detail, providing the necessary commands and explanations to perform the tasks successfully. By following this guide, users can effectively install Docker, work with containers and images, and publish their own Docker images for others to use. Docker's flexibility and efficiency make it a valuable tool for developers and system administrators working with containerized applications.

0 Comments