How to get WordPress running with Docker

🚀Prerequirments

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:
version: ‘3.1’

services:

wordpress:
image: wordpress
restart: always
ports:
– 8080:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb

db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: exampledb
MYSQL_USER: exampleuser
MYSQL_PASSWORD: examplepass
MYSQL_RANDOM_ROOT_PASSWORD: ‘1’

🤔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-compose up

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:
wordpress:
image: wordpress
restart: always
ports:
– 8080:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
volumes:
– “D:/dev/wordpress/html:/var/www/html"

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.
– "D:/dev/wordpress/html:/var/www/html"

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.
docker-compose up

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 */
define(‘FS_METHOD’, ‘direct’);

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:
FROM wordpress
RUN apt-get update
RUN apt-get install -y libz-dev libmemcached-dev && \
pecl install memcached && \
docker-php-ext-enable 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:
From

wordpress:
image: wordpress

to

wordpress:
image: wordpress_with_extra_extension

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

Link: https://dev.to/lampewebdev/how-to-get-wordpress-running-with-docker-4mg6