DEV Community

Cover image for How to create a WordPress containerization environment with podman(a docker alternative) in fedora 36/37 Workstation GNU/Linux
Christian Bueno
Christian Bueno

Posted on • Updated on

How to create a WordPress containerization environment with podman(a docker alternative) in fedora 36/37 Workstation GNU/Linux

Hello to everyone, today we are going to learn how to create a WordPress containerization environment with podman a docker alternative.
We need podman, the WordPress image, and a database image MySQL or MariaDB
First we are going to download the images, using the following command.

podman pull WordPress:6.0.3-php8.0-apache
podman pull mariadb:10.7.6-focal

Enter fullscreen mode Exit fullscreen mode

Now we are going to create a Container file in order to build a customized WordPress image.

But , What is the problem if we use the normal WordPress image?

Well, we are not being able to install themes, because WordPress won’t be able to connect to internet. It’s relate to different port in the mapped port.

We will create a pod and inside it will create the wordpress and database containers and we have to export or map the container port with a host port.
For example we can use in the port mapping option 8080:80 when create the pod.

--publish host-port:container-port
--publish 8080:80

podman pod create --name ${pod_name} --infra --publish 8080:80 --publish 3306:3306 --network bridge

Enter fullscreen mode Exit fullscreen mode

That will raise the following errors.

The REST API encountered an error
Error: cURL error 28: Failed to connect to localhost port >8080: Connection timed out (http_request_failed)
Your site could not complete a loopback request
Error: cURL error 28: Failed to connect to localhost port >8080: Connection time out (http_request_failed)

The REST API encountered an error

To fix that we are going to add the port 8080 for example in the Apache server config of the wordpress image, you can use any port you like. This way when we create the pod in the bellow script we can use in the port mapping 8080 as the port of the wordpress container.

#Containerfile

ARG wordpress_image=wordpress:6.0.3-php8.0-apache
FROM $wordpress_image
ARG port=8080
LABEL maintainer="Christian Bueno <chmabuen@espol.edu.ec>" 
#add port
RUN cd /etc/apache2/; \
sed -i "/Listen 80/aListen $port" ports.conf; \
cd /etc/apache2/sites-available/; \
sed -i "s/:80/*:*/" 000-default.conf
EXPOSE $port
CMD ["apache2-foreground"]
# podman build --build-arg port=<port> --build-arg wordpress_image=<wordpress_image> -t <wordpress_name_and_tag> -f Containerfile
# podman build --build-arg port=8082 --build-arg wordpress_image=wordpress:6.0.2-php7.4-apache -t christianbueno1/wordpress:602-8082 -f Containerfile
# podman build -t christianbueno1/wordpress:602-8080 -f Containerfile
# podman build -t <new_image_name:version> -f Containerfile

Enter fullscreen mode Exit fullscreen mode

Now to buil the new worpress image, run the following command, you can use for example the following values.

  • In port 8080
  • In the wordpress_image wordpress:6.0.3-php8.0-apache
  • In the tag name use your DockerHub user and any name you like e.g.dockeHubUser/image-name:version-number this way you will can upload the image to DockerHub in the future.
podman build –build-arg port=8080 –build-arg wordpress_image=wordpress:6.0.3-php8.0-apache -t christianbueno1/wordpress:603-8080

Enter fullscreen mode Exit fullscreen mode

After that, then we are going to create a pod and inside that we are going to create the wordpress and database container.

We will create 2 directories var-www-html and var-lib-mysql to store all the work and avoid lost it.

mkdir var-www-html var-lib-mysql
Enter fullscreen mode Exit fullscreen mode

Because we are going to mount a directory(volume) to the containers we need to map the UID:GID of your user with the UID:GID of the container. This way the container will can modify those files and directories.

The directory /var/www/html have a UID:GID of 33(www-data) , will use the tool podman unshare. The directory /var/lib/mysql have an UID:GID of 999(mysql).

