DEV Community

Cover image for How to get WordPress running with Docker
Michael "lampe" Lazarski
Michael "lampe" Lazarski

Posted on

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

Top comments (10)

Collapse
 
devellopah profile image
Islam Ibakaev • Edited

Beatiful article, dude. Btw, wordpress image volume can be shorten to

 volumes:
   - .:/var/www/html
Collapse
 
lampewebdev profile image
Michael "lampe" Lazarski

Thanks, dude :)

Yeah, it could! Thanks :)

Collapse
 
kyletozer profile image
Kyle Tozer

I'm finding that I am not as easily able to edit the files in my bind mount on the host as you are. After running docker-compose up, I try to make changes to my site files and keep getting permissions errors in my IDE, is there a way around this? My docker-compose.yml file looks almost exactly like yours. Thanks!

Collapse
 
juslintek profile image
Linas

I would not suggest it that way. Its better to isolate http,php layers. Now they are cramed up in a single docker layer named wordpress. Why is that because you can test performance with different http services at the same time. As well usually you'd know which layer ir responsible for server side problems. Am I wrong?

Collapse
 
lampewebdev profile image
Michael "lampe" Lazarski

I don't fully understand what you mean.

This is based on the offical WordPress docker image.

Also it is a setup for development.

Why should I at this point care about the http performance?

If I want to have something that scales I can just spin up multiple instances of the docker image.

You get the usual logs as with every service so you could just look into the logs.

For me this is the wrong point in time to think about http performance but we can have a talk about that.

Collapse
 
venelinn profile image
Venelin

Nice article!
But you can also install Lando and use Wordpress recipe. I guess the performance should be the same since Lando uses Docker !

Collapse
 
shry profile image
shry

I have developed an open-source project called wordup on GitHub,
which is a development tool only for WordPress.
You can also setup a docker-compose based stack in a matter of seconds, export your project and much more.

Collapse
 
lampewebdev profile image
Michael "lampe" Lazarski

Didn't know about Lando. I will have a look at it.

Thanks for the great comment.

Have a fantastic day.

Collapse
 
dendihandian profile image
Dendi Handian

Everyone should use bedrock version of Wordpress. It's development-friendly.

Collapse
 
vanaf1979 profile image
Stephan Nijman

This may be of interest to you! Gutenberg Local Environment Rewrite make.wordpress.org/core/2019/08/30...