104Drupal - 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

4. Drupal.

Drupal 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

4.1 Create Network and Volumes with Portainer.

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

Click on Environments Local.

4.1.1 Each Drupal instance has its container.

  • cnt_dpl_wb_number.
  • cnt_dpl_db_number.
    Containers will be created from the docker compose command line.

4.1.2 Each Drupal instance has its network.

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

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

4.1.3 Each Drupal instance has its volume.

  • vol_dpl_wb_number
  • vol_dpl_db_number
    Create these volumes.
Select Volumes.  
Add volume.  
Name: vol_dpl_wb_1  
Create the volume.  
Add the volume.  
Name: vol_dpl_db_1  
Create the volume.  

4.1.4 Information.

Command lines to create the netwok and volumes without portainer.
docker network create --opt com.docker.network.bridge.name=net_dpl_1 net_dpl_1
docker volume create vol_dpl_wb_1
docker volume create vol_dpl_db_1

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

chown -R www-dock:www-dock /var/lib/docker/volumes/vol_dpl_wb_1
chown -R mysql-dock:mysql-dock /var/lib/docker/volumes/vol_dpl_db_1

4.3 Install Drupal with a yaml script.

logging with your username.
mkdir /home/teacher/drupal
vi /home/teacher/drupal/instance_drupal_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_dpl_1    from portainer before installing drupal.  
# you must create volume vol_dpl_wb_1  from portainer before installing drupal.  
# you must create volume vol_dpl_db_1  from portainer before installing drupal.  
#  
# Be careful with the tab, the indentation is 4 characters.  
#  
name: drupal_1  ## change  n of drupal_n  
services:  
    drupal:  
        cpu_shares: 90  
        command: []  
        container_name: cnt_dpl_wb_1  ## change   n  of cnt_dpl_wb_n  
        depends_on:  
            drupaldb:  
                condition: service_started  
                restart: true  
                required: true  
        deploy:  
            resources:  
                limits:  
                    memory: 2048M  
        environment:  
            - DRUPAL_DB_HOST=cnt_dpl_db_1  ## change  n of cnt_dpl_db_n  
            - DRUPAL_DB_PASSWORD=641fqAB4d  ## Change password if you want  
        hostname: cnt_dpl_wb_1  ## change  n of cnt_dpl_wb_n  
        image: drupal:latest  
        links:  
            - drupaldb:mariadb  
        ports:  
            - target: 80  
              published: '8081'  ## change n of published : 808n  
              protocol: tcp  
        restart: always  
        volumes:  
            - /var/lib/docker/volumes/vol_dpl_wb_1/_data:/var/www/html #  create volume from portainer and change  n of vol_dpl_wb_n  
        user: 2001:2001  ## www-dock (uid:gid)  
        devices: []  
        cap_add: []  
        network_mode: net_dpl_1  # create with portainer, change n of net_dpl_n  
        privileged: false  
    drupaldb:  
        cpu_shares: 90  
        command: []  
        container_name: cnt_dpl_db_1  ## change   n  of cnt_dpl_db_n  
        deploy:  
            resources:  
                limits:  
                    memory: 2048M  
        environment:  
            - MYSQL_ROOT_PASSWORD=641fqAB4d # it is the same password DRUPAL_DB_PASSWORD  
        hostname: cnt_dpl_db_1 ## change  n of cnt_dpl_db_n  
        image: mariadb:latest  
        ports:  
            - target: 3306  
              published: '33771'  ## change  n of  3377n  
              protocol: tcp  
        restart: always  
        volumes:  
            - /var/lib/docker/volumes/vol_dpl_db_1/_data:/var/lib/mysql   ## create volume from portainer and change  n of vol_dpl_db_n  
        user: 2002:2002  ## mysql-dock (uid:gid)  
        devices: []  
        cap_add: []  
        network_mode: net_dpl_1 # create network  from portainer and change  n of net_dpl_1  
        privileged: false  

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

Information.
If you want to change your drupal instance in 2,
edit this file and replace _1 by _2.
Then replace the port 8081 by 8082
and 33771 by 33772.

4.4 Attach volumes with containers.

On portainer, by default the volumes are marked Unused.
This is a problem for drupal.

Launch Portainer.
https://192.168.60.55:9443

Click on Environments Local.

Select Container cnt_dpl_db_1.
Click on cnt_dpl_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_dpl_db_1 - local.
Click on Deploy the container.
Click on Replace.

Select Container cnt_dpl_wb_1.
Click on cnt_dpl_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_dpl_wb_1 - local.
Click on Deploy the container.
Click on Replace.

4.5 Give permissions to www-dock for volumes.

I don't know why Portainer creates the files with root permissions.
This is not really a solution but it solves my problem.
chown -R www-dock:www-dock /var/lib/docker/volumes/vol_dpl_wb_1

4.6 Open Drupal.

4.6.1 Firewall.

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

ifconfig

net_dpl_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_dpl_1 -s 172.18.0.0/16 -j MASQUERADE
ufw route allow in on net_dpl_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_dpl_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

4.6.2 First access Drupal.

http://192.168.60.55:8081.

Select language : English
Save and continue.
Standard.
Save and continue.
Database type : MySQL,MariadB
Database name : cnt_dpl_db_1
Database username : root
Database password: 641fqAB4d
Advanced options.
Host: 172.18.0.1
The ip host is the network net_dpl_1 gateway.
The gateway is displayed in Portainer Networks.
Port number: 33771
Save and continue.

Site name: Mybeautifulweb
Site email addres: as you want
Username: as you want
Password: as you want
Confirm password: as you want
Save and continue.

Drupal is ready.