Docker - wordpress all-in-one container on CentOS 7

Posted on Tue 18 October 2016 by Pavlo Khmel

This example shows LAMP + Wordpress all in one docker container. This setup is not going to scale. And all-in-one makes it easy to backup and restore. CentOS 7 container with enabled systemd makes it easy to implement this.

Note: this example stores all data inside writable container layer. All data will be lost when you delete container. Recommended way is to store data on persistent volumes.

0. Install docker

yum -y install epel-release
yum -y install docker-io
systemctl enable docker
systemctl start docker

Change these in your setup:

  • DB_NAME: wordpress
  • DB_USER: wordpress
  • PASSWORD: password

1. Create CentOS 7 container with enabled systemd:

mkdir docker-c7-systemd
cd docker-c7-systemd

Create file Dockerfile:

FROM centos:7
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done);   
rm -f /lib/systemd/system/multi-user.target.wants/*;  
rm -f /etc/systemd/system/*.wants/*;  
rm -f /lib/systemd/system/local-fs.target.wants/*;   
rm -f /lib/systemd/system/sockets.target.wants/*udev*;   
rm -f /lib/systemd/system/sockets.target.wants/*initctl*;   
rm -f /lib/systemd/system/basic.target.wants/*;  
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]

Run:

docker build --rm -t local/c7-systemd .

2. Create wordpress container

cd ..
mkdir docker-c7-wordpress
cd docker-c7-wordpress

Create file Dockerfile:

FROM local/c7-systemd
# Install packages
RUN yum -y install httpd
RUN yum -y install mariadb-server mariadb
RUN yum -y install php php-mysql php-gd
RUN yum clean all
# Enable services
RUN systemctl enable httpd.service
RUN systemctl enable mariadb.service
# Install Wordpress
RUN cd /var/www/html/; curl -LO http://wordpress.org/latest.tar.gz
RUN cd /var/www/html/; tar xzf latest.tar.gz
RUN chown -R apache:apache /var/www/html/*
RUN cd /var/www/html/wordpress; cp wp-config-sample.php wp-config.php
# Set database, user and password
RUN sed -i 's/database_name_here/wordpress/' /var/www/html/wordpress/wp-config.php
RUN sed -i 's/username_here/wordpress/' /var/www/html/wordpress/wp-config.php
RUN sed -i 's/password_here/password/' /var/www/html/wordpress/wp-config.php
# make wordpress as DocumentRoot
RUN sed -i 's/\/var\/www\/html/\/var\/www\/html\/wordpress/' /etc/httpd/conf/httpd.conf
EXPOSE 80

docker build --rm -t local/wordpress .

Run container to make last changes:

docker run --restart=always --privileged --name wordpress -td -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 80:80 local/wordpress
docker exec -it wordpress /bin/bash

Check status:

docker ps -a

MariaDB changes:

mysql -u root -e "CREATE DATABASE wordpress;"
mysql -u root -e "CREATE USER wordpress@localhost IDENTIFIED BY 'password';"
mysql -u root -e "GRANT ALL PRIVILEGES ON wordpress.* TO wordpress@localhost IDENTIFIED BY 'password';"
mysql -u root -e "FLUSH PRIVILEGES;"
exit

Finish Wordpress configuration via web: http://[server-address]/wordpress/

3. How to stop and start container

For maintenance purpose you will need to stop and start container.

Command docker stop will not work with systemd enabled container. It will send SIGKILL signal after 10 sec. That is not desirable for database.

Use internal shutdown command instead:

docker exec -ti wordpress /usr/sbin/shutdown -h now

Start container:

docker start wordpress