You need to have 🐳Docker installed. How you install docker heavily depends on your operating system.
🤔Choosing our image
A WordPress Docker image can be found here.
We will use this as our base image and extend it.
🚀building our image with docker-compose
Let’s first create a new Folder. For this tutorial,
I will create a folder in d:/dev/ called wordpress.
There we create a file called docker-compose.yml
Copy and paste this into the file:
🤔What is this docker-compose.yml file doing?
It will create two services. The wordpress service is our docker image with PHP and WordPress pre-installed. WordPress needs PHP to run. We tell docker that it should run on port 8080 and in the environment we are telling WordPress what db credentials it should use.
Our second service is a mysql server we call it db. WordPress needs a database to store data. In the environment we tell the database credentials should be. This has to be the same as in the wordpress service environment because WordPress needs these credentials to login and create the correct data for WordPress to run. The last environment is for good measure. It will create a random root user password.
Now we just need to run docker-compose up in our wordpress folder.
Docker will download the images and then try to start them. this can take several minutes. So be patient.
Now you just have to browse to http://localhost:8080 and you should see the WordPress prompt to set up a new instance.
🚀Mounting the docker folder
There is a problem with our current setup. When you want to install a WordPress plugin, WordPress will ask you to enter an FTP Login. We really don’t want that.
Another problem is how do you edit the WordPress config or themes or plugins?
We need to mount to change the wp-config.php file.
So how do we access it?
One way would be to just ssh into the container but that’s not what we will do here. We will mount the WordPress folder into our Host system.
First, we need to create an html folder in our wordpress folder. Second, we need to update our docker-compse.yml file.
This is how it should look now:
We just added two lines to our wordpress service.
The first line is volumes. Volumes are an easy way to work on files in a docker image like it would be local files on your system.
This line reads the following: Whatever is in /var/www/html folder in the docker container. Take that and make it visible and editable in D:/dev/wordpress/html.
Again D:/dev/wordpress/html is something that works for me. You can put that wherever you want.
Now just stop the docker-compose process by pressing ctrl+c in your terminal.
and run it again.
When you now navigate to d:/dev/wordpress/html you should see the WordPress files. Just open wp-config.php and add the following line before the define( ‘DB_NAME’, ‘exampledb’);
/* Make WordPress install plugins directly */
Also with this method, it is now easy to edit WordPress themes and plugins. They are located in the wp-content folder. Isn’t that nice?
🚀Installing php extensions needed by some WordPress plugins
Some WordPress plugins need PHP extensions. the base wordpress image has already some installed but maybe you need more. For this, we need to create our own docker image. Here we will install the memcached PHP extension.
Create a new file called Dockerfile in the wordpress folder. Add this to it and save:
RUN apt-get update
RUN apt-get install -y libz-dev libmemcached-dev && \
pecl install memcached && \
In the FROM line we tell docker to base our image on the wordpress image we used before.
The first RUN simple says that we want to update the packages of the Linux distro we are using. Until here it should be the same for every extension.
The second RUN command is specific to memcached. It installs the needed Linux packages and then enables the PHP extension. If you need another extension a simple google search for that extension should do the trick, probably people already at some point installed this extension with docker and have figured out what Linux packages are needed.
Okay we just need to build the new package with:
docker build -t wordpress_with_extra_extension .
Yeahyyyy, you just build a new docker image! cool right?
Now we just need to tell our docker-compose.yml file to use that image:
That is it! Now just stop and run docker-compose up and your good to go!
Thanks for reading!
Say Hallo! Instagram | Twitter | LinkedIn | Medium