Docker Wordpress



Setup

August 23, 2017

2 days ago  wordpress docker docker-compose continuous-integration github-actions. Follow asked 6 mins ago. Pathavyer Pathavyer. 1 1 1 bronze badge. New contributor. Pathavyer is a new contributor to this site. Take care in asking for clarification, commenting, and answering. The WordPress environment we’re setting up will consists of three Docker services: A service for providing and serving WordPress A service for the MySQL database. Sep 21, 2020 There is an official WordPress image for Docker. This image has a built-in webserver (Apache) and PHP. There is also a WordPress Docker-Compose example that you can use. All that is needed is a database server (MariaDB). WordPress with Docker Applications in containers run isolated from one another in the userspace of the host operating system sharing the kernel with other containers. This reduces the overhead required to run packaged software while also enabling the containers to run on any kind of infrastructure. Nov 07, 2019 Docker is a great containerization tool to experiment with WordPress. Its minimal environment helps you maintain the efficiency of your system resources. In this tutorial, you’ve learned how to install Docker on Linux, macOS, and Windows. You’ve also learned how to set up WordPress on Docker using the Docker Compose utility.

Disclaimer: Shortly after writing this, we decided to abandon Docker. The setup we have uses Nginx as the server, so it’s slightly more complicated. While I was able to get it running on my local machine using Docker Compose, I ran into problems when deploying it to a swarm with “docker stack deploy”. I think it was an internal Docker networking issue that was really difficult to troubleshoot. Also, earlier in the process, we couldn’t get one of our Macs to work with a development environment that worked fine on two other machines. This was extremely frustrating, since this is the kind of problem Docker is supposed to solve. For a more comprehensive list of Docker gripes, check out “Docker in Production: A History of Failure” (I especially liked the GIF comparison under Surviving Docker in Production)

If you want to set up a basic WordPress install using Docker, it’s pretty easy. It’s all laid out on the official Docker Hub WordPress page. But what about Multisite? No images, or even a mention about WPMS. After much weeping, wailing, and bashing of keyboards (…going through Bret Fisher’s excellent course on Udemy didn’t hurt either) I came up with a workable solution.

We are going to use the “wordpress:latest” image that uses Apache for simplicity here, but these techniques should work for the other variations too. I recommend going through this simple setup first before adding other factors, like Nginx, into the mix.

Create a Dockerfile

First, we’re going to create a Dockerfile for our custom WP image. Make sure to use the default name “Dockerfile” so docker-compose knows what to do.

The first line says we are inheriting everything from the “wordpress:latest” image. The next line puts the wp-config-sample.php file that we will modify in a safe place until the entrypoint script runs. If we tried to put it in /var/www/html it would get written over when WordPress is downloaded into that directory. The second COPY line puts our custom entrypoint script into the container. The ENTRYPOINT line runs our custom entrypoint script, which is just the default docker-entrypoint.sh script with a minor change we will make. And finally, the CMD line runs our Apache server.

Modify wp-config-sample.php

Add the following just above “/* That’s all, stop editing! Happy blogging. */” to a clean copy of wp-config-sample.php:

Create the Entrypoint Script

DockerDocker wordpress ftp

The default entrypoint script is located in the same directory as the Dockerfile on GitHub. Copy the contents of this file into a new one called wordpress-entrypoint.sh and add the following after line 52:

Create and run docker-compose.yml

Docker wordpress mysql

Setting up the volumes isn’t entirely necessary, but it’s nice to have them named something sensible like “wordpress” and “db-data” rather than a random string when you run a “docker volume ls” command like we will do later. Also, I created a mounted volume for our themes directory since this is likely what we’ll be modifying in a development environment.

A note about the wordpress environment section. I was getting an “Unbound Variable” error from WORDPRESS_TABLE_PREFIX until I explicitly set it here. I couldn’t see why I would need to set this but didn’t feel like hunting down the problem. If anyone knows why this is happening, let me know.

After saving your compose file, run:

Install WordPress

Go to http://localhost in a browser and you should see the familiar WordPress installation wizard.

If something goes wrong you can troubleshoot by viewing your wp-config.php file in the running wordpress container by entering: docker exec -it [your folder name]_wordpress_1 /bin/bash This should take you to a shell prompt in your container and you can view your file by running: cat wp-config.php. The first thing I would look for is to see if WP_DEBUG is set to ‘true’ instead of the default ‘false’ value.

Enter your language, username, password, etc. Log in after the installation is complete and enable Multisite by navigating to Tools > Network Setup. After clicking “Install”, WordPress will give you some instructions on how to enable the network by changing wp-config.php and .htaccess. This is what we will do next.

