Dockerising a Contao website
This article is based on Contao 3. There is a new version, see Dockerising Contao 4
I’m a fan of containerisation! It feels much cleaner and systems don’t age that quickly.
Latest project that I am supposed to maintain is a new Contao website. The company who built the website of course just delivered files and a database. The files contain the Contao installation next to Contao extensions next to configuration and customised themes.. All merged into a blob… Thus, in the files it is hard to distinguish between Contao-based files and user generated content. So I needed to study Contao’s documentation and reinstall the website to learn what files should go into the Docker image and which files to store outside.
However, I finally came up with a solution that is based on two Contao images :)
A general Contao image
PLEASE NOTE: sSMTP is not maintained anymore! Please swith to
msmtp
, for example, as I explained in Migrating from sSMTP to msmtp.
The general Contao image is supposed to contain a plain Conato installation. That is, the recipe just installs dependencies (such as curl, zip, and ssmtp) and downloads and extracts Contao’s sources. The Dockerfile looks like this:
The first block apt-get install
s necessary stuff from the Debian repositories.
The second block downloads a Contao 3.5 from https://download.contao.org/3.5/zip
, extracts it to /var/www/
, and links /var/www/html
to it.
It also creates the cron.txt
(see github.com/contao/core/pull/8838).
The third block installs a few required and/or useful PHP extensions.
And finally the fourth block retrieves and installs Composer to /var/www/html/composer
, where the Contao-composer-plugin expects it.
That’s already it! We have a recipe to create a general Docker image for Contao. Quickly setup an automatic build and .. thada .. available as binfalse/contao
.
A personalised Contao image
Besides the plain Contao installation, a Contao website typically also contains a number of extensions.
Those are installed through composer, and they can always be reinstalled.
As we do not want to install a load of plugins everytime a new container is started we create a personalised Contao image.
All you need is the composer.json
that contains the information on which extensions and which versions to install.
This json should be copied to /var/www/html/composer/composer.json
, before composer can be run to install the stuff.
Here is an example of such a Dockerfile:
This image can then be build using:
The resulting image tagged contao-personalised
will contain all extensions required for your website.
Thus, it is highly project specific and shouldn’t be shared..
How to use the personalised Contao image
The usage is basically very simple. You just need to mount a few things inside the container:
/var/www/html/files/
should contain files that you uploaded etc./var/www/html/templates/
may contain your customised layout./var/www/html/system/config/FILE.php
should contain some configuration files. This may include thelocalconfig.php
or apathconfig.php
.
Optionally you can link a MariaDB for the database.
Tying it all together using Docker-Compose
Probably the best way to orchestrate the containers is using Docker-Compose.
Here is an example docker-compose.yml
:
This assumes that your personalised Dockerfile is located in path/to/personalised/Dockerfile
and your website files are stored in $PATH/files
, $PATH/templates
, and $PATH/system/config/localconfig.php
.
Docker-Compose will then build the personalised image (if necessary) and create 2 containers:
contao
based on this image, all user-based files are mounted into the proper locationscontao_db
a MariaDB to provide a MySQL server
To make Contao speak to the MariaDB server you need to configure the database connection in $PATH/system/config/localconfig.php
just like:
Here, the database should be accessible at contao_db:3306
, as it is setup in the compose file above.
If you’re running contao with “Rewrite URLs” using an .htaccess you also need to update Apache’s configuration to allow for rewrites.
Thus, you may for example mount the follwoing file to /etc/apache2/sites-available/000-default.conf
:
This tells Apache to allow everything in any .htaccess file in /var/www.
When everything is up running the Conato install will be available at port 8080
(see ports
definition in the compose file) of the machine hosting the Docker containers.
Mail support
PLEASE NOTE: sSMTP is not maintained anymore! Please swith to
msmtp
, for example, as I explained in Migrating from sSMTP to msmtp.
The image above comes with sSMTP installed. If you need support for email with your Contao installation, you just need to mount two more files into the container:
Tell PHP to mail through sSMTP
The following file tells PHP to use the ssmtp
binary for mailing. Just mount the file to /usr/local/etc/php/conf.d/mail.ini
:
Configure sSMTP
PLEASE NOTE: sSMTP is not maintained anymore! Please swith to
msmtp
, for example, as I explained in Migrating from sSMTP to msmtp.
The sSMTP configuration is very easy. The following few lines may already be sufficient, when mounted to /etc/ssmtp/ssmtp.conf
:
For more information read Mail support for Docker’s php:fpm and the Arch Linux wiki on sSMTP or the Debian wiki on sSMTP.
- network (68) ,
- software (160) ,
- university (46) ,
- website (22) ,
- administration (43) ,
- web (86) ,
- php (16) ,
- programming (16)
- apache (15) ,
- config (21) ,
- explained (43) ,
- contao (4) ,
- docker (17) ,
- mail (11) ,
- network (81) ,
- university (42) ,
- ssmtp (5)
Leave a comment
There are multiple options to leave a comment: