How to Install Podman on Ubuntu 20.04 and 22.04
How to Install Podman on Ubuntu 20.04 and 22.04
Podman is an open-source container management tool that provides many of the same features as Docker but with a focus on security and simplicity. Unlike Docker, Podman does not require a daemon running as root, making it a more secure option for running containers. This guide will show you how to install Podman on both Ubuntu 20.04 and 22.04, and how to use it to manage containers effectively.
Why Choose Podman?
Before jumping into the installation, let’s briefly cover why Podman is gaining popularity:
Rootless Containers: Podman can run containers as a non-root user, enhancing security by limiting privileges.
Docker-Compatible Commands: Podman uses the same commands as Docker (podman run, podman pull, etc.), making it easy to switch from Docker.
No Daemon: Unlike Docker, Podman doesn’t require a central daemon to manage containers. Each container runs independently.
Kubernetes-Ready: Podman can generate Kubernetes YAML files, making it suitable for use in container orchestration platforms like Kubernetes.
Prerequisites
Before proceeding with the installation, make sure your system meets the following requirements:
Ubuntu 20.04 or 22.04: This guide is tailored for these versions of Ubuntu.
User with sudo privileges: You’ll need root access to install Podman.
Internet access: The Podman packages will be downloaded from official repositories.
Step 1: Update Your System
Start by updating your package list and upgrading your existing packages. Open a terminal and run the following commands:
sudo apt update
sudo apt upgrade
This ensures that your system is up to date with the latest security patches and software.
Step 2: Install Podman on Ubuntu 20.04
Adding the Podman Repository
Podman is not available in the default Ubuntu 20.04 repositories, so you’ll need to add the official Podman repository. To do this, run the following commands:
. /etc/os-release
sudo sh -c “echo ‘deb http://deb.tigera.io/ubuntu focal main’ > /etc/apt/sources.list.d/podman.list”
This command adds the Podman repository to your system.
Adding the GPG Key
Next, you need to add the GPG key for the Podman repository to ensure package integrity:
sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys 90F28C3A81530A2B
Installing Podman
Now, update your package list and install Podman:
sudo apt update
sudo apt install podman
Once the installation is complete, verify the installation by running:
podman –version
This should display the version of Podman installed on your system.
Step 3: Install Podman on Ubuntu 22.04
For Ubuntu 22.04, the process is a bit simpler as Podman is available in the official Ubuntu repositories.
Installing Podman
You can install Podman directly using the APT package manager:
sudo apt update
sudo apt install podman
Once installed, you can verify the version with:
podman –version
Step 4: Configure Podman for Rootless Containers (Optional)
One of Podman’s standout features is its ability to run containers without root privileges. To configure this, follow these steps:
Step 4.1: Enable User Namespace
Ubuntu enables user namespaces by default, but you can verify it by checking the kernel parameters:
sudo sysctl user.max_user_namespaces
If the value is set to 1, user namespaces are enabled.
Step 4.2: Configure Subuid and Subgid
To run rootless containers, each user needs entries in the /etc/subuid and /etc/subgid files. Add the following lines to both files, replacing username with your actual username:
username:100000:65536
This will allow your user to map the UID and GID ranges needed for rootless containers.
Step 4.3: Run Rootless Containers
Once you’ve configured the namespace and UID ranges, you can run containers without using sudo. For example:
podman run –rm -it alpine
This will pull the alpine image and start a container as a regular user.
Step 5: Using Podman
Now that Podman is installed, let’s go over some basic commands to help you get started.
5.1 Pulling an Image
To pull a container image from a registry, use the podman pull command. For example, to pull the latest Ubuntu image, run:
podman pull ubuntu
5.2 Running a Container
Once the image is pulled, you can run it in a container using the podman run command. Here’s an example of running an Ubuntu container interactively:
podman run -it ubuntu
The -it flag ensures that the container is interactive, giving you a terminal to work with inside the container.
5.3 Listing Running Containers
To view all running containers, use the podman ps command:
podman ps
To view all containers (including stopped ones), use:
podman ps -a
5.4 Stopping and Removing Containers
To stop a running container, use:
podman stop
Replace with the actual container ID. To remove a stopped container, use:
podman rm
5.5 Managing Images
To list all the images on your system, use:
podman images
To remove an image, use:
podman rmi
Step 6: Switching from Docker to Podman
If you’ve been using Docker and want to transition to Podman, the good news is that Podman uses the same command-line syntax as Docker. Simply replace docker with podman in your commands. For example:
Docker command: docker run -it ubuntu
Podman equivalent: podman run -it ubuntu
Podman can even alias Docker commands to podman to make the transition easier. To set this up, run:
alias docker=podman
This allows you to continue using Docker commands, but they will be executed by Podman instead.
Step 7: Managing Pods in Podman
One of Podman’s advanced features is its ability to manage pods. A pod is a group of containers that share networking and storage resources. Here’s how you can create and manage pods in Podman:
7.1 Creating a Pod
To create a pod, use the podman pod create command:
podman pod create –name mypod
7.2 Running Containers in a Pod
You can run containers within a pod by specifying the –pod option:
podman run -d –pod mypod nginx
This will run an Nginx container inside the mypod pod.
7.3 Listing Pods
To list all pods, use:
podman pod ls
Conclusion
Podman is an excellent alternative to Docker, especially if you’re looking for a more secure, rootless container runtime. It’s easy to install on both Ubuntu 20.04 and 22.04, and its compatibility with Docker commands makes it an easy transition for users familiar with Docker. Whether you’re running containers for development or production, Podman offers a lightweight, secure, and efficient way to manage containers on your Ubuntu system.
By following this guide, you should be able to install and configure Podman on your Ubuntu machine, and start running and managing containers with ease.