Ubuntu

How to Install and Use Docker Containers on Ubuntu 22.04

How to Install and Use Docker Containers on Ubuntu 22.04

With Docker, you can build applications, test the applications, and then deploy the applications easily and efficiently. The docker image executes code in a container. The code is encapsulated in a container along with packages and libraries for the code to run in any environment. With dockers in Ubuntu, new software versions and features can be added to production easily and efficiently.

This article explains how you can install and use Docker Containers on Ubuntu.

Pre-requisites

  • 4GB RAM for Virtual Machines
  • 64-bit Kernel
  • GNOME Desktop Environment

How to Install and Use Docker Containers on Ubuntu 22.04

Docker containers can be used on Ubuntu for deploying, scaling, and testing applications. With the terminal in Ubuntu, Docker can easily be installed by following the steps explained below.

Step 1: Update & Upgrade Existing Packages

Use the Update and Upgrade Commands to make your Ubuntu system Up-to-date with the latest packages. The update command will check for available updates:

sudo apt-get update

To upgrade the packages, the command used is:

sudo apt-get upgrade

Step 2: Download Docker

Before installing Docker, ensure the “ca-certificates”, “curl”, and “software-properties-common” packages are already installed using the command:

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

Now, add the Docker GPG Key to your system. The GPG Key will ensure the authenticity of the Docker Packages when being installed.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

The command line moves to the next line indicating the GPG Key was added:

After adding the GPG Key, add the Docker Repository using the command:

echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

The command line moves to the next line indicating the repository was added:

Update the Ubuntu system again to add the changes:

sudo apt update

Once the system updates, verify the available Docker Versions using the command:

apt-cache policy docker-ce

As you can see the Docker is not installed yet:

Now install Docker using the command:

sudo apt install docker-ce

Confirmation will be asked during the installation, Continue by entering “Y”:

Once downloaded, check if the Docker is running using the command:

sudo systemctl status docker

You will see the Docker is active and is Running:

Now, to view information about Docker installed on your Ubuntu system, use the command:

docker info

This will display the version and additional information about the Docker installed on your Ubuntu system:

Step 3: Using Docker Images

Docker containers are made of Docker Images and you can use Docker by running commands with arguments having the syntax:

docker <<strong>option</strong>> <<strong>command</strong>> <<strong>arguments</strong>>

The Docker Images are available on Docker Hub and by using the “search” command with docker, images can be searched on the Docker Hub. In our case, to search for “ubuntu” related images, the command used is:

sudo docker search ubuntu

The Official Column with “[OK]” means the images are made officially by the company:

The “ubuntu” image is present and can now be downloaded using the “pull” command:

sudo docker pull ubuntu

This will download the “ubuntu” image from the Terminal:

Once downloaded, the image will be extracted and can be verified using the command:

sudo docker images

The images downloaded and extracted will be shown, in our case, “ubuntu”:

Step 4: Running Docker Container

To run a Docker Container, the “run” command is used and the container should be accessed before running it. The “i” and “t” switches in combination provide us the ability to access containers. Considering our previous steps where we installed the “ubuntu” docker image, can be used as a container. To use the “ubuntu” image as a container, the command used is:

sudo docker run -it ubuntu

The command line will change to the container ID:

The “c9a64 a410738” is the Container ID of the “ubuntu” container. Inside the container, any command can be executed as well and any program can be installed. Before installing any packages inside the container, ensure to update the container using the command:

apt update

To install “vlc” inside the container, the command used will be:

apt install vlc

This will start downloading “vlc”:

Once downloaded and installed, the command prompt or terminal will inform the successful installation:

To exit from the Container, use the “exit” command in the command line:

exit

This will exit the container and the command line will confirm by showing the regular user in the terminal:

Bonus Step: Managing Docker Containers

To view the active and inactive docker containers running on your Ubuntu system, use the command:

sudo docker ps -a

All containers are displayed in the CLI, the status column displays if the container is active or inactive:

This is how you can install, create, and manage a Docket Container on Ubuntu.

Conclusion

Docker Containers can deliver efficient software in Ubuntu and can be installed by adding the Docker GPG Key, adding the Docker Repository, and then installing the Docker Engine. Once installed the status can be checked and with the “run” command, Containers can be used. This article explained the installation and creation of a Docker Image Container.

Similar Posts