Jump to content

Running a ProcessWire site with Docker


jean-luc

Recommended Posts

logo.png
 
Docker (http://www.docker.com) is an open platform for building, shipping and running distributed applications.
 
Docker containers are a great way to package a complete application with its specific dependencies in a portable way so that it can easily be deployed on any compatible network or cloud infrastructure.
 
Recently I spent a few days making my ProcessWire site run in a Docker container, and - as I could not find any good tutorial for this - it sounded like a good idea to write one.  :)
 
You will find on the web plenty of presentations and tutorials about Docker, so I won't start with the basic concepts, and this tuto assumes that you have a first understanding of Docker's fundamentals.
 
What we want to do here is to migrate an existing site to a set of docker containers.
 
Therefore, to start with, you should have:
- docker installed on your computer;
- the site directory of your ProcessWIre site
- a backup of your site's MySQL database
 
Let's start.
 
Create a docker container for the site database
 
For several reasons (insulation, security, scalability), it is preferable to host the site database in a separate docker container. 
 
1. Set-up a SQL database with MariaDb or MySQL
$ docker run --name database -e MYSQL_ROOT_PASSWORD=rootdbpassword -d mariadb
Here I choose to use the MariaDB official container in its latest version, but MySQLwould be just fine as well.
 
2. Run a PhpMyAdmin container and create the ProcessWire database  
  
We first select an simple image with PhpMyAdmin on the Docker Hub: nazarpc/phpmyadmin and we create a docker container based on this image. This container will  access the port exposed by the database container via a private networking interface. We specify this with the `--link` option.
 
