106Wordpress - amagerard/Docker GitHub Wiki

Docker/Home

RedHat10/Docker.

1- Docker 2- Firewall 3- Portainer 4- Drupal 5- Joomla
6-Wordpress 7-Xwiki 8- Mediawiki 9- Environment
Casaos Zimaos

6. Wordpress.

Wordpress is a Content Management System.
It is possible to create, manage and easily modify a website.

This procedure is based on this example.

Docker server ip :192.168.60.55/24
Your username: teacher
Your home directory:/home/teacher

6.1 Create Network and Volumes with Portainer.

Open Portainer for create network and volumes.
https://192.168.60.55:9443

Click on Environments Local.

6.1.1 Each Wordpress instance has its container.

  • cnt_wps_wb_number.
  • cnt_wps_db_number.
    Containers will be created from the docker compose command line.

6.1.2 Each Wordpress instance has its network.

  • net_wps_number.

Create net_wps_1 .

Select Networks.  
Add network.  
Name : net_wps_1  
Add Driver options:  
Name : com.docker.network.bridge.name   value : net_wps_1  
Create the network.  

Name: com.docker.network.bridge.name value : net_wps_1 is not specified, docker will give it a name as br-XXXX.

6.1.3 Each Wordpress instance has its volume.

  • vol_wps_wb_number.
  • vol_wps_db_number.
    Create these volumes.
Select Volumes.  
Add volume.  
Name: vol_wps_wb_1  
Create the volume.  
Add the volume.  
Name: vol_wps_db_1  
Create the volume.  

6.1.4 Information.

Command lines to create the netwok and volumes without portainer.
docker network create --opt com.docker.network.bridge.name=net_wps_1 net_wps_1
docker volume create vol_wps_wb_1
docker volume create vol_wps_db_1

6.2 Give permissions to www-dock and mysql-dock for volumes.

chown -R www-dock:www-dock /var/lib/docker/volumes/vol_wps_wb_1
chown -R mysql-dock:mysql-dock /var/lib/docker/volumes/vol_wps_db_1

6.3 Install Wordpress with a yaml script.

Logging with your username.
mkdir /home/teacher/wordpress
vi /home/teacher/wordpress/instance_wordpress_1.yaml

# 2025, January  
# replace number  by 1 or 2 or ... until 9  
# example n = 1  
# port published wb = 8081  
# port published db = 33771  
# ip = 192.168.60.55  
# you must create network net_wps_1    from portainer before installing wordpress.  
# you must create volume vol_wps_wb_1  from portainer before installing wordpress.  
# you must create volume vol_wps_db_1  from portainer before installing wordpress.  
#  
# Be careful with the tab, the indentation is 4 characters.  
#  
name: wordpress_1  ## change  n of wordpress_n  
services:  
    wordpress:  
        cpu_shares: 90  
        command: []  
        container_name: cnt_wps_wb_1  ## change  n  of cnt_wps_wb_n  
        depends_on:  
            wordpressdb:  
                condition: service_started  
                restart: true  
                required: true  
        deploy:  
            resources:  
                limits:  
                    memory: 2048M  
        environment:  
            - WORDPRESS_DB_HOST=172.18.0.1:33771  ##<ip gateway network net_wps_1>:port published mariadb  
            - WORDPRESS_DB_NAME=wordpress  
            - WORDPRESS_DB_USER=wordpress  
            - WORDPRESS_DB_PASSWORD=641fqAB4d ## as you want  
        hostname: cnt_wps_wb_1  ## change n to cnt_wps_wb_n  
        image: wordpress:latest  
        links:  
            - wordpressdb:mariadb  
        ports:  
            - target: 80  
              published: '8081'  ## change  n of 808n  
              protocol: tcp  
        restart: always  
        volumes:  
            - /var/lib/docker/volumes/vol_wps_wb_1/_data:/var/www/html # create volume from portainer and change  n of vol_wps_wb_n  
        user: 2001:2001  ## www-dock gid ., Put '#' before the line for casaos or zimaos  
        devices: []  
        cap_add: []  
        network_mode: net_wps_1 # create network  from portainer and change  n of net_wps_1  
        privileged: false  
    wordpressdb:  
        cpu_shares: 90  
        command: []  
        container_name: cnt_wps_db_1  ## change  n  of  cnt_wps_db_n  
        deploy:  
            resources:  
                limits:  
                    memory: 2048M  
        environment:  
            - MYSQL_ROOT_PASSWORD=641fqAB4d ## it is the same MYSQL_PASSWORD  and WORDPRESS_DB_PASSWORD  
            - MYSQL_DATABASE=wordpress  
            - MYSQL_USER=wordpress  
            - MYSQL_PASSWORD=641fqAB4d  ## it is the same MYSQL_PASSWORD  and WORDPRESS_DB_PASSWORD  
  
        hostname: cnt_wps_db_1 ## change  n of cnt_wps_db_n  
        image: mariadb:latest  
        ports:  
            - target: 3306  
              published: '33771'  ## change  n to  3377n  
              protocol: tcp  
        restart: always  
        volumes:  
            - /var/lib/docker/volumes/vol_wps_db_1/_data:/var/lib/mysql  ## create volume from portainer and change  n of vol_wps_db_n  
        user: 2002:2002  ## mysql-dock gid ., Put '#' before the line for casaos or zimaos  
        devices: []  
        cap_add: []  
        network_mode: net_wps_1  # create network  from portainer and change  n of net_wps_1  
        privileged: false  

