How To Manage Podman Containers With Vagrant

Posted on 143 views

The Vagrant Podman Provisioner can be used to automatically install Podman which is a daemon-less container engine used to develop, manage and run OCI containers. Podman built and officially supported by RedHat, acts as a drop-in replacement for Docker. Just like Docker, Podman has the ability to pull container images and configure containers to run automatically on boot.

Podman is highly preferred when running containers since it allows one to run containers from Kubernetes as long as the images are OCI-compliant. Podman can also be used along with other provisioners such as the puppet provisioner e.t.c. In this setup, we will use Vagrant to set up the best working environment for the Podman provisioner.

This guide takes a deep dive into how to manage Podman Containers With Vagrant.

Getting Started.

For this guide, you need to have Vagrant installed on your system. Below are dedicated guides to help you install Vagrant on your system.

On Ubuntu, you will have to install the below package.

sudo apt-get install libarchive-tools

For this guide, I will use VirtualBox as my hypervisor. You can install it on your system using the guides below

With Vagrant and Virtualbox successfully installed using the aid of the above guides, now you are set to proceed as below.

Step 1 – Create a Vagrant Box

For this guide, we will create a CentOS 7 vagrant box using VirtualBox as the provider.

$ vagrant box add centos/7 --provider=virtualbox

You can as well use other Hypervisors as below.

##For KVM
vagrant box add centos/7 --provider=libvirt

##For VMware
vagrant box add generic/centos7 --provider=vmware_desktop

##For Docker
vagrant box add generic/centos7 --provider=docker

##For Parallels
vagrant box add generic/centos7 --provider=parallels

Create a Vagrant file for CentOS 7 as below

mkdir ~/vagrant-vms
cd ~/vagrant-vms
touch ~/vagrant-vms/Vagrantfile

Now we are set to edit the vagrant file depending on our preferences as below.

Step 2 – Manage Podman Containers With Vagrant

Now manage your Podman containers with Vagrant as below. Stop the running instance:

$ vagrant halt
==> default: Attempting graceful shutdown of VM...

1. Build Podman Images with Vagrant

The Vagrant Podman provisioner can be used to automatically build images. Container images can be built before running them or prior to any configured containers as below.

$ vim ~/vagrant-vms/Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.provision "podman" do |d|
    d.build_image "~/vagrant-vms/app"
  end
end

Above is a sample code on how to build an image from a Docker file and remember the path ~/vagrant/app must exist on your guest machine.

vagrant ssh
mkdir ~/vagrant-vms/app
chmod 755 ~/vagrant-vms/app

Stop the running instance as below

vagrant halt

Start Vagrant as below.

vagrant up --provision

2. Pull Podman Images with Vagrant

Images can also be pulled from Docker registries. There are two methods you can use to specify the Docker image to be pulled.

The first method is by using the argument images:

For example, to pull an Ubuntu image use:

$ vim ~/vagrant-vms/Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.provision "podman",
    images: ["ubuntu"]
end

The second option is by using the pull_images function as below.

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.provision "podman" do |d|
    d.pull_images "ubuntu"
    d.pull_images "alpine"
  end
end

Sample output:

$ vagrant up --provision
.........
==> default: Rsyncing folder: /home/thor/vagrant-vms/ => /vagrant
==> default: Running provisioner: podman...
    default: Podman installing
==> default: Pulling Docker images...
==> default: -- Image: ubuntu
==> default: Trying to pull registry.access.redhat.com/ubuntu...
==> default:   name unknown: Repo not found
==> default: Trying to pull registry.redhat.io/ubuntu...
==> default:   unable to retrieve auth token: invalid username/password: unauthorized: Please login to the Red Hat Registry using your Customer Portal credentials. Further instructions can be found here: https://access.redhat.com/RegistryAuthentication
==> default: Trying to pull docker.io/library/ubuntu...
==> default: Getting image source signatures
==> default: Copying blob sha256:7b1a6ab2e44dbac178598dabe7cff59bd67233dba0b27e4fbd1f9d4b3c877a54
==> default: Copying config sha256:ba6acccedd2923aee4c2acc6a23780b14ed4b8a5fa4e14e252a23b846df9b6c1
==> default: Writing manifest to image destination
==> default: Storing signatures
==> default: ba6acccedd2923aee4c2acc6a23780b14ed4b8a5fa4e14e252a23b846df9b6c1

3. Run Podman Containers with Vagrant

In addition to building and pulling Podman images, Vagrant can as well be used to run Podman containers. Running containers can be done using the Ruby block syntax normally with do…end blocks.

For example to run a Rabbitmq Podman container, use the below syntax.

$ vim ~/vagrant-vms/Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.provision "podman" do |d|
    d.run "rabbitmq"
  end
end

Start the instance

$ vagrant up --provision
........
==> default: Rsyncing folder: /home/thor/vagrant-vms/ => /vagrant
==> default: Running provisioner: podman...
==> default: Starting Docker containers...
==> default: -- Container: rabbitmq

Verify that the container has been started.

$ vagrant ssh
$ podman ps
CONTAINER ID  IMAGE                              COMMAND          CREATED             STATUS                 PORTS  NAMES
245e7d8cf138  docker.io/library/rabbitmq:latest  rabbitmq-server  About a minute ago  Up About a minute ago         rabbitmq

You can as well run multiple containers using the same image. Here, you have to specify the name of each container.

For example, running multiple MySQL instances, you can use the code as below.

$ vim ~/vagrant-vms/Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.provision "podman" do |d|
    d.run "db-1", image: "mariadb"
    d.run "db-2", image: "mariadb"
  end
end
$ vagrant up --provision
==> default: Rsyncing folder: /home/thor/vagrant-vms/ => /vagrant
==> default: Running provisioner: podman...
    default: Podman installing
==> default: Starting Docker containers...
==> default: -- Container: db-1
==> default: -- Container: db-2

Furthermore, a container can be configured to run with a shared directory mounted in it.

For example, running an Ubuntu container with a shared directory /var/www use the below code;

$ vim ~/vagrant-vms/Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.provision "podman" do |d|
    d.run "ubuntu",
      cmd: "bash -l",
      args: "-v '/vagrant:/var/www'"
  end
end

Sample output:

$ vagrant up --provision
==> default: Rsyncing folder: /home/thor/vagrant-vms/ => /vagrant
==> default: Running provisioner: podman...
    default: Podman installing
==> default: Starting Docker containers...
==> default: -- Container: ubuntu

You can verify your created Podman containers as below:

$ vagrant ssh
$ podman ps -a 
CONTAINER ID  IMAGE                            COMMAND  CREATED        STATUS                             PORTS  NAMES
ec976aa0ae53  docker.io/library/ubuntu:latest  bash -l  2 minutes ago  Exited (0) Less than a second ago         ubuntu

You can pause/hibernate the vagrant instance as below.

vagrant suspend

Delete the Vagrant instance:

vagrant destroy

Conclusion.

That marks the end of this guide! We have successfully gone through how to manage Podman Containers With Vagrant. I hope you found this guide important.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.