It can be run temporarily (and exited by ctrl-C):
docker run --rm --link database:mysql -p 8881:80 nazarpc/phpmyadmin
Or it can be run as a daemon in the background:
docker run -d --name phpmyadmin --link database:mysql -p 8881:80 nazarpc/phpmyadmin
From phpmyadmin (accessed from your browser at http://hostaddress:8881) you can now create your ProcessWire database, create a dedicated user for it, and import the database content from a previously saved SQL file.
 
Note: alternatively, you can do all database operations from the command line in the database docker container created during step 1, or use another mysql user interface container if you prefer…
 
3. Update the database parameters in your site configuration
 
In your site's `config.php` file, the sql server name shall be set to `mysql`:
$config->dbHost = 'mysql';
Other `$config->dbXxx` settings shall match the database name, user and password of the just-created database.
 
 
Create a Docker Image for Apache, PHP and the Processwire site
 
1. Create an image-specific directory with the following contents and `cd` to it
bash-3.2$ ls -l . config
.:
total 16
-rw-rw-rw-   1 jean-luc  staff  1163 21 aoû 12:09 Dockerfile
drwxr-xr-x  17 jean-luc  staff   578 17 aoû 12:48 ProcessWire
drwxr-xr-x   7 jean-luc  staff   238 21 aoû 12:07 config
drwxr-xr-x   7 jean-luc  staff   238 20 aoû 18:46 site

config:
total 160
-rw-rw-rw-  1 jean-luc  staff    160 20 aoû 18:28 msmtprc
-rw-rw-rw-  1 jean-luc  staff  72518 20 aoû 18:56 php.ini
where:
2.  Set the `Dockerfile` content
FROM php:5.6-apache

RUN    apt-get update \
&& apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libmcrypt-dev libpng12-dev zziplib-bin msmtp\
&& a2enmod rewrite \
&& a2enmod ssl \
&& docker-php-ext-install mysqli pdo_mysql iconv mcrypt zip \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install gd 

EXPOSE 80
EXPOSE 443

# Add a specific php.ini file
COPY config/php.ini /usr/local/etc/php/

# Configure the mail sent utility msmtp (http://msmtp.sourceforge.net) and make it readable only by www-data
COPY config/msmtprc /usr/local/etc/php/
RUN chmod 600 /usr/local/etc/php/msmtprc \
&& chown www-data:www-data /usr/local/etc/php/msmtprc

# Remove all default site files in /var/www/html
RUN rm -fR /var/www/html/*

# Copy ProcessWire core files
COPY ProcessWire/wire /var/www/html/wire
COPY ProcessWire/index.php /var/www/html/index.php
COPY ProcessWire/htaccess.txt /var/www/html/.htaccess

# Copy site-specific files
COPY site /var/www/html/site

# Make www-data the owner of site-specific files
RUN chown -R www-data:www-data /var/www/html/site

VOLUME /var/www/html/site
Based on the official image `php:5.6-apache`, it installs missing packages to the system, adds  mod-rewrite and mod-ssl to Apache, plus a number of PHP modules needed by Processwire (core or modules): mysqli, pdo_mysql, iconv, mcrypt, zip, and gd.
 
Then it copies the site files to the location expected by the Apache server.  Finally it declares a Docker volume `/var/www/html/site` (i.e. the site files and assets), so that it can be shared with other containers.
 
3. Set the msmtp configuration
 
We need to configure a sendmail utility, so that we can send emails from php, for example when a user registers on the website. The simplest way to do it is to rely on an external smtp server to do the actual sending. That's why we use msmtp.
 
- define the desired smtp account in `config/msmtprc`
account celedev-webmaster
tls on
tls_certcheck off
auth on
host smtp.celedev.com
port 587
user webmaster@celedev.com
from webmaster@celedev.com
password thepasswordofwebmasteratceledevdotcom
 
- in `config/php.ini`, configure the sendmail command so it uses msmtp:
sendmail_path = /usr/bin/msmtp -C /usr/local/etc/php/msmtprc --logfile /var/log/msmtp.log -a celedev-webmaster -t
 
4. Build the Docker image
docker build -t php-5.6-pw-celedev .
5. Create a Data-only container for the site files
docker run --name celedev-data php-5.6-pw-celedev echo "Celedev site data-only container"
6. Run the web server container
docker run --name celedev-site -p 8088:80 --link database:mysql --volumes-from celedev-data -d php-5.6-pw-celedev
Note that this container is linked to our database and shares the 'celedev-data' volume created previously
 
During development, it can be convenient to keep an access to the host file system from the container. For this, we can add a shared volume to the previous command:
docker run --name celedev-site -p 8088:80 --link database:mysql -v /Users/jean-luc/web/test-docker:/hostdir --volumes-from celedev-data -d php-5.6-pw-celedev
 
Our ProcessWire website is now up and running and we can test it in our browser at http://hostaddress:8088. Great!
 
What we now have in Docker
bash-3.2$ docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
php-5.6-pw-celedev    latest              2aaeb241c2e2        3 hours ago         1.149 GB
nazarpc/phpmyadmin    latest              e25cd4fd48b3        8 days ago          521 MB
mariadb               latest              dd208bafcc33        2 weeks ago         302.2 MB
debian                latest              9a61b6b1315e        5 weeks ago         125.2 MB

bash-3.2$ docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                    PORTS                                         NAMES
68cc5d976f0d        php-5.6-pw-celedev    "apache2-foreground"     20 hours ago        Up 20 hours               443/tcp, 0.0.0.0:8088->80/tcp                 celedev-site
0729fe6d6752        php-5.6-pw-celedev    "echo 'Celedev site d"   20 hours ago        Exited (0) 20 hours ago                                                 celedev-data
e3e9e3a4715c        mariadb               "/docker-entrypoint.s"   3 days ago          Up 3 days                 3306/tcp                                      database
Saving the site data
 
We can create an archive of the site files by running a tar command in a dedicated container:
bash-3.2$ docker run --rm -it --volumes-from celedev-data -v /Users/jean-luc/web/test-docker:/hostdir debian /bin/bash

root@2973c5af3eaf:/# cd /var/www/html/
root@2973c5af3eaf:/var/www/html# tar cvf /hostdir/backup.tar site
root@2973c5af3eaf:exit

bash-3.2$
Tagging and archiving the Docker image
 
We can also add a tag to the docker image that we have created in step 4 (recommended):
bash-3.2$ docker tag 2aaeb241c2e2 php-5.6-pw-celedev:0.11

bash-3.2$ docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
php-5.6-pw-celedev    latest              2aaeb241c2e2        3 hours ago         1.149 GB
php-5.6-pw-celedev    0.11                2aaeb241c2e2        3 hours ago         1.149 GB
nazarpc/phpmyadmin    latest              e25cd4fd48b3        8 days ago          521 MB
mariadb               latest              dd208bafcc33        2 weeks ago         302.2 MB
And we can archive this image locally if we dont want to push it now to the Docker Hub:
bash-3.2$ docker save php-5.6-pw-celedev:0.11 | gzip > php-5.6-pw-celedev-0.11.tar.gz 

And that's it!

You now have a portable image of your ProcessWire website that you can run directly on any docker-compatible system.

  • Like 19
Link to comment
Share on other sites

Good work! Regarding best practises, the only suggestion I have is that the tutorial should be changed to use environment variables for the database connection. This has several benefits, for an example

  • It would be very easy to launch a separate development site, which uses a different database, because all you need to change is the environment variable when launching the development web container
  • You could easily use the same config.php as a template for new sites
  • You don't have to store sensitive information in your version control
  • The config.php becomes more cloud compatible (i.e. a twelve-factor app)

Also since the MySQL-container is linked to the web-container, the config.php can directly use the environment variables set by Docker.

An example config.php would be

$config->dbHost = getenv("MYSQL_PORT_3306_TCP_ADDR");
$config->dbName = getenv("MYSQL_DBNAME");
$config->dbUser = getenv("MYSQL_DBUSER");
$config->dbPass = getenv("MYSQL_DBPASS");
$config->dbPort = getenv("MYSQL_PORT_3306_TCP_PORT");

Obviously launching the web container(s) would need to include these variables too, e.g.

docker run -e "MYSQL_DBNAME=mypwdb" -e "MYSQL_DBUSER=mypwdbuser" -e "MYSQL_DBPASS=mypwdbpass" --name celedev-site -p 8088:80 --link database:mysql --volumes-from celedev-data -d php-5.6-pw-celedev

Just my two cents :)

  • Like 7
Link to comment
Share on other sites

  • 9 months later...

I play with Rancher environment and custom docker images at the moment and it's fun :)

Created local images and deployed as Rancher stack

  1. caddy webserver (PW forum topic) as revproxy (use docker-gen to generate the caddyfile) and web server for PW.
  2. php5-fpm container (replaced it quickly with php7-fpm for short testing PW 3 with php7)
  3. openssh (used as sftp / scp and optional ssh access to the "webspace" = docker volume)

Works fine with HTTPS (frontend, ssl offloading) and http in the backend / internal container). Tested PW frontend, backend, image upload and content editing ...).

 

Because playing with docker is fun... 

I use RancherOS (OS with docker as pid 1), a custom kernel and custom desktop images (xorg, dbus, volumeicon, pcmanfm, chromium, ...) as "Docker Desktop"... but I should find the way back to PW soon because I'll create a new website in the near future... :lol:

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
  • 7 months later...
  • 2 weeks later...

Edit: solved using this gem of knowledge! I had to use "mariadb" as the DB host, because that is how I named (and linked) it in my compose file!

 

I am running the PW installer. PW files are located on my host (Arch Linux). Db files as well.

If I use localhost as the DB host I get this: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2 "No such file or directory")

The solution given to this everywhere is to use 127.0.0.1 instead, because localhost makes it look at my host machine instead of the MariaDB container.

But with 127.0.0.1 I get: SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")

I have confirmed inside the database container (using sh) that the user and password are correct.

The web server and database containers are linked. I am using https://github.com/abiosoft/caddy-docker/  and https://github.com/bianjp/docker-mariadb-alpine/

I found some advice to create firewall rules, but they did not help:

iptables -t filter -A INPUT -p tcp -i docker0 --dport 3306 -j ACCEPT

iptables -t filter -A OUTPUT -p tcp -o docker0 --dport 3306 -j ACCEPT

I found advice to add this to my.cnf: bind-address = 0.0.0.0 so it listens to all interfaces. No help.

What to try next?


Here is my docker-compose.yml:

Spoiler

 

version: '3.0'

services:
  caddy:
    image: abiosoft/caddy:php
    volumes:
      - /home/user/Documents/project/docker/Caddyfile:/etc/Caddyfile
      - /home/user/Documents/project/docker/processwire:/srv:Z
    ports:
      - "2015:2015"
    links:
      - mariadb
    restart: always

  mariadb:
    image: bianjp/mariadb-alpine:latest
    environment:
      MYSQL_DATABASE: db
      MYSQL_USER: user
      MYSQL_PASSWORD: pwd
      MYSQL_ROOT_PASSWORD: pwd
    volumes:
      - /home/user/Documents/project/docker/mariadbdata:/var/lib/mysql
    ports:
      - "3306:3306"
    restart: always

 

 

Edited by Beluga
SOLVED
Link to comment
Share on other sites

  • 4 weeks later...
  • 1 month later...
  • 1 month later...

Hello MRiza,

On 30/04/2017 at 6:52 AM, MRiza said:

I know this thread was old, but I want to build a web server that is using official package from the distribution repo. So I write this Dockerfile and if anyone wants to test/use it jus look at https://github.com/mriza/docker-processwire

I try to use your docker file (thanks a lot) with my Debian server but i met some difficulties : 

1 : I added a specific user

As root,

useradd -d /home/processwire -m -s /bin/bash processwire
usermod -aG docker processwire

2 : I built a Docker image

As processwire user,

Download the project files to /home/processwire: wget -c https://github.com/mriza/docker-processwire/archive/v0.2.tar.gz
Extract: tar xvzf v0.2.tar.gz
Change to the new directory: cd docker-processwire-v0.2/
Build the image : "docker build -t processwire ."

3 : I wrote my docker-compose file

vi /home/processwire/docker-compose.yml

#******************* docker-compose.yml file ************

processwire:
  image: processwire
  container_name: processwire
  restart: always
  volumes:
    - /home/processwire/docker-processwire-0.2/pw:/var/www/html
  ports:
    - "82:80"

#******************* docker-compose.yml file ************

 

4 : I ran Docker application

As processwire user and from /home/processwire, I ran "docker-compose up -d". Processwire website is now live on port 82, and I can access the server IP address (http://192.168.2.2:82 in my case). But when I try to install the "Site Installation Profile" (Default, intermediate edition) ,it fails (http://192.168.2.2:82/install.php) : 

File system is not writable by this installer. Before continuing, please rename '/site-default' to '/site'

I understand that a user in my Docker Application doesn't have write permissions for my external directory /home/processwire/docker-processwire-0.2/pw, but I don't see who...

 

Any ideas ?

Joël, first steps in the field of ProcessWire CMS

ps. Sorry for my bad english, I 'm French

 

Link to comment
Share on other sites

Extra informations :

root@CarExpresso:/home/processwire/docker-processwire-0.2# ls -la pw/

total 100
drwxr-xr-x 8 processwire processwire  4096 avril 30 06:36 .
drwxr-xr-x 3 processwire processwire  4096 mai   31 15:51 ..
-rw-r--r-- 1 processwire processwire   537 avril 30 06:36 composer.json
-rw-r--r-- 1 processwire processwire 12223 avril 30 06:36 .htaccess
-rw-r--r-- 1 processwire processwire  2421 avril 30 06:36 index.php
-rw-r--r-- 1 processwire processwire 47540 avril 30 06:36 install.php
drwxr-xr-x 6 processwire processwire  4096 avril 30 06:36 site-beginner
drwxr-xr-x 6 processwire processwire  4096 avril 30 06:36 site-blank
drwxr-xr-x 6 processwire processwire  4096 avril 30 06:36 site-classic
drwxr-xr-x 6 processwire processwire  4096 avril 30 06:36 site-default
drwxr-xr-x 6 processwire processwire  4096 avril 30 06:36 site-languages
drwxr-xr-x 5 processwire processwire  4096 avril 30 06:36 wire



 

Link to comment
Share on other sites

Help yourself... ;-)

1 - apache user id inside my container

docker exec processwire /bin/sh -c "id www-data"

# uid=33(www-data) gid=33(www-data) groups=33(www-data)

2 - I create my directory site (pw/site)

mkdir site
chown -R 33:33 site
mkdir modules
mkdir assets
chown -R 33:33 modules
chown -R 33:33 assets
cp site-default/config.php site/config.php
chown -R 33:33 site/config.php

cd site-default/
ls
assets      finished.php  install  ready.php
config.php  init.php      modules  templates

cd ..

cp site-default/finished.php site/
cp site-default/ready.php site/
cp site-default/init.php site/
cp -r site-default/install site/
cp -r site-default/templates site/

After that, everythink is ok with http://192.168.2.2:82/install.php

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...