top of page

WordPress and Docker

Writer's picture: kyle Haileykyle Hailey

One of the attractive things about Docker to me, is that I can spin up containers with software that doesn’t affect the rest of my system. I can spin down the container, remove the container, make multiple containers. It’s all nice neat and clean unlike traditional application installation on an OS where files are spewed all through the file system and it’s a  mess to try and clean it it up and/or remove it if need be. Sort of like the different between brain surgery and removing or putting on a piece of clothing.

In the last couple of posts I’ve talked about using WordPress with Delphix and Docker. One of my desired use cases was to be able to spin up multiple wordpress containers as in


Screen Shot 2015-12-02 at 3.37.42 PM

The above diagram shows Delphix but that’s beside the point. The point is that I want, and should be able to, run multiple docker wordpress container. In actuality I never got this working correctly. My problem was running multiple wordpress docker containers at the same time. Actually running them was fine. The problem was accessing the websites for each of these wordpress containers. There are multiple wordpress docker images available. There is the official wordpress docker image and then there are a number of other popular images. The two other I tried were “tutum/wordpress” and “centurylink/wordpress”.

Official WordPress Docker image

Then start docker and download the “wordpress” container


service docker start
docker pull wordpress

Start docker wordpress container


docker run -p 80:80 --name wordpress1  \
           -e WORDPRESS_DB_HOST=172.16.160.160:3306 \
           -e WORDPRESS_DB_USER=wordpressuser \
           -e WORDPRESS_DB_PASSWORD=password \
           -d wordpress

Start a second Docker WordPress container, using a different MySQL database and mapping port 80 to port 81

docker run -p 81:80 --name wordpress2 \
           -e WORDPRESS_DB_HOST=172.16.160.160:3307 \
           -e WORDPRESS_DB_USER=wordpressuser \
           -e WORDPRESS_DB_PASSWORD=password \
           -d wordpress

And accessing each of these actually works. Just go to the host URL for the first one and go to the host URL:81 for the second.  Wordpress by default is set up on port 80 which is the default port so to access wordpress all you have to do is give the host name as the URL. In the case of the second WordPress container, we now access WordPress on port 81.

The problem is that after we access the second wordpress website via port 81, the URL gets re-written without the port and thus we start accessing the first wordpress container.

I tried to turn off URL re-writting by running

a2dismod rewrite

and bouncing apache

/etc/init.d/apache2 start
/etc/init.d/apache2 stop

an editing the file


/var/www/html/.htaccess

to have “RewriteEngine Off” but no dice. The URLs kept getting rewritten, thus I decided to try to use a different wordpress docker container image,

WordPress image from tutum

docker pull tutum/wordpress
docker run -p 80:80 --name wordpress1 \
           -e WORDPRESS_DB_HOST=172.16.160.161:3307 \
           -e WORDPRESS_DB_USER=wordpressuser \
           -e WORDPRESS_DB_PASSWORD=password \
           -d tutum/wordpress

but this seems to create a MySQL database itself instead of ysin gthe one I specified. Looking at the run.sh on github, https://github.com/tutumcloud/lamp/blob/master/run.sh, it looks like it’s hard coded to look for MySQL datafiles on location, so this is not going to work with multiple virtual databases.

WordPress image from centurylink

start first container

docker run -p 80:80 --name wordpress1 \
           -e DB_PASSWORD=delphixdb \
           -e DB_1_PORT_3306_TCP_ADDR=172.16.160.161 \
           -e DB_1_PORT_3306_TCP_PORT=3307  \
           -d centurylink/wordpress

but it couldn’t connect to my MySQL database.  To see the errors you can run

docker logs wordpress1

The default user for MySQL in this image is root and root requires some extra set up. Trying to access MySQL directly as root gives an error


mysql -uroot -pdelphixdb -h172.16.160.161 -P3307
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'linuxtarget' (using password: YES)

To allow root access requires connect to MySQL via the socket file:


mysql -u root -pdelphixdb  --socket=/u01/app/toolkit/provision/V/u02/app/mysql/data-NWO/temp/mysql.sock

