12.6 C
Jaipur
Saturday, January 22, 2022

How to Install and Use Podman (Docker Alternative) on Ubuntu 20.04

Must read

Podman is an open-source tool for managing containers, images, volumes, and pods (group of containers). It’s used the libpod library APIs for managing container lifecycles and supports multiple container image formats, including OCI (Open Container Initiative) and Docker images.

Podman is OCI (Open Container Initiative) compliance container engine. It’s compatible with the Docker CLI interface and allows you to run container rootless (running container without root privileges). Podman was released as part of Red Hat Enterprise Linux, designed to be the next generation of Linux container tool with faster experimentation and development of features.

For this tutorial, you will learn how to install Podman on the Ubuntu 20.04 system. You will be installing Podman and learn the basic usages of podman for managing Docker containers, images, and volumes.

Prerequisites

  • An Ubuntu 20.04 server – ensure all packages are the latest version, to get rid of deprecated dependencies and lead you to an error during the installation.
  • A user with root privileges, or user root – for installing new packages and change configuration system-wide.

Installing Podman on Ubuntu 20.04

At first, you will be adding a Third-party repository and key for installing podman on the Ubuntu 20.04 system.

1. Execute the following command to export environment variables on the ‘/etc/os-release‘ file.

. /etc/os-release

2. Add the podman repository with stable release and add the GPG key to your system using the command below.

echo "deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/ /" | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list

curl -L "https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/Release.key" | sudo apt-key add -

3. Update/refresh Ubuntu repositories and upgrade all packages to the latest version.

sudo apt update
sudo apt -y upgrade

4. After that, install podman using the apt command below.

sudo apt install podman

Type ‘y‘ and press ‘Enter‘ to continue the installation.

Installing Podman on Ubuntu 20.04

5. After installation completes, verify the podman version using the following command.

podman version

Below is the similar output you will get.

Check podman version

As can be seen, you’ve installed podman v3.3.1 compiled with Go 1.16 for the Linux architecture amd64 (64-bit).

Creating New User and Run hello-world Container

Podman allows you to run containers under the user without root privileges. For this stage, you will be adding a new user and run the container ‘hello-world’ based on the Docker image.

1. Execute the following command to add a new user ‘johndoe’.

useradd -m -s /bin/bash johndoe
passwd johndoe

Type new password for user ‘johndoe‘.

2. Next, log in as user ‘johndoe’ and run the container based on the Docker image ‘hello-world’.

Log in as user ‘johndoe‘.

su - johndoe

Run new container based on Docker image ‘hello-world‘ using podman command.

podman run hello-world

You will be asked for which container registry you want to use, choose the ‘docker.io’ container registry and you will see a similar output as below.

Podman run hello-world container

3. To verify your container and images on the local machine, execute the command below.

podman ps -a
podman images

You will see a new container is created and the current status is ‘Exited‘. The container is based on the ‘hello-world‘ Docker image.

Podman check container and images

Podman Basic Usage

With this guide, you will learn how to use podman for managing containers, images, and volumes. You will learn the basic command of podman for managing container, and commands you will be using for this guide is 100% same as Docker CLI command.

1. To find container images compatible with the podman, execute the podman command below.

podman search nginx

And you will see a list of container images from the default Docker image registry and Quay images registry.

Choose the container images ‘nginx’ from the Docker registry and download to your local machine using the podman command below.

podman pull nginx:alpine

Choose the container registry ‘docker.io‘ and the download process for the Nginx container image will begin.

Podman downloaad container images

2. To check available container images on your local machine, execute the podman command below.

podman images

And you will see two images – hello-world and nginx images.

Check images on local machine with podmanAdvertisement

3. Next, to start and run a new container based on image ‘nginx:alpine‘, execute the podman command with the option ‘run‘ as below.

podman run -it --rm -d -p 8080:80 --name web nginx:alpine

Some options you must know:

  • -i or –interactive – keep the container STDIN open even if unattached.
  • -t or –-tty – allocates a pseudo-terminal that connects your terminal with the container’s STDIN and STDOUT.
  • –rm – automatically remove the container when the container is exited or stopped.
  • -d – running the container in the background, deattach the container after it’s running.
  • -p 8080:80 – map the port between container and host system. The port ’80’ on the container is mapped to the host system port ‘8080’.
  • –name web – specify the new container name as ‘web’.
  • nginx:alpine – the image we use is ‘nginx:alpine’.

You will see the random string and number of your running container. Verify your running container using the podman command below.

podman ps

You will see the output as below.

Start and run container with podman

The container status is ‘Up‘ and exposes the port TCP port ‘8080‘ on the host machine.

Open your web browser and visit your server IP address with port ‘8080‘ as below.

http://192.168.1.15:8080/

And you will see the default ‘index.html’ age of your nginx container.

Container web based on Nginx image is running

4. Additionally, you can check logs of your container using the ‘logs‘ option as below.

podman logs web

Podman checkc logs

Or you can specify the last number lines of the container log with the ‘–tail‘ option as below.

podman logs --tail 10 web

Podman check latest logs

5. Now you can stop the running container using the podman option ‘stop‘ as below.

podman stop web

Your container ‘web’ will be stopped. Check using the command below.

podman ps
podman ps -a

And you will see similar output as below.

stop container podman

The container ‘web’ is stopped and automatically deleted because you’re using the option ‘–rm’ on your podman command.

6. Next, you will learn how to use the custom volume to change the default ‘index.html’ on the new container.

Create a new directory ‘~/data‘ and the ‘index.html‘ file using the following command.

mkdir -p ~/data/
nano ~/data/index.html

Copy and paste the HTML script below.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Welcome to Container Nginx</title>
</head>
<body>
  <h2>Hello from Nginx container - Managed with Podman</h2>
</body>
</html>

Press the ‘Ctrl+x‘ button, type ‘y‘, then press ‘Enter‘ to save the configuration and exit.

Now execute the following podman command to run the new container with custom volume.

podman run -it --rm -d -p 8080:80 --name web -v ~/data:/usr/share/nginx/html nginx:alpine

An option you must know:

  • -v – specify the volume for your container. The ‘~/data‘ directory will be mounted to the new container directory ‘/usr/share/nginx/html‘.

Now execute the following command to verify the running container.

podman ps

You will see the new container ‘web‘ in on the status ‘Up‘.

Using custom volume with podman

Next, open your web browser and type your server IP address with port ‘8080‘.

http://192.168.1.15:8080/

And you will see the custom index.html page you’ve created on top, which means the ‘~/data‘ directory is mounted to the ‘web‘ container.

Container web with custom index.html provided by volume

7. Next, you can log in to the running container with the option ‘exec‘ and executing the ‘sh’ shell.

Make sure your container ‘web‘ is running using the following command.

podman ps

Now execute the podman command below to log in to the container ‘web’.

podman exec -it web /bin/sh

Check the hostname of the container ‘web’.

hostname

Check the IP address and routing table of container ‘web’.

ip a
route -n

Now type ‘exit‘ to log out from the container ‘web‘.

Podman log in to the container

8. Now clean up your environment using the following command.

Stop the container ‘web‘ using the podman command below.

podman stop web

Remove all containers with the status ‘Exited’ using the podman command below.

podman rm $(podman ps --filter "status=exited" -q)

Cleanup exited container with podman

Conclusion

Congratulation! You have successfully installed Podman on Ubuntu 20.04 system. Also, you’ve learned the basic usage of podman for downloading container images, run the container, checking container status, logs, and basic usage of podman for managing volume. For the next step, you can try to create your custom images with OCI specifications for your application push/upload to the container registry.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article