Docker is a virtualization platform that automates the process of application deployment. Containers and Images in Docker are used for this purpose. Docker containers offer a lightweight and portable environment for the deployment of applications. These containers are based on Docker images which assists in packaging an application with all of its runtime dependencies inside a Linux container.

You can use Docker to automate the application deployment in your organization; it will boost employee productivity and create a more agile development environment to manage resources better. That’s why we are here to demonstrate how to install and configure Docker on the Ubuntu system. So, let’s start!

How to install Docker on Ubuntu

First of all, you have to update your system repositories:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-1.png" data-lazy- height="304" src="data:image/svg xml,” width=”737″>

Now, we will install some essential packages required for installing Docker on the system:

$ sudo apt install apt-transport-https ca-certificates curl software-properties-common

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-2.png" data-lazy- height="477" src="data:image/svg xml,” width=”736″>

Enter “y/Y” to permits the installation process to continue:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-3.png" data-lazy- height="478" src="data:image/svg xml,” width=”739″>

Utilize the below-given command for adding the GPG key of the Docker repository:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add

If the execution of the command shows “OK“, it means the GPG key is successfully added to our system:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-4.png" data-lazy- height="307" src="data:image/svg xml,” width=”739″>

Next, we will add the Docker repository to Ubuntu system:

$ sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-5.png" data-lazy- height="421" src="data:image/svg xml,” width=”720″>

Again, update the repositories of your Ubuntu system:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-6.png" data-lazy- height="313" src="data:image/svg xml,” width=”721″>

Now, execute the “apt-cache” command to ensure that we are going to install the application from the Docker repository:

$ apt-cache policy docker-ce

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-7.png" data-lazy- height="482" src="data:image/svg xml,” width=”755″>

Install Docker on your system by utilizing this command in your terminal:

$ sudo apt install docker-ce

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-8.png" data-lazy- height="476" src="data:image/svg xml,” width=”755″>

Enter “y/Y” to permit the Docker installation process to continue:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-9.png" data-lazy- height="484" src="data:image/svg xml,” width=”753″>

After the successful installation of Docker, check out its status on your system:

$ sudo systemctl status docker

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-10.png" data-lazy- height="476" src="data:image/svg xml,” width=”755″>

How to add a user to Docker group on Ubuntu

Command related to Docker requires you to be a “root” user or a “docker” group member. If you are not working as a root user, you can use the “usermod” command to add the current user to the “docker” group:

$ sudo usermod -aG docker ${USER}

Here, the “-aG” option will append the current user “${User}” to the “docker” group:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-11.png" data-lazy- height="314" src="data:image/svg xml,” width=”752″>

Now, log out and log in back to the system for applying the new “docker” membership:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-12.png" data-lazy- height="305" src="data:image/svg xml,” width=”752″>

If you want to add a user that is not logged in to the system, then execute the below-given command by adding its username:

$ sudo usermod -aG docker username

How to use docker command on Ubuntu

To view information about docker such as its syntax, option, commands, type “docker” in your terminal:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-13.png" data-lazy- height="478" src="data:image/svg xml,” width=”759″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-14.png" data-lazy- height="478" src="data:image/svg xml,” width=”754″>

Also, execute the below-given command for knowing the system-wide information related to Docker:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-15.png" data-lazy- height="479" src="data:image/svg xml,” width=”755″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-16.png" data-lazy- height="472" src="data:image/svg xml,” width=”753″>

How to work with Docker images on Ubuntu

Docker images are utilized for creating Docker containers. You can “push” these images to your Docker repository, whereas Docker “pull” these images from the Docker Hub by default. Docker Hub permits anyone to host their images; thus, most Linux distros and related images are hosted in the Docker Hub.

First of all, we will check that we have access to download images from Docker Hub. For this, execute the sample command given below:

$ sudo docker run hello-world

The output is declaring that Docker is enabled to find our specified “hello-world” image locally. Then, the Docker application utilized Docker Hub for downloading it. Then Docker creates a new container from the “hello-world” image. Lastly, the application present in the container showed output on our Ubuntu terminal:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-17.png" data-lazy- height="567" src="data:image/svg xml,” width=”739″>

How to search for a Docker image on Ubuntu

The “docker search” command is utilized in the Ubuntu terminal for searching any Docker image available on the Docker Hub. For instance, we will search for the “ubuntu” image on the Docker Hub but if you want to search for another Docker image, then specify its name where we added “ubuntu”:

The output of command mentioned above will list out all the Docker images whose names successfully matched with our search string “ubuntu”:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-18.png" data-lazy- height="478" src="data:image/svg xml,” width=”854″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-19.png" data-lazy- height="480" src="data:image/svg xml,” width=”860″>

How to pull a Docker image on Ubuntu

The “docker pull” command is used to download a Docker image to your Ubuntu system. For instance, we have searched for the “ubuntu” image, and we found out that it is available on Docker Hub. Now, to download the “ubuntu” image, we will execute the “docker pull” command in the following way:

The output signifies that the “ubuntu” image is successfully downloaded to our system:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-20.png" data-lazy- height="302" src="data:image/svg xml,” width=”738″>

To verify the existence of the downloaded images on your system, execute this command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-21.png" data-lazy- height="278" src="data:image/svg xml,” width=”739″>

How to run a Docker container on Ubuntu

Now, we will demonstrate how you can run a Docker container with “ubuntu” image. To do so, utilize the below-given command in your terminal:

Here the combination of the “-it” option will permit you to interact with the container using the shell access:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-22.png" data-lazy- height="255" src="data:image/svg xml,” width=”736″>

The output is also displaying our container ID, which is “bdee2efafad“. You should note your container ID because you will later need it to manage this container on your system.

How to update packages inside a Docker container on Ubuntu

If you want the package present in the current Docker container, then execute the “apt update” command in the following way:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-23.png" data-lazy- height="500" src="data:image/svg xml,” width=”825″>

How to install an application in a Docker container on Ubuntu

Docker container also provides you access to install any specified application in it. For instance, in our Docker container, we will install “Node.js”:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-24.png" data-lazy- height="445" src="data:image/svg xml,” width=”703″>

Enter “y/Y” to permit the Node.ls installation process to continue:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-25.png" data-lazy- height="498" src="data:image/svg xml,” width=”706″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-26.png" data-lazy- height="501" src="data:image/svg xml,” width=”703″>

The error-free output indicates that Node.js is successfully installed inside our Docker container. To verify its existence, execute this command:

This command will display the version of the Node.js application installed inside the Docker container:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-27.png" data-lazy- height="282" src="data:image/svg xml,” width=”702″>

How to list Docker containers on Ubuntu

In your Ubuntu terminal, utilize the “docker ps” command for listing the active Docker container on your system:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-28.png" data-lazy- height="310" src="data:image/svg xml,” width=”811″>

You can add the “-a” option in the same command if you want to extract all Docker containers, including the active and inactive ones:

Currently, we do not have any inactive Docker containers; that’s why the output is only showing the information related to active containers:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-29.png" data-lazy- height="328" src="data:image/svg xml,” width=”828″>

To check the latest created Docker container, add the “-l” option to the “docker ps” command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-30.png" data-lazy- height="279" src="data:image/svg xml,” width=”828″>

How to start a Docker container on Ubuntu

The “docker start” command is used to start a Docker container on your system. In this command, we add the container ID of the specific Docker container. For instance, we will start the “ubuntu” Docker container by adding its container ID “3bdee2efafad” in the below-given command:

$ sudo docker start 3bdee2efafad

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-31.png" data-lazy- height="366" src="data:image/svg xml,” width=”708″>

How to stop a Docker container on Ubuntu

In our system, the name of the container “ubuntu” assigned by Docker is “brave_brown”. We will specify this name in the “docker stop” command to stop the running “ubuntu” container:

$ sudo docker stop brave_brown

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-32.png" data-lazy- height="287" src="data:image/svg xml,” width=”708″>

How to commit changes to a new Docker image on Ubuntu

The changes you make in your Docker container are only limited to itself, such as starting and stopping the Docker container, adding files in it, etc. However, once you completely remove the Docker container, all of the applied changes will be lost. Docker offers you the function of committing these changes to a new instance of Docker image. It can be handy in a situation where you want to reuse a container for some other purpose. You can operate by committing changes of a Docker container to a new Docker image with the help of the “docker commit” command.

Now, check out the below-given syntax of “docker commit” command:

$ docker commit -m “description of changes” -a “Author_Name” [Container_ID] [Repository]/[Docker_Image_Name]

Here:

  • -m” option is added to commit the message.
  • -a” is utilized for specifying the name of the author.
  • Author_Name” will be your username.
  • [Container_ID] is the ID of a specific Docker Container for which you want to commit the changes.
  • If you have created any repository on the Docker Hub, then add its name in the [Repository]; otherwise, you will write your Docker username in its place.
  • Lastly, specify the name of the new Docker image in the [Docker_Image_Name].

