WordPress and docker – Using phpmyadmin with docker and wordpress and mysql

This is a bonus post. I got curious about running a phpmyadmin instance and realised it was easy to get running.

If you’ve gone through my previous posts:

Or perhaps you already have an instance of wordpress running and a database that has persistence set up. Now you want to add a mysql administration tool into the mix.

For this I did not go out of my way to write a full docker-compose.yml file. I only wanted a phpmyadmin instance running locally for testing any wordpress development.

To do this I ran:

1
2
3
4
$ docker run
    --name myadmin
    --network dockerwordpress_default -d
    --link dockerwordpress_db_1:db -p 8080:80 phpmyadmin/phpmyadmin

The steps involved

The important part is making sure you link to your db and that you run the phpmyadmin container on the same network as the db.

First see what the name of your db container is:

1
$ docker ps

You should something like this:

1
2
3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
81ba5a434e49 wordpress:latest "docker-entrypoint..." 23 hours ago Up 23 hours 0.0.0.0:8000->80/tcp dockerwordpress_wordpress_1
549f46a13b6b mysql:5.7 "docker-entrypoint..." 23 hours ago Up 23 hours 3306/tcp dockerwordpress_db_1

Your mysql container is the important one for this post. Copy the name, in my case it is “dockerwordpress_db_1”.

Check your networks:

1
$ docker network ls

You should see something like this:

1
2
3
4
5
NETWORK ID NAME DRIVER SCOPE
1db6773f4c13 bridge bridge local
938a9daa793d dockerwordpress_default bridge local
d8e0c5970cdb host host local
40e39b778c65 none null local

In my case I would use “dockerwordpress_default” as the network for my phpmyadmin container.

Using docker run

Now you know enough to run the command that I showed at the start of the post:

1
2
3
4
$ docker run
    --name myadmin
    --network dockerwordpress_default -d
    --link dockerwordpress_db_1:db -p 8080:80 phpmyadmin/phpmyadmin

Explanations of the important parts:

–link dockerwordpress_db_1:db

dockerwordpress_db_1:db this is pointing to the network “dockerwordpress_db_1” and then linking to the mysql database service which you would have started in this post: Running wordpress and mysql using docker compose

–network dockerwordpress_default

dockerwordpress_default You got this when you ran docker network ls above.

Viewing phpmyadmin

If your phpmyadmin container started up successfully you should now be able to connect to it on port 8080.

If your docker is linked to your localhost:

http://localhost:8080

Alternativelyl find your ip by running:

1
$ docker-machine ip

Then

http://yourdckerip:8080

Login to phpmyadmin using the details you used in the docker configurations:

Eg

1
2
MYSQL_USER=wordpress
MYSQL_PASSWORD=wordpress

WordPress and docker – Developing wordpress themes using docker volumes

As promised, here is part 2 of my docker & wordpress posts. Here is how you could develop wordpress themes using docker.

I’m going to quickly run through how to use docker to do wordpress theme development and not loose any changes when you stop docker of switch off your computer.

In order to make sure data persists even when your docker container is no longer running, you need to set up a volume.

You can read the official docker docs here: https://docs.docker.com/engine/admin/volumes/volumes/

I tweaked the docker-compose file below to map the container’s “wp-content” folder to your current working directory you have your docker-compose.yml file.

version: '3'

services:
   db:
     image: mysql:5.7
     volumes:
       - ./db_data:/var/lib/mysql
     restart: always
     env_file: 
       - ./.env

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     volumes:
       - ./wp-content:/var/www/html/wp-content
     ports:
       - "8000:80"
     restart: always
     env_file: 
       - .env

volumes:
    db_data:
    wp-content:

You can view your wordpress site in your browser by going to:

1
http://localhost:8000

or if you need to use an ip check your docker-machine ip by running:

1
$ docker-machine ip

See the previous post for how to set up the .env file.

You should see two directories listed in the same directory you have your docker-compose.yml file in.

1
2
db_data
wp-content

This is not 100% ideal. For simplicity I’ve set up these folders in the same directory. I will explain in more detail why at the end of the post.

Creating your wordpress theme with docker volume

Now that you have a persistent volume set up you can start to tweak the theme.

You will find the themes in the ./wp-content/themes folder.

