To set up WordPress and MySQL in no time and without installing them, here is a useful docker-compose.yml file. If you’re new to WordPress development, you’d probably installed XAMPP and MySQL on your Windows machine. You may not want to do it if you’re going to keep your operating system in tiptop condition. Changes are the configuration you do for XAMPP, and MySQL may harm the operating system. Also, you may not want these servers running when you don’t need them.
WordPress And MySQL Images
We are going to use 2 Docker images – WordPress and MySQL – using docker-compose.yml.
docker-compose.yml File For Docker Compose
The following is a docker-compose.yml file for our Docker Compose. Note we are making the WordPress files available for modification by placing them in a local directory (line 31).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | version: "2.3" services: restfulapi-db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql networks: - restfulapi_bridged ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: somewordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress restfulapi-wordpress: depends_on: - restfulapi-db image: wordpress:latest ports: - "8000:80" networks: - restfulapi_bridged environment: WORDPRESS_DB_HOST: restfulapi-db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wordpress volumes: - C:\wp_dir:/var/www/html networks: restfulapi_bridged: driver: bridge volumes: db_data: {} |
Make sure the wp_dir folder exists, and Docker File Sharing enabled before moving on.
To use this with Docker Compose, run the following commands in the same directory where the docker-compose.yml file is. Docker Compose will download the WordPress and MySQL images and start them (containers) up.
1 2 | docker-compose pull docker-compose up |
The following is the output on the command-line window:
When you close the command-line window, you also terminate both WordPress and MySQL containers. If you don’t want that, use the -d option. For example:
1 | docker-compose up -d |
If you check out the C:\wp_dir folder, you’d see all the WordPress files.
Testing WordPress With MySQL
Go to http://localhost:8000 and follow the WordPress installation process. To access MySQL from Windows, use the port 3306, as stated in the docker-compose.yml file.
This post is part of the Docker For Developers tutorial.