How to use Docker for easy and fast WordPress development
Francesco Napoletano

Francesco Napoletano @napolux

About: Software Engineer, husband, programmer, videogames player, technical writer. Opinions expressed here are my own.

Location:
Milan, Italy
Joined:
Jun 17, 2017

How to use Docker for easy and fast WordPress development

Publish Date: Jul 18 '18
78 14

A while ago I used to do my WordPress development using this "vagrant-based" environment. It's very well written, opensource and comes with whatever you may need to create themes and plugins for WordPress.

But I wanted something lighter.

I needed a superlight WordPress installation, with no hassle and a very fast deployment. Docker Compose was the right choice.

  • Containers are lighter than a Virtual Machine
  • Containers are isolated but share the same OS and libraries (if needed)
  • If needed, the same docker-compose.yml file can be used for development and production, making environments compatible at a higher level.

So, here's my docker-compose.yml file, for you to use.

version: '3'
services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: password
       MYSQL_DATABASE: wordpress
       MYSQL_USER: user
       MYSQL_PASSWORD: password
   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     volumes:
       - ./wp-content/:/var/www/html/wp-content 
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: user
       WORDPRESS_DB_PASSWORD: password
volumes:
    db_data:
Enter fullscreen mode Exit fullscreen mode

You can also clone it from GitHub, if you want.

Let's dig into this a bit...

First line: we're telling docker which syntax version we're going to use in the rest of the file. You can read more about the different versions on the docs.

Then in the services section we're telling the system we need these images:

  • MySQL version 5.7, with some "strong" credentials :P
  • WordPress, latest version

For WordPress, we're telling the WordPress image to map the port 80 to our 8000, and to map the local wp-content folder to the same folder in the WordPress installation.

We're going to put all our code in that folder, in order to make it available in the WordPress admin panel (think of themes and plugins).

So, to be up and running all you have to do is:

Open your browser. go to http://localhost:8000 and you're good to go!

Easy, isn't it?

Originally published @ https://coding.napolux.com

Comments 14 total

  • MarvelousWololo
    MarvelousWololoOct 6, 2018

    what about user permissions for the local /wp-content/?

    • Lucas Fernandes
      Lucas FernandesMar 30, 2019

      Would like to know how to solve it

      • Francesco Napoletano
        Francesco NapoletanoApr 15, 2019

        Well, if you have problems, go with 777 and be happy :P

        I don't have any problem, my folder is at 644

        • MarvelousWololo
          MarvelousWololoJul 3, 2019

          bro go with 777 is almost always a terrible idea. :(

          • Francesco Napoletano
            Francesco NapoletanoJul 3, 2019

            It is in general. Probably not in this specific case ;-)

            • Adam Nielsen
              Adam NielsenJul 5, 2020

              I have the same issue. The files are all owned by www-data:www-data and have permission 644 - I don't have permissions to edit/create because my user is adam and even if I add me to group www-data I won't get access. Any advice? And how would you do it with 777 permission? Would you change the permission each time you create a new file?

  • fy
    fyApr 8, 2019

    Not sure what useful info have you added in this post. The same but in more details can be found here
    hub.docker.com/_/wordpress/

  • Jules Colle
    Jules ColleJul 24, 2019

    Awesome! I love

    • how small this is
    • how it only keeps the wp-content folder in the file system (after 10 years of WP development this is the first time I find a dev example that encourages me to only place the wp-content folder under version control.)

    Spotted 1 small error: your last two lines should be

    volumes:
      db_data:

    and not

    volumes:
    db_data:

    otherwise it won't build. It's fine in your git repo though.

    I agree that the 777 file permission is totally fine here. After all it's supposed to be a local dev environment.

  • mp10
    mp10Aug 1, 2019

    Hi. I have an error
    MySQL Connection Error: (1130) Host '172.18.0.3' is not allowed to connect to this MySQL server

  • mp10
    mp10Aug 1, 2019

    I have error

    ERROR: for wordpress-local_db_1 Cannot start service db: b'Mounts denied: EOF'

    ERROR: for db Cannot start service db: b'Mounts denied: EOF'
    ERROR: Encountered errors while bringing up the project.

  • Christopher Ueda
    Christopher UedaApr 7, 2020

    This is the first docker/Wordpress tutorial/repo that has worked for me on the first try! Thanks!

Add comment