In the admin (go to http://localhost:8000/wp-admin, or http://yourmachineip:8000/wp-admin) you will see listed the themes in the Appearance – Themes section.

You can easily delete themes you don’t want, and refresh and see they will no longer be listed in the Themes section in the admin.

You can add new themes and make changes to existing themes and just refresh and you will see the changes in the browser.

A better way to handle volumes:

So I mentioned above, you would not want your volumes necessarily within the same folder as your container files that you have now containerized using docker.

For one, you will want to make your final work into an image with a tag and run the image on a production ready server.

Docker-compose is for development, and should not be your final go live strategy.

Your volumes you should also either point to another container or a safe secure place on your computer / server.

If we’re thinking big picture, and thinking about the deployment part. You will have your docker-compose files separate from your projects, but maybe for simplicity sake you decide to keep them with your project files, that’s fine, but make sure you consciously think about why you want them there, what are the benefits of that structure (I’m just hypothetically asking)?

You should always have your volumes in a safe place, perhaps a dedicated server space that has recovery tactics in place like regular backups, mirrored, clustered. There are many ways to tackle secure, fail safe voluming. Make sure if you are planning a project that is for a client project that will go into the world, that you have planned ahead the entire deployment ecosystem.

A badly managed volume becomes a single point of failure, and one of the main 101s of cloud infrastructure and proper devops thinking should be to remove as many single points of failure as possible.

For the purpose of this blog post though, I’ve kept all of that out. My aim is to allow you to test the concept of wordpres and voluming with as little effort as possible.

I will create a follow up post on how to create a docker image of your code, push your tweaked code into an image, and then you can run from a fully packaged image rather than from a folder.

You’re now dabbling in the realm of containers, so you should be thinking in “image” and moving away from the thought process of “I have x amount of files to push to a server”, but like I say, I’ll touch on that in a follow up post.

Enjoy 🙂 May you experience lots of success in your containerizing.

Just a note on some struggles I had:

In order to test the volumes for this post, I delete the volumes a lot and restarted my computer. I shut down the containers and started them. I really tried to break the volumes. For the most part the volumes persisted well, I had to purposely delete the volume and all the files in order to stop it from persisting.

While I did all that my environment started to lag and I noticed the wordpress container sometimes started up before the database container, and then if I tried to load the wordpress site it did not show up. Since the wordpress container ran before the database was properly configured, I had to rerun the wordpress container to get it to connect with the database container again.

 

Docker and WordPress: Running wordpress and mysql using docker compose

Curious about how to work with docker and wordpress? Here is a quick walk through on how to use docker to run wordpress and mysql using docker compose.

In the next post I’ll show how you can can volume the themes and edit themes in your development process. And in a follow up post I will show how to run your own created wordpress image in kubernetes and in minikube. 🙂

You can get a fairly generic yml file from the official docker website:

https://docs.docker.com/compose/wordpress/

I took the configurations and made a few small tweaks:

docker-compose.yml

version: '3'

services:
   db:
     image: mysql:5.7
     volumes:
       - ./db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: mysql_root_password
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
     env_file: ./.env

volumes:
    db_data:

The above configuration will set up wordpress and mysql with the variable values in the “environment” sections. If will map wordpress from the interal port 80 to external port 8000. It also creates a volume mount from the internal location of  “/var/lib/mysql” to the external location of “./db_data”. This will help with persistent data storage (you won’t loose your data if your container stops).

By “internal” I mean within the container’s environment and by “external” I mean the environment running the docker container, in my case this would be my mac.

Then run the docker compose script:

1
$ docker-compose up -d

Check the docker images are running:

1
$ docker ps

You can then view the wordpress install by checking your docker ip and then pointing to the external port (in this case it will be 8000).

Get your docker’s ip:

1
$ docker-machine ip

The first time you run this, you will be asked to fill in some details about your wordpress blog. The next time you run it, it should all be prepopulated (provided you haven’t deleted your “db_data” folder).

You can also test that the data is persistent by delete the docker containers and images and then downloading the images again and running the docker-compose script. If everything starts up the same way you left it, then your persistent data is working.

I made improvements to the script by pulling out the environment variables into a .env file. This will help make the containers for customizable. I’ve commented out the environment variables in the docker-compose file so you can see what they were.

docker-compose.yaml (v2)

version: '3'

services:
   db:
     image: mysql:5.7
     volumes:
       - ./db_data:/var/lib/mysql
     restart: always
     env_file: ./.env

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     env_file: ./.env

volumes:
    db_data:

Your .env file:

MYSQL_ROOT_PASSWORD=mysql_root_password
MYSQL_DATABASE=wordpress
MYSQL_USER=wordpress
MYSQL_PASSWORD=wordpress

WORDPRESS_DB_HOST=db:3306
WORDPRESS_DB_USER=wordpress
WORDPRESS_DB_PASSWORD=wordpress

To test

Check if your .env variables imported correctly by running the docker exec command:

1
docker exec -it CONTANER-ID bash

Then inside the container you can run:

1
echo $WORDPRESS_DB_USER

If that returns just an empty line, you need to just check the env_file settings and the contents of the file.

Check your container id by running:

1
docker ps

Some useful scripts

Stopping all containers (this will stop ALL, so use with caution):

1
$ docker stop $(docker ps -aq)

Removing all containers (use with caution, this will remove all containers):

1
$ docker rm $(docker ps -aq)

Deleting all images (use with caution! this will remove all images):

1
$ docker rmi $(docker images -q)

Github

View this code on github:
https://github.com/CariZa/DockerComposeWordpress

Use the command line tool to scaffold a wordpress site on windows

Pre-requirements

Make sure you have apache and phpmyadmin installed. You can download and install xampp here, which will provide you with both and more.

https://www.apachefriends.org/download.html

You will also need an internet connection.

This is easier if you use Linux, but if you prefer using Windows here is a quick guide.

Get wp-cli.phar

Follow the online instructions to get wp-cli.phar:

http://wp-cli.org/

If that doesn’t help try these alternative install methods:

https://github.com/wp-cli/wp-cli/wiki/Alternative-Install-Methods

Add the wp-cli.phar file to your system. I have a bin folder on Windows that I add any .phar files to.

C:\bin\wp-cli.phar

To run the wp-cli.phar on windows you will need to use the following command:

php [filepath]\wp-cli.phar

With [filepath]\ being the path to the location of your wp-cli.phar file.

Eg:

php C:\bin\wp-cli.phar

Check that the wp-cli.phar is working:

Run

php C:\bin\wp-cli.phar –info

Add a database

Go to your phpmyadmin, and add a blank database for your new wordpress to use. Typically your phpmyadmin will be here: http://localhost/phpmyadmin.

Just click on the “Databases” tab and create a new database called wp-test.

Add the mysql.exe filepath to your environment variables so that the command line can run mysql. You also need to have the php.exe filepath in your evironment variables;

Eg I added the below to my PATH variable in my evironment variables:

mysql

C:\xampp\mysql\bin;

php

C:\xampp\php;

Remember to separate each file path with a semicolon ;.

Read this post if you need to learn how to add to your environment variables.

Change to your wordpress directory

Go to the directory you want to add wordpress to. This will need to be on a server like apache. If you have xampp installed add wordpress in the htdocs folder.

Eg

cd C:\xampp\htdocs\wordpress\

This can be in any directory you like, just create the directory you want to install in and make sure you are in that directory in your command line.

Download wordpress using the command line

Step 1. Now that you have wp-cli.phar working download wordpress.

Run

php C:\bin\wp-cli.phar core download

The bonus of this approach is if you have already downloaded wordpress it will be cached on your system and the system will use that cached copy to install. You do not need to download it every time.

Setup the wp-config.php

Step 2. Every wordpress installation needs a wp-config.php file. Run the below to create the file.

Run

php C:\bin\wp-cli.phar core config –dbname=[db name] –dbuser=[db user] –dbpass=[db pass]

Eg

php C:\bin\wp-cli.phar core config –dbname=wp-test –dbuser=root

I have left out the –dbpass as I don’t have a password. By default in phpmyadmin the user name will be root and the password blank.

Getting an error?

If you get this error, you need to check that you have the mysql.exe location in your environment variables.

Error:
‘mysql’ is not recognized as an internal or external command, operable program or batch file.

You need to open a new command line instance in order to use the environment variable. You won’t be able use the mysql path reference in the same command line window.

Set wordpress up

Step 3. This is where you add in your admin user details and database details.

Run

php C:\bin\wp-cli.phar core install –title=’Test’ –admin_user=[your user name] –admin_password=[your password] –admin_email=[your email]  –url=[your wordpress url]

Eg

php C:\bin\wp-cli.phar core install –title=’Test’ –admin_user=admin –admin_password=securepassword –[email protected]  –url=http://localhost/wordpress/

Done.

You now have the ability to get wordpress up and running through the command line.