Local Development

When you develop your WordPress plugins, it is helpful to run them in a “local” WordPress site that is installed on your computer. Docker is a great solution for this, and the Docker Compose standard allows us to embed the configuration files in our code. That way they are sharable, and you don’t really need to know much about Docker to use them.

All plugins created with Plugin Machine include a Docker Compose file. The README contains the one command need to start the site and a link to the URL. If you enable WordPress testing with the WordPress test suite, you will not have to do any of the complex setup, Instead, you can run one command and Docker will create an environment with all of that setup for you.

All plugins include a docker-compose file for local development. Based on the features you enable, you will have different services.

Developing With Docker Locally

Starting The Site

You can start the local development site by running the command “docker-compose up -d” in the terminal. When this is done, you should be able to access the site, at http://localhost:6100 in your browser.

Helpful Commands

Here are some of the commands you can use to manage the local development environment.

Start server:

docker-compose up -d

Stop server:

docker-compose down

Remove containers:

docker-compose rm

Using WP-CLI

There is a service for using WP CLI inside of Docker. We prefix the command with “docker-compose run wpcli”.

Here are some examples:

Add a new user, who is an admin with username “admin” and password “pass”:

    docker-compose run wpcli wp user create admin admin@example.com --role=admin user_pass=pass`

Running WordPress Tests

If you enabled WordPress tests, there will be a special container for running those integration tests.


Changing The Port

In the plugin, their is a file called “docker-compose.yml”, which configures the local development environment.

In “services”, find the “wordpress” service. It should look like this

      - wpdb
    image: wordpress:latest
      - wordpress_data:/var/www/html
      - ./:/var/www/html/wp-content/plugins/cat-facts
      - "6100:80"
    restart: always
      WORDPRESS_DB_HOST: wpdb:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress

In the “ports” entry we have “6100:80”. IF you want to use http://localhost:2112 as your URL, change that that “2112:80.” Make sure to save the file.

If you have already started the site, you will need to turn it off and back on again:

docker-compose down
docker-compose rm
docker-compose up -d

Before the colon is the port on your host machine, that’s why you use the localhost url including port 6100. After the colon is the port number inside the container. In this case — using the WordPress docker image that is port 80.