Setup WordPress Environment With Docker Composer
Tags: wordpress, docker, setupenv, dev2023
Published: 2023-08-24 12:38:37

To set up a WordPress development environment with Docker Compose, follow these steps:

### Install Docker and Docker Compose
 on your machine if you haven't already. You can download them from the official [<u>Docker website</u>](https://www.docker.com/).

### Create a new directory where you want to set up your WordPress development environment
.
`$mkdir -p Dev.to/Docker-Wp`
`$cd Dev.to/Docker-Wp`

### Create a new file called `docker-compose.yml`
 in the directory and open it in a text editor.

Add the following code to the `docker-compose.yml` file:

```yaml
version: '3.3'

services:
  db:
    image: mariadb
    volumes:
      - ./mywebsite/db:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: wordpress_db
      MYSQL_PASSWORD: root

  wordpress:
    build:
      context: docker
      dockerfile: Dockerfile
      args:
        HOST_UID: ${HOST_UID}
    depends_on:
      - db
    volumes:
      - ./mywebsite/wp:/var/www/html
    image: wordpress:latest

    ports:
      - "80:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: root
      WORDPRESS_DB_NAME: wordpress_db

volumes:
  db_data:
```

### Create Docker file
docker/Dockerfile
```yaml
FROM wordpress:latest

ARG HOST_UID=1000
```
Save the `docker-compose.yml` file and Dockerfile.

Open a terminal or command prompt and navigate to the directory where you created the `docker-compose.yml` file.

Run the following command to start the WordPress development environment:

```
docker-compose up -d
```

Docker Compose will download the necessary images and start the containers. Wait for the process to complete.

Once the containers are up and running, you can access your WordPress site by opening a web browser and going to `http://localhost`. You should see the WordPress installation page.

Follow the on-screen instructions to complete the WordPress installation, including setting up the admin account and database connection.

You can make changes to the WordPress files in the `wp-content` directory of your project. Any changes you make will be reflected in the running containers.

To stop the development environment, run the following command:

```
docker-compose down
```

This will stop and remove the containers, but your data will persist in the `db_data` volume, so you won't lose any changes or content.

You now have a fully functional WordPress development environment set up with Docker Compose!
Note: Make sure to replace `password` with your desired password for the MySQL database and WordPress user. Also, you can change the port `8000` in the `docker-compose.yml` file to any other available port if you prefer.

Additionally, if you need to access the MySQL database directly, you can use a tool like phpMyAdmin. To do so, add the following service definition to the `docker-compose.yml` file:

```yaml
  phpadmin:
    depends_on:
      - db
    image: phpmyadmin
    restart: always
    ports:
      - "8080:80"
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: root
      UPLOAD_LIMIT: 300000000
```

Save the file and run `docker-compose up -d` to start the phpMyAdmin container. You can then access phpMyAdmin by going to `http://localhost:8080` in your web browser.
Good luck!

Contact me to support!
[Twitter](https://twitter.com/ducmynguyen)
[Facebook](https://www.facebook.com/ducmy.nguyen.vn/)