For instance, for the user “linuxhint” having the container ID “3bdee2efafad” the “docker command” will be:

$ sudo docker commit -m “added Node.js” -a “linuxhint” 3bdee2efafad linuxhint/ubuntu-nodejs

In the commit mentioned above, we have specified that in this Docker container, we have added NodeJS:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-33.png" data-lazy- height="288" src="data:image/svg xml,” width=”745″>

Again, list out the Docker images to ensure that new Docker image “ubuntu-nodejs” is created or not:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-34.png" data-lazy- height="321" src="data:image/svg xml,” width=”747″>

How to push a Docker image to a Docker Hub on Ubuntu

At this point, the changes we committed to a new Docker image are saved on our local system. You may want to share the new Docker image with any friend, team members, or with the whole world on Docker Hub for development purposes. To do so, you should have an account on Docker Hub so that you can push the newly created image to the repository on Docker Hub.

If you already hold a Docker Hub account, then skip this step. In the other case, you can create a new Docker account using the following link.

Enter your Docker username, Email ID, and password for creating a new account:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-35.png" data-lazy- height="632" src="data:image/svg xml,” width=”1297″>

Click on the “Create a repository” option for creating a Docker repository on Docker Hub:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-36.png" data-lazy- height="637" src="data:image/svg xml,” width=”1298″>

Specify the name of the repository and its type of visibility. If you want to make you Docker repository appear in search result then make it “Public”, otherwise keep it “Private”:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-37.png" data-lazy- height="637" src="data:image/svg xml,” width=”1299″>

Here, you can see the information related to the newly created repository:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-38.png" data-lazy- height="630" src="data:image/svg xml,” width=”1299″>

If the username of your local system and your Docker registry name is different, then you have to tag the newly created Docker image with the Docker registry username.

Firstly, log into your Docker hub account using your terminal:

$ sudo docker login docker.io

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-39.png" data-lazy- height="364" src="data:image/svg xml,” width=”811″>

Now, we will utilize the “docker tag” command to tag the “ubuntu-nodejs” images created by “linuxhint” user with the “sharqa” Docker registry name:

$ sudo docker tag linuxhint/ubuntu-nodejs sharqa/ubuntu-nodejs

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-40.png" data-lazy- height="305" src="data:image/svg xml,” width=”813″>

After logging into the Docker account, we will push the newly created Docker image into our Docker repository present on the Docker Hub.

Now, check out the syntax of “docker push” command:

$ sudo docker push [Docker_registry_username]/[Docker_image_name]

In the command mentioned above, specify your Docker registry name, which is “sharqa” in our case, then add a “/” followed by the Docker image name:

$ sudo docker push sharqa/ubuntu-node.js

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-41.png" data-lazy- height="319" src="data:image/svg xml,” width=”811″>

The output is showing that we have successfully pushed into our Docker registry. Verify it by exploring your Docker Hub account:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-42.png" data-lazy- height="620" src="data:image/svg xml,” width=”1293″>

Here you can see the information related to the pushed “ubuntu-nodejs” image:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-43.png" data-lazy- height="453" src="data:image/svg xml,” width=”1293″>

How to remove a Docker container on Ubuntu

You cannot remove a running Docker container from your system by using the “docker rm” command. The first thing you need to do is stop the Docker container with “docker stop” and then utilize the “docker rm” command to remove it from your system.

Now, we will stop the “ubuntu” container from our system:

$ sudo docker stop brave_brown

Here “brave_brown” is the name of the “ubuntu” container assigned by Docker. Write out the below-given “rm” command to remove it:

$ sudo docker rm brave_brown

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-44.png" data-lazy- height="317" src="data:image/svg xml,” width=”737″>

To confirm the removal of the “ubuntu” container, again list out the Docker container by executing the “docker ps” command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/How-to-Install-and-Configure-Docker-on-Ubuntu-45.png" data-lazy- height="297" src="data:image/svg xml,” width=”1015″>

You can see that the “ubuntu” container is nowhere in the list, which means that we have successfully deleted this container.

Conclusion

Developers can use Docker as a virtualization platform to create lightweight, self-contained, and portable application containers that make it simple to develop, test, and deploy applications. This article demonstrated how to install and configure Docker on Ubuntu. We showed you how to create, stop, and remove a container. Moreover, this article also provided the procedure of pulling and pushing the Docker images to Docker Hub.