How to Create a WordPress Site with Docker Compose and MariaDB in Minutes

Question:

How can I set up a wordpress site with docker compose using the following configuration file and commands? I have omitted the wp-config.php file by mistake.“`version: ‘3.8’services:wordpress:image: wordpressdepends_on:- mariadbenvironment:- WORDPRESS_DB_PASSWORD=p@$$w0rd- WORDPRESS_DB_USER=rootports:- 8088:80volumes:- ./html:/var/www/htmlmariadb:image: mariadbenvironment:- MYSQL_ROOT_PASSWORD=p@$$w0rd- MYSQL_DATABASE=wordpressvolumes:- ./database:/var/lib/mysql“`I executed `sudo docker-compose up -d` to run the containers.My docker version is 25.0.2, build 29cf629 and my docker compose version is 2.24.5.“`

Answer:

How to set up a wordpress site with docker compose

Docker compose is a tool that allows you to define and run multiple containers as a single service. In this article, we will show you how to use docker compose to set up a wordpress site with a mariadb database. We will also explain how to fix the common mistake of omitting the wp-config.php file.

Before you start, you need to have the following:

  • Docker installed on your machine. You can check your docker version by running `docker –version`. In this example, we are using docker version 25.0.2, build 29cf629.
  • Docker compose installed on your machine. You can check your docker compose version by running `docker-compose –version`. In this example, we are using docker compose version 2.24.5.
  • A directory where you want to store your wordpress files and database files. In this example, we are using `./html` for wordpress files and `./database` for database files.
  • Configuration file

    The first step is to create a configuration file that defines the two

    services:

    wordpress and mariadb. The configuration file is a YAML file that has the extension `.yml`. In this example, we are using `docker-compose.yml` as the file name. Here is the content of the file:

    “`

    version: ‘3.8’

    services:

    wordpress: image: wordpress depends_on:

  • mariadb
  • environment:

  • WORDPRESS_DB_PASSWORD=p@$$w0rd
  • WORDPRESS_DB_USER=root
  • ports:

  • 8088:80
  • volumes:

  • ./html:/var/www/html
  • mariadb: image: mariadb environment:

  • MYSQL_ROOT_PASSWORD=p@$$w0rd
  • MYSQL_DATABASE=wordpress
  • volumes:

  • ./database:/var/lib/mysql
  • “`

    Let’s break down the file and see what each line means:

  • `

    version: ‘3.8’

    ` specifies the version of the docker compose file format. You can find more information about the supported versions [here](https://docs.docker.com/compose/compose-file/compose-versioning/).

  • `

    services:

    ` defines the services that make up your application. In this case, we have two

    services:

    wordpress and mariadb.

  • `wordpress:` is the name of the first service. You can use any name you want, as long as it is unique within the file.
  • `image: wordpress` tells docker to use the official wordpress image from the docker hub. You can find more information about the wordpress image [here](https://hub.docker.com/_/wordpress/).
  • `depends_on:` specifies the dependencies of the service. In this case, wordpress depends on mariadb, which means that mariadb will be started before wordpress.
  • `environment:` sets the environment variables for the service. In this case, we are setting the wordpress database password and user, which should match the ones we set for mariadb.
  • `ports:` maps the ports between the host and the container. In this case, we are mapping port 8088 on the host to port 80 on the container, which is the default port for wordpress.
  • `volumes:` mounts the directories on the host to the directories on the container. In this case, we are mounting `./html` on the host to `/var/www/html` on the container, which is the default directory for wordpress files.
  • `mariadb:` is the name of the second service. You can use any name you want, as long as it is unique within the file.
  • `image: mariadb` tells docker to use the official mariadb image from the docker hub. You can find more information about the mariadb image [here](https://hub.docker.com/_/mariadb/).
  • `environment:` sets the environment variables for the service. In this case, we are setting the mariadb root password and the database name, which should match the ones we set for wordpress.
  • `volumes:` mounts the directories on the host to the directories on the container. In this case, we are mounting `./database` on the host to `/var/lib/mysql` on the container, which is the default directory for database files.
  • Running the containers

    The next step is to run the containers using the configuration file. To do that, you need to open a terminal and navigate to the directory where you have the `docker-compose.yml` file. Then, you need to run the following command:

    “`

    sudo docker-compose up -d

    “`

    This command will pull the images from the docker hub if they are not already present on your machine, create the containers, and start them in the background. The `-d` flag means detached mode, which means that the containers will run in the background and you can use your terminal for other tasks. You can check the status of the containers by running:

    “`

    sudo docker-compose ps

    “`

    This command will show you the name, state, and port mapping of each container. You should see something like this:

    “` Name Command State Ports ——————————————————————————–

    wordpress_1 docker-entrypoint.sh apach … Up 0.0.0.0:8088->80/tcp

    mariadb_1 docker-entrypoint.sh –def … Up 3306/tcp

    “`

    Accessing the wordpress site

    The final step is to access the wordpress site from your browser. To do that, you need to open your browser and type the following URL:

    “`

    http://localhost:8088

    “`

    This URL will take you to the wordpress installation page, where you can choose your language, site title, username, password, and email. After you fill in the required fields and click on the install button, you should see a success message and a login button. You can then log in to your wordpress site and start creating your content.

    Fixing the wp-config.php file

    If you have omitted the wp-config.php file by mistake, you will see an error message when you try to access the wordpress site. The wp-config.php file is a configuration file that contains the database connection details and other settings for your wordpress site. To fix this issue, you need to do the following:

  • Stop the containers by running `sudo docker-compose stop`.
  • Create a wp-config.php file in the `./html` directory on the host. You can use the sample file provided by wordpress [here](https://wordpress.org/support/article/editing-wp-config-php/), or you can copy the file from the wordpress container by running `sudo docker cp wordpress_1:/var/www/html/wp-config-sample.php ./html/wp-config.php`.
  • Edit the wp-config.php file and replace the database connection details with the ones you have set in the `docker-compose.yml` file. For example, you need to change the following lines:
  • “` / The name of the database for WordPress */

    define( ‘DB_NAME’, ‘database_name_here’ );

    / MySQL database username */

    define( ‘DB_USER’, ‘username_here’ );

    / MySQL database password */

    define( ‘DB_PASSWORD’, ‘password_here’ );

    “`

    to:

    “` / The name of the database for WordPress */

    define( ‘DB_NAME’, ‘wordpress’ );

    / MySQL database username */

    define( ‘DB_USER’, ‘root’ );

    / MySQL database password */

    define( ‘DB_PASSWORD’, ‘p@$$w0rd’ );

    “`

  • Save the wp-config.php file and restart the containers by running `sudo docker-compose start`.
  • Access the wordpress site again from your browser and you should see the installation page as expected.
  • Conclusion

    In

this article, we have shown you how to set up a wordpress site with docker compose using a configuration file and commands. We have also explained how to fix the common mistake of omitting the wp-config.php file. We hope you have found this article helpful and informative. If you have any questions or feedback, please feel free to leave a comment below. Thank you for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *

Privacy Terms Contacts About Us