Docker Run Ubuntu

2/12/2022by admin
  1. Docker Run Ubuntu 20.04 Image
  2. Docker Run Ubuntu Container

Docker is an application that simplifies the process of managing application processes in containers. Containers let you run your applications in resource-isolated processes. Containers are similar to Virtual Machines, but are much less resource-intensive, as they only need the absolute minimum to run a particular application, and does not require installation of a separate Operating System.

Let’s look at how we can install and run the Docker on our Ubuntu 18.04 from the Command Line, in a few steps.

Step 1: Retrieve and add the GPG Public Keys

Docker is not, really, for running an OS. It is for running a single process in a contained (isolated) environment. The container uses the same kernel as what is on the host. When you create a Docker image with FROM ubuntu you are starting your image with some pre-created layers that brings in some parts of a standard file system and packages. Oct 04, 2019 Docker is not, really, for running an OS. It is for running a single process in a contained (isolated) environment. The container uses the same kernel as what is on the host. When you create a Docker image with FROM ubuntu you are starting your image with some pre-created layers that brings in some parts of a standard file system and packages.

Again, similar to other tutorials regarding package installation on Linux, we always need to get the public key for that package so that we know that it is valid. We can then download the required data and install the package securely.

So let’s get the GPG public key for Docker, which is available at the link:

To download the key, we will use the wget command from the Terminal.

The downloaded key in my case is called gpg. After the public key is downloaded, add it to the system keys using apt-key.

Step 2: Verify Key Fingerprint

Now, to verify that we have added the proper key, we need to check the fingerprint for Docker’s key.

Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.

You should get an output similar to the below screenshot.

Step 3: Install Required Packages

To set up the Docker repository, our system must have packages which allow us to download files over HTTPS. So, you need the below packages, which can be downloaded using apt or apt-get.

Now that we have the necessary requirements, we can now add the Docker repository to our list of sources.

Step 4: Add the Docker Repository to the Sources List

We need to add the Docker source to our list of sources in the system so that any future updates can occur using that source URL when using apt update.

We need to modify /etc/apt/sources.list.

Use any text editor (like nano, vi, etc) and open /etc/apt/sources.list. I will be using vi editor to edit the file.

Go to the end of the file, and add this line to ensure that we add the repository source link.

Exit the editor, after saving your changes.

Now, we are ready to update the system and install our package!

Step 5: Install Docker on Ubuntu

Now that we have our sources with us, we are ready to install Docker!

Docker Run Ubuntu

It’s simple. First, we update our system and other critical packages. Then, we fetch and install the latest version of Docker from the source. The version that we will be installing is the Docker Community Edition (docker-ce), so we then install that using apt install.

Now, if there aren’t any errors, we will return to our shell prompt, and our install has completed successfully!

Step 6: Verify Docker Installation

Docker Run Ubuntu 20.04 Image

If Docker was installed correctly, it would have then automatically started a Docker daemon process. So, we need to use the systemctl command and check if the docker service has started or not.

If the command works as expected, then you will see an output similar to mine, indicating that the docker service is active, which means that our installation was indeed successful!

Now, let us now look at how we can configure Docker and run a Docker Container.

Step 7: Execute Docker Commands without sudo

By default, the docker command can only be run by the root user or by a user in the docker group, which is automatically created during Docker’s installation process. If you attempt to run the docker command without prefixing it with sudo or without being in the docker group, you’ll get an output like this:

To avoid typing sudo for every docker command, add your username to the docker group.

Now, log out and log in again to apply for your user membership successfully on the docker group. Now, there is no need to prefix any docker command using sudo!

Step 8: Run a Docker Container

A Docker Container, being similar to a Virtual Machine, also needs an image to work on. There are various images hosted on Docker Hub, Docker’s official website for hosting images. Any image you need can be fetched from this website.

Let us now try to run a simple hello-world Docker Container, which prints ‘Hello World’ and exits.

1. Fetch the image

To pull the corresponding Docker image, use:

Docker Run Ubuntu

2. Run the container

Now we have the image, using which we can run the Docker container.

To check if we actually have the image, let us list all our Docker images on the system, using docker images.

