Guidance on using Docker with WordPress theme/plugin development

Hello docker community! I’m new here and looking for some guidance on integrating Docker into our workflow for custom WordPress theme development.

Currently, we’re a team of 3 developers and sometimes we contract out freelancers. We would like to be able to create the same dev environment for all of us using a Docker container. From my understanding of Docker, it seems as the most logical way to go about this for a project that already exists would be to:

-Create a local directory, let’s call it wp-docker-setup and import everything from wp-content into it

-Add a docker-compose.yaml file that looks like the following:

version: '3'

    services:
       db:
         image: mysql:5.7
         volumes:
           - ./wp-data:/var/lib/mysql
         restart: always
         environment:
           MYSQL_ROOT_PASSWORD: wordpress
           MYSQL_DATABASE: wordpress
           MYSQL_USER: wordpress
           MYSQL_PASSWORD: wordpress

       wordpress:
         depends_on:
           - db
         image: wordpress:latest
         volumes: 
           - .:/var/www/html/wp-content/
         ports:
           - "80:80"
         restart: always
         environment:
           WORDPRESS_DB_HOST: db:3306
           WORDPRESS_DB_USER: wordpress
           WORDPRESS_DB_PASSWORD: wordpress
    volumes:
        db_data:

-Mount the current directory as a volume – .:/var/www/html/wp-content/

-Mount the db file so it’s imported into mysql and loads with the site.
^Here’s where I’m stuck

My questions are:

  1. Where do I put the db file so that it’s mounted when I docker-compose the container?
  2. After the container is composed and running, let’s see I make changes that affect the db, how could I access the db in order to export the new file?

I’ve been researching this for the past few days but haven’t had much luck… I was able to figure out mounting the themes but mounting the db and then later being able to access it has stumped me.

Any guidance/suggestions on where to look or how to answer these questions is really appreciated. Thank you in advance!

Questions:

I’m looking for the exact same thing, have you ever found a great tutorial on how to begin working on Wordpress on Docker?

Here’s a brief explanation of your docker-compose.yml file:

  1. It causes two docker containers to be created.
  2. One, representing a server running wordpress, mounts your local directory (the directory in which your docker-compose.yml file is) into /var/www/html/wp-content/. This works exactly the same way as you copying this directory over to a real wordpress server.
  3. The second, representing a MySQL server, mounts a subdirectory of your local called wp_data into /var/lib/mysql. This means that the database files will be created or picked up from this subdirectory. You also do not need to copy anything back - any changes made to the database will directly be made to the files in this subdirectory.

To use this in a theme development workflow, here are the instructions:

  1. Create a directory (let’s call it wp-theme-dev). Copy your wp_content files into it.
  2. Inside it, create a subdirectory called wp_data. Copy your database files to it.
  3. In the wp-theme-dev directory, create the docker-compose.yml file. Since you have provided your own database files, make sure the database name, usernames and passwords are correctly specified in the file.
  4. With everything in place, use the command docker-compose up to create and start both containers.

At this point, you can access http://localhost:80 in a browser, and see your wordpress installation working. You can make changes to the files in the wp-theme-dev directory. Since the wordpress container mounts this directory as a volume, the changes will take effect without you having to restart the container. Similarly, any changes to the database that gets made will be saved to the files in wp-theme-dev/wp_data subdirectory.

If you like, I can suggest some small improvements to the process.

It has been a few days and I have been making some steps closer to what I would like my work flow to be, I like it that you also copy the database content to a folder, I havent got that to work yet. This is what a “working” project looks like. I mount my database via - db_data:/var/lib/mysql, but this isn’t visible in my project file.

I’m also missing another piece of the puzzle and that is how I get my Gulp workflow to work in my theme file. I now rung gulp watch from my host machine, but I like it that when I start up my containers all these commands will also run inside the container.

Do you have any tips on that or other general tips on. my docker-compose.yml file? Thanks for your response btw!