Getting Started with Podman on Ubuntu Server 22.04

Introduction

Podman is a modern, lightweight container engine that provides a secure and rootless alternative to Docker. Unlike Docker, Podman operates without a central daemon, making it ideal for developers and system administrators who prioritize security and flexibility in container management.

In this guide, we’ll walk you through the basics of installing and using Podman on Ubuntu Server 22.04. Additionally, we’ll address a common issue related to pulling container images using short names (e.g., nginx) and explain how to resolve it.


What is Podman, and Why Use It?

Podman is a container engine designed to build, run, and manage containers with a focus on security and flexibility. Here’s why Podman is gaining popularity:

  • Daemonless Architecture: Podman doesn’t rely on a background service (unlike Docker’s daemon-based model), reducing attack surfaces and improving security.
  • Rootless Containers: Run containers as a non-root user to further enhance security.
  • Docker-Compatible Commands: Podman’s CLI is designed to be compatible with Docker, allowing you to use commands like podman pull or podman run seamlessly.
  • Systemd Integration: Manage containers as system services with ease.

Step 1: Install Podman on Ubuntu Server 22.04

1.1 Update Your System

Before installing Podman, ensure your system is updated to avoid compatibility issues:

sudo apt update && sudo apt upgrade -y  

1.2 Install Podman

Podman is included in Ubuntu’s default repositories, so you can install it directly using the apt package manager:

sudo apt install podman -y  

1.3 Verify Installation

After installation, verify the installed version of Podman to ensure it’s ready to use:

podman --version  

Example output:

podman version 3.4.4


Step 2: Run Your First Podman Container

2.1 Pull a Container Image

Podman uses container images to run applications. Start by pulling the lightweight Alpine Linux image:

podman pull alpine  

2.2 Run a Container

Run a container using the Alpine image:

podman run --rm -it alpine sh  

This command starts an interactive shell inside the Alpine container. You can test basic commands, such as:

echo "Hello from Podman!"  

2.3 Exit the Container

To exit the container, type:

exit  


Step 3: Understanding Podman Short-Name Resolution

The Issue with Short Names

When pulling container images using short names (e.g., nginx), Podman needs to know which registry to search for the image. Without proper configuration, you may encounter the following error:

Error: short-name "nginx" did not resolve to an alias and no unqualified-search registries are defined in "/etc/containers/registries.conf"  

Why Does This Error Happen?

Podman requires a configuration file (registries.conf) to define the container registries it should search for images. If this file is missing or improperly configured, Podman cannot resolve short names like nginx.


Step 4: Fixing the Short-Name Resolution Error

4.1 Locate or Create the registries.conf File

The registries.conf file is typically located at /etc/containers/registries.conf. Check if the file exists:

ls /etc/containers/registries.conf  

If it doesn’t exist, create it:

sudo nano /etc/containers/registries.conf  

4.2 Add Default Registry Configuration

Add the following configuration to the file:

[registries.search]  
registries = ['docker.io', 'quay.io']  

  • docker.io: Refers to Docker Hub, the most commonly used container registry.
  • quay.io: Another popular container registry.

Save and exit the file (Ctrl+O, then Ctrl+X if using nano).

4.3 Retry the Pull Command

With the registry configuration in place, try pulling the nginx image again:

podman pull nginx  

Podman should now resolve the short name nginx to docker.io/library/nginx and successfully pull the image.


Step 5: Run Containers as a Non-Root User

One of Podman’s standout features is its ability to run containers without requiring root privileges. Here’s how to set it up:

5.1 Switch to a Non-Root User

Log in as a regular user (e.g., uadmin):

su - uadmin  

Verify Podman works for the non-root user:

podman info  

5.2 Fix Permission Issues for Non-Root Users

If you encounter a permission denied error when running Podman as a non-root user, it’s likely due to incorrect ownership of the user’s home directory.

Check the ownership of the home directory:

ls -ld /home/uadmin  

If the directory is owned by root, fix it:

sudo chown -R uadmin:uadmin /home/uadmin  

Manually create the required .local directory:

mkdir -p /home/uadmin/.local/share/containers  
sudo chown -R uadmin:uadmin /home/uadmin/.local  

Test Podman again:

podman info  

Podman should now work successfully as a non-root user.


Step 6: Useful Podman Commands for Beginners

Here are some essential Podman commands to help you get started:

  • List Running Containers:
podman ps
  • List All Containers (Including Stopped):
podman ps -a
  • Stop a Running Container:
podman stop <container-id>
  • Remove a Container:
podman rm <container-id>
  • Remove an Image:
podman rmi <image-name>

    Conclusion

    Podman is a secure, lightweight, and flexible container engine that’s perfect for Ubuntu Server 22.04. In this guide, we covered:

    • Installing Podman.
    • Running your first container.
    • Fixing the short-name resolution error.
    • Running containers as a non-root user.

    By following these steps, you now have a solid foundation to start exploring Podman and managing containers effectively. With its rootless capabilities and Docker-compatible CLI, Podman is an excellent tool for modern containerization workflows.

    Stay Tuned:
    Next Article we will talk about deploy ubuntu server on cloud platforms such as GCP, AWS and Azure.

    Leave a Comment