Skip to main content

Create fast, easy, and repeatable containers with Podman and shell scripts

Get started with containers in a fast, repeatable way through the familiar shell scripting interface.
Image
Blue, white, orange, and brown containers stacked

Pexels, CC0

Podman is a daemon-less container engine for developing, managing, and running Open Container Initiative (OCI)-compliant containers and container images. It follows industry standards to provide a robust container-management tool that you can also integrate into Kubernetes and other services as needed.

Better still, Podman is Docker-compatible to the point that you can simply alias the Docker command-line interface (CLI) to the Podman command (alias docker=podman) and change nothing about your workflow or scripts.

This article starts a series about Podman's features, and I'll reuse the setup below in my future articles. In this one, I'll show you how to get started with containers in a fast and repeatable way through the familiar interface of shell scripting.

Setting things up

Because containers are designed to be disposable or ephemeral, it's important to make your containers repeatable. You want to be able to create a container with the environment you need, destroy it when you no longer need it, and know that you can recreate it later with no added effort. Many users look to the Dockerfile format for this, but a good shell script can be just as useful and has the added benefit of greater flexibility. For example:

$ podman --version

podman version 3.0.2-dev

I have already pulled some container images that I will use to run my containers and pods:

$ podman image ls

REPOSITORY                   TAG     IMAGE ID      CREATED      SIZE
docker.io/library/wordpress  latest  054741915cf1  2 days ago   629 MB
docker.io/library/mysql      latest  bbf6571db497  3 days ago   521 MB
docker.io/library/httpd      latest  ea28e1b82f31  3 days ago   148 MB

Scripting containers

I have created two simple scripts to deploy my pod and containers. The first creates a pod with a WordPress container:

#!/bin/bash
# create_blog.sh

set -e   #exit on most errors

podman pod create --name blog --infra --publish 8080:80 --network bridge 

podman run --pod blog --name mysql -e MYSQL_USER=alexon \
-e MYSQL_PASSWORD=123456 -e MYSQL_DATABASE=wpdb \
-e MYSQL_ROOT_PASSWORD=567890 \
--volume /var/local/mysql:/var/lib/mysql:Z \
-d docker.io/library/mysql 

podman run --pod blog --name wordpress -e WORDPRESS_DB_HOST=mysql \
-e WORDPRESS_DB_USER=alexon \
-e WORDPRESS_DB_PASSWORD=123456 \
-e WORDPRESS_DB_NAME=wpdb \
-d docker.io/library/wordpress

The next one starts a web server:

#!/bin/bash
# create_web.sh

podman run --name=httpd -p 8081:80 \
--volume /var/local/httpd:/usr/local/apache2/htdocs:Z \
-d docker.io/library/httpd

I'm referencing external mount points for my containers. Here are the contents of these directories:

$ ls -R /var/local/

/var/local/:
httpd  mysql

/var/local/httpd:
index.html

/var/local/mysql:

$ cat /var/local/httpd/index.html

<html>
  <header>
    <title>Enable SysAdmin</title>
  </header>
  <body>
    <p>Hello World!</p>
  </body>
</html>

[ You might also be interested in reading How Podman runs on Macs and other container FAQs. ]

Deploying the containers

With the proper configuration in place, I deploy my containers using my scripts:

$ ./create_web.sh

7c4c01d99cccf70a0c42ebdba458afb8be6d1563de3a91c99519213a4d8654af

$ ./create_blog.sh

a061ffe41575ca69ec48a2151220ba9462ed87c3c54d0f35bfa74507b56ff902
5bd37169ce0de342e5b86f7d8ba057b06cddb05b6d73520fca8c5ca69f1fe353
055fc7959f58921ef11119b44b1d250558272b523765e639a5e5c3aa2ad3d2a0

Just to make sure my applications are running correctly inside my recently deployed containers, I access them through a web browser. First, the basic httpd container app:

Image
Basic httpd container app
(Alexon Oliveira, CC BY-SA 4.0)

Then the basic WordPress container app:

Image
Basic WordPress container app
(Alexon Oliveira, CC BY-SA 4.0)

Both are up and running properly. 

Building familiarity into your container workflow

Containers don't have to be strange concepts to a Linux user. Integrated into the operating system, they're powerful tools for the busy sysadmin. In my next article, I'll demonstrate how Podman provides tools to see information about running pods.

Until then, you can learn more about Podman from 10 Podman guides to do more with containers in 2022Top 10 container guides for sysadmins, and of course, Podman.io.

Topics:   Podman   Containers  
Author’s photo

Alexon Oliveira

Alexon has been working as a Senior Technical Account Manager at Red Hat since 2018, working in the Customer Success organization focusing on Infrastructure and Management, Integration and Automation, Cloud Computing, and Storage Solutions. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.