podman unshare chown 33:33 -R var-www-html
podman unshare chown 999:999 -R var-lib-mysql

Enter fullscreen mode Exit fullscreen mode

But an important thing is can be able to edit files from the host, in this case files in the directory var-www-html, for that reason, we will use Permissions to add write rights to Others. This way our users can be able to edit those files. Obviously it will be done after run the wordpress container.

An important environment variable is PODMAN_USERNS it is used to set the user namespace mode for all the containers in a pod. You can set this variable to ”” to make sure map your user to the root user in the container. By the way it is the default value.

--userns=mode

export PODMAN_USERNS=""
Enter fullscreen mode Exit fullscreen mode

Now is the time to run the shell script. It will create a pod and inside it create the mariadb container and the wordpress container. In the variable section in the script you can edit the values of the user, password, database name, etc. The script need 2 arguments wordpress_image and pod_name if you don’t pass it the default values will be taken.

#!/bin/bash
# create_blog.sh

set -e   #exit on most errors

##variables
wordpress_image=${1:-christianbueno1/wordpress:603-php80-8080}
database_image=mariadb:10.7.6-focal
pod_name=${2:-blog}
user=chris
password=maGazine1!ec
database_name=company
#use your directories name
database_volume=var-lib-mysql
wordpress_volume=var-www-html
#
database_container_name=mariadb-${pod_name}
wordpress_container_name=wordpress-${pod_name}
##

echo "Creating the pod"
podman pod create --name ${pod_name} --infra --publish 8080:8080 --publish 3306:3306 --network bridge

echo "Creating the mariadb container"
podman run --pod ${pod_name} --name ${database_container_name} \
-e MARIADB_USER=${user} \
-e MARIADB_PASSWORD=${password} \
-e MARIADB_DATABASE=${database_name} \
-e MARIADB_ROOT_PASSWORD=${password} \
--volume ./${database_volume}:/var/lib/mysql:Z \
-d ${database_image}

echo "Creating the wordpress container"
podman run --pod ${pod_name} --name ${wordpress_container_name} \
-e WORDPRESS_DB_HOST=${database_container_name}:3306 \
-e WORDPRESS_DB_USER=${user} \
-e WORDPRESS_DB_PASSWORD=${password} \
-e WORDPRESS_DB_NAME=${database_name} \
--volume ./${wordpress_volume}:/var/www/html:Z \
-d ${wordpress_image}

#export PODMAN_USERNS=keep-id
#
#run the script
#
#./create_blog.sh <wordpress_image> <pod_name>
#./create_blog.sh christianbueno1/wordpress:602-8080 podman-blog
#use the default wordpress_image=christianbueno1/wordpress:603-php80-8080 in the first argument.
#./create_blog.sh "" podman-blog
#use the default wordpress_image and pod_name arguments
#./create_blog.sh

Enter fullscreen mode Exit fullscreen mode

Run the scripts.

./create_blog.sh <new_wordpress_image> <pod_name>
Enter fullscreen mode Exit fullscreen mode

In my case

./create_blog.sh christianbueno1/wordpress:603-php80-apache podman-blog
Enter fullscreen mode Exit fullscreen mode

Go to the browser localhost:<port> and remember to use your configured port, in my case 8080, https://localhost:8080.

Last but not least add write rights to others in var-www-html from inside the wordpress container so you can edit files from the host using any text editor for example vscode.

Go into the wordpress container.

podman exec -it wordpress-<pod_name> /bin/bash
Enter fullscreen mode Exit fullscreen mode

In my case

podman exec -it wordpress-podman-blog /bin/bash
Enter fullscreen mode Exit fullscreen mode

You will be as root user. Add write rights to Others.

cd /var/www
chmod o+w -R html

Enter fullscreen mode Exit fullscreen mode

That’s all.
If it has helped you, ask for your support and be able to continue writing tutorials, any amount will be welcome. paypal.me/podmanblog

podman rootless volume
debug-rootless-podman-mounted-volumes

Top comments (0)