Modify wp-config-sample.php

Add the lines from the admin screen to wp-config-sample.php above the line reading /* That’s all, stop editing! Happy blogging. */ They should look something like this:

Create an .htaccess file

Copy the text from the admin into a file named .htaccess It should look like this:

Modify docker-compose.yml

Under the wordpress volumes section add the following line which will override the default .htaccess file with the new one.

Rebuild the WordPress Image

Now we have to clean up our docker environment by stopping the processes:

Removing our wordpress-multisite image:

Removing our wordpress volume: (you can get your volume’s name by running docker volume ls)

Now that everything has been cleaned up, we can re-run:

This will rebuild our WordPress image and volume with the updated wp-config.php file and our new .htaccess file.

Docker Wordpress

Bask in the Glory of WordPress Multisite using Docker (…see the disclaimer at top)

When you log back into your WordPress Admin, hover over “My Sites” in the top-left. If all has gone well, Network Admin will be the first link you see.

One of the most popular articles on my website is “Configure a local WordPress development on macOS from scratch”. I have recently used Docker a lot so I decided to tackle the same subject again but this time using containers.

This is not a Docker tutorial although by following along you learn how helpful this tool can be. I highly encourage you to familiarize yourself with few basic concepts like: images, containers, networks and volumes. Having a Docker app installed makes a lot of sense too. Regular readers know that I am a great friend with Homebrew. Yes, you can use it to download Docker too.

Wordpress + MySQL + phpMyAdmin #

To comfortably work with WordPress in a bare-bones local environment two components are required, although a third one is nice to have in some circumstances.

  1. MySQL or MariaDB database
  2. Database GUI like phpMyAdmin (optional)

Docker compose is a tool for creating multi-container Docker applications defined using single docker-compose.yml file (.yml and .yaml extension works just fine). Sounds like a fantastic method to connect our three building blocks together. I will do my best to provide helpful descriptions and comments to each of the core building blocks. Start by making a new directory for your website, create a docker-compose.yml in there and let’s finally get into the meat of this article.

That’s it — time to build our stack! Bear in mind that when you run it for a first time it is going to download all necessary stack images. Every subsequent invocation is going to be almost instant.

Voilà! #

Hopefully you found this helpful. This simple setup helps me a lot to spin up a new WordPress from scratch in absolutely no time.

More Videos For Docker Wordpress »

Ps. For simple websites like this one don’t use Wordpress. Use Hugo instead :)

Docker Wordpress Ssl

  • Hey - Thanks for the nice write, its been helpful. I was windows for a long time and using wamp for my wordpress sites and thinking to switch over to docker now. Wanted to know your experience with the docker in comparison to standalone php-apache-mysql setup. Which one do you use? and does each of your wordpress site have a separate docker-compose file? or there is only one docker-compose file you use and have all your sites inside it.

    • Docker all the things :)

      For each project I create new Docker compose file.
      Hopefully that helps.
      Have great day 🥑

  • On the screen where we have to put database details, what hostname should I put for mysql?

    • That should come preconfigured when you set it up like I did. The piece of config that decides about it is this:

      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress

      • Thanks for reply, I am asking about this screen. What information should I put here... https://uploads.disquscdn.c...

  • What do I do if the ports are already in use? I attempted to kill the ports used but was not able to, so I tried changing them in the config file but still couldn't get this to run.

  • Hi Pawel! Thanks for nice starting point to create WordPress in seconds but I have question.. I'm using PhpStorm and when it only showing all from `wp-content` directory PhpStorm does not hint the core functions because there are not in project. Do you know how to configure it to use the function hints?

    • Hi.

      I see what is going on. I am affraid that in this case you have to expose a project root level in your volume ['./:/var/www/html'] to make PhpStorm correctly infer WP core methods. I am not a PhpStorm user and potentially there is a much better way. Sorry for being not too helpful.

      Have a great day 🥑

  • Hi Pawel! Nice tutorial, thanks! Question though - how would you mount php.ini ? I'm using Ubuntu on WSL.

  • Great tutorial. However, I also need .htaccess file on my wp folder because some plugins and also WP itself is changing it. How to do that?

  • Hi Pawel, thanks a lot for this tutorial. I followed your steps, but I cant edit the files of the volume. They all have user `www-data` while my user is adam. File permission is 644, so I can't even edit them, if I add `www-data` group to `adam`. Any advice how to fix it? Also asked this here: https://stackoverflow.com/q...

Docker Wordpress Hub

What'ya think?