We have the image hello-world in our system, as expected. Now, let us run the container.

Simply execute:

Now, this container will run and print Hello World, before terminating.

We have thus completed running our first Docker Container!

Similarly, we can fetch and run other interactive Docker Containers as well, thus showing the ease of usage of docker. To illustrate this, let us show one more example: Installing a Debian image!!

Running a Docker Debian Container

We use the same commands as earlier, using docker pull debian, to download the image.

Now, to run this container, since this needs an interactive terminal session, we use:

The -it option specifies an interactive terminal session (Combining the -i and the -t options)

As you can see, we are using a Debian system inside an Ubuntu OS, without the number of resources that a Virtual Machine uses. The image size is itself only 114 MB. Amazing, isn’t it?

Conclusion

In this tutorial, we showed you how we can install Docker on Ubuntu 18.04 from the Terminal, and how we can fetch images and run Docker containers using the docker command. I hope this tutorial serves you well and clears any doubts regarding Docker installation or running a Docker container on Ubuntu.

Docker is a free, open-source and cross-platform containerization tool that helps you to deploy and run the application in an isolated environment. Docker has become one of the most important parts of modern software development and DevOps pipelines.

In this tutorial, you will learn –

Prerequisites

  • A server running Ubuntu 20.04.
  • A root password is configured on your server.

Install Docker on Ubuntu

There are two ways to install Docker on Ubuntu 20.04. You can install Docker from the Ubuntu default repository or from the Docker’s official repository. The latest version of Docker is not included in the Ubuntu default repository. So it is always recommended to install Docker from Docker’s official repository.

In this section, we will show you how to install Docker on Ubuntu from Docker official repository.

Step 1 – Install Required Dependencies

First, you will need to install some prerequisite packages in your system. You can install them by running the following command:

Step 2 – Add Docker Official Repository

Next, you will need to import the GPG key and add the Docker repository to your system.

First, import the GPG key with the following command:

Next, add the Docker official repository using the following command:

Once the repository is added, update the repository cache with the following command:

Step 3 – Install Docker Ubuntu

Once the repository is updated, you can install the Docker on Ubuntu by running the following command:

This command will install the latest version of Docker from the Docker official repository.

Step 4 – Verify Docker Version

Once the Docker has been installed, you can verify the installed version of Docker with the following command:

Docker Run Ubuntu Container

You should get the following output:

Step 5 – Install a Specific Docker Version

If you want to install a specific Docker version, first list all available Docker versions using the following command:

You should see all the Docker version in the following output:

Now, install the specific version of Docker by adding =version after package name as shown below:

Step 6 – Manage Docker Services

To start the Docker service, run the following command:

To enable the Docker service at system reboot, run the following command:

To check the status of Docker service, run the following command:

Run Docker Commands Without Sudo

By default, Docker can be run as a root user or with Sudo privileges. If you want to run Docker commands as a non-root user then you will need to add your user to the docker group.

You can add your user to the docker group using the following command:

Now, your user can run Docker commands without specifying sudo.

Docker Basic Commands

You can list all commands available in Docker with the following command:

You should get a list of all commands in the following output:

To see system-wide information about Docker, run the following command:

Output:

How to Use Docker

At this point, Docker is installed and running in your system. Next, you will need to check whether you can access and download the image from DockerHub.

Let’s download the hello-world image from the DockerHub using the docker pull command:

This will download or pull the hello-world image from the DockerHub repository as shown below:

Next, verify the downloaded image with the following command :

You should see your downloaded image in the following output:

Next, run a container from the hello-world image with the following command:

You should see the following output:

Docker run ubuntu/latest

How to Uninstall Docker

If you want to uninstallDocker from your system. It is recommended to remove all Docker containers, images and volumes from your system.

Run

First, stop all running containers with the following command:

Once all containers are stopped, remove them with the following command:

Next, remove all Docker images with the following command:

Next, remove all docker objects with the following command:

Finally, remove the Docker package with all dependencies by running the following command:

Conclusion

In the above post, you learned how to install and use Docker on Ubuntu 20.04. To learn more about Docker, check out my all Docker tutorial.

Comments are closed.