To find the socket file, go to the location that Delphix mounts the datafiles onto the host and searching for mysql.sock. After connecting, then tell the MySQL database to allow socketless root connections


GRANT SUPER ON *.* TO 'root'@'%' IDENTIFIED BY 'delphixdb';
GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY 'delphixdb';
flush privileges

Now I can start two wordpress containers

docker run -p 80:80 --name wordpress1 \
           -e DB_PASSWORD=delphixdb \
           -e DB_1_PORT_3306_TCP_ADDR=172.16.160.161 \
           -e DB_1_PORT_3306_TCP_PORT=3306  \
           -d centurylink/wordpress

docker run -p 81:80 --name wordpress2 \
           -e DB_PASSWORD=delphixdb \
           -e DB_1_PORT_3306_TCP_ADDR=172.16.160.161 \
           -e DB_1_PORT_3306_TCP_PORT=3307  \
           -d centurylink/wordpress

The problem now is that when I access the wordpress website, the page comes out blank with no errors. Haven’t tracked down why this isn’t work.

Summary

At this point I haven’t managed to create multiple wordpress containers and clearly access each website separately.

Next step is to probably create my own wordpress docker image set up like the official wordpress docker image but without URL rewrite. TBA

A few useful commands I ran into along the way

# docker ps
CONTAINER ID IMAGE  COMMAND CREATED  STATUS PORTS NAMES
113559099a60 wordpress:latest "/entrypoint.sh apac  6 seconds ago  Up 4 seconds 0.0.0.0:80->80/tcp  wordpress1

list the running images. From this list I can stop them, remove them, see logs from them etc. For example

# docker logs wordpress1
WordPress not found in /var/www/html - copying now...
WARNING: /var/www/html is not empty - press Ctrl+C now if this is an error!
+ ls -A
index.html
+ sleep 10
Complete! WordPress has been successfully copied to /var/www/html
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'wordpressuser'@'172.16.160.160' (using password: YES) in - on line 10

stop/start/delete

docker stop wordpress1
docker start wordpress1
docker stop wordpress1
docker rm wordpress1

getting a shell into one of the containers

docker exec -i -t wordpress1 bash

get a list of downloaded images

# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
wordpress               latest              9909dec6d65f        9 days ago          514.8 MB
tutum/wordpress         latest              5025a6da41dd        4 months ago        493.6 MB
centurylink/wordpress   latest              91f5520cafc8        7 months ago        520.1 MB

get details on a container (output is truncated as it is long)

# docker inspect wordpress1
[{
    "AppArmorProfile": "",
    "Args": [
        "apache2-foreground"
    ],
    "Config": {
        "AttachStderr": false,
        "AttachStdin": false,
        "AttachStdout": false,
        "Cmd": [
            "apache2-foreground"
        ],
        "CpuShares": 0,
        "Cpuset": "",
        "Domainname": "",
        "Entrypoint": [
            "/entrypoint.sh"
        ],
        "Env": [
            "WORDPRESS_DB_HOST=172.16.160.160:3307",

Mapping a local directory into a container with “-v” option which makes sharing files with the container easier and allows for persistent storage across container creation and deletion

# docker run -p 3142:80 --name wordpress1 -v /tmp/wordpress:/tmp/container/wordpress  -e WORDPRESS_DB_HOST=172.16.160.161:3306 -e WORDPRESS_DB_USER=wordpressuser -e WORDPRESS_DB_PASSWORD=password -d wordpress

and this will map local host directory /tmp/wordpress to the container directory /tmp/container/wordpress. Mapping can be limited to a single file and constrained to read only instead of the default read/write.

ATTENTION: another useful bit of knowledge is changing the wordpress siterul in the new container:

mysql -u wordpressuser -ppassword -h 172.16.160.161 -P 3306 wordpress << EOF
update wp_options set option_value=’http://172.16.160.161′ where option_id<3;
select option_value,option_id from wp_options where option_id < 4;
EOF

1 view0 comments

Recent Posts

See All

Kyle Hailey

  • Facebook
  • Twitter
  • LinkedIn
  • Instagram

San Franisco, Ca 94131

415-341-3430  (please text initially before calling)

bottom of page