Installation.
docker compose -f /home/teacher/wordpress/instance_wordpress_1.yaml up
Crtl+C to stop.

Information.
If you want to change your Wordpress instance in 2,
edit this file and replace _1 by _2.
Then replace the port 8081 by 8082
and 33771 by 33772.
You must replace WORDPRESS_DB_HOST=172.18.0.1 by the net_wps_2 gateway.

6.4 Attach volumes with containers.

On portainer, by default the volumes are marked Unused.

Open Portainer.
https://192.168.60.55:9443

Click on Environments Local.

Select Container cnt_wps_wb_1.
Click on cnt_wps_wb_1.
Click Duplicate/Edit.
Advanced container settings.
Click on Volumes.
Volumes mapping.
Choose container /var/www/html.
Click on Volume.
Select a Volume.
Choose vol_wps_wb_1 - local.
Click on Deploy the container.
Click on Replace.

Select Container cnt_wps_db_1.
Click on cnt_wps_db_1.
Click Duplicate/Edit.
Advanced container settings.
Click on Volumes.
Volumes mapping.
Choose container /var/lib/mysql.
Click on Volume.
Select a Volume.
Choose vol_wps_db_1 - local.
Click on Deploy the container.
Click on Replace.

6.5 Open Wordpress.

6.5.1 Firewall.

Find your interface for network net_wps_1.
The gateway is displayed in Portainer Networks.
Example:
The net_wps_1 gateway is 172.18.0.1/16.
Network interface is net_wps_1.

ifconfig

net_wps_1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500  
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255  
        ether 02:42:6a:71:3d:51  txqueuelen 0  (Ethernet)  
        RX packets 10742  bytes 23229052 (22.1 MiB)  
        RX errors 0  dropped 0  overruns 0  frame 0  
        TX packets 12571  bytes 5854522 (5.5 MiB)  
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0  

Add rules on firewall.
iptables -t nat -A POSTROUTING ! -o net_wps_1 -s 172.18.0.0/16 -j MASQUERADE
ufw route allow in on net_wps_1 out on eth0 to any from any

The postrouting iptable rule disappears after restarting the server.
The solution is to add the rule in crontab.
Add postrouting iptable in /opt/docker/postrouting_sh.
vi /opt/docker/postrouting_sh

iptables -t nat -A POSTROUTING ! -o net_wps_1  -s 172.18.0.0/16 -j MASQUERADE  

Don't forget to delete line if you no longer use this iptable rule.

Information.
To delete an iptable postrouting rule without reboot.
Postrouting line number.
iptables --list -v --line-numbers -t nat
Remove rule ( 1 is the line number).
iptables -t nat -D POSTROUTING 1

6.5.2 First access Wordpress.

http://192.168.60.55:8081

Select language : English.
Continue.
Site Title : Mybeautifulweb.
Username: as you want.
Password: as you want.
Confirm use a weak password : as you want.
Your Email: as you want.
Search engine visibility : as you want.
Install WordPress.
Login.
Username: your username.
Password: your password.

Wordpress is ready.

To return to the administrator page.
http://192.168.60.55:8081/wp-admin