Setup WordPress site with Docker on Windows 11



Through a combination of various online how-to’s, I pieced together how to setup a WordPress site on Windows 11 computer using Docker containers. This post will take you step-by-step through the process. This can easily be adapted to a Linux system as well.

I am going to explain the process for configuring everything on a Windows 11 computer. This is strictly localized to your current computer or LAN. If you want to open it up to be accessed remotely you would have to use Port Forwarding on your router (this is outside the scope of this post, but I am here to tell you it is easy).

Be advised, while this is being done on a Windows computer, the containers are all Ubuntu Linux based.

That being said, one of the things you are going to need to get comfortable with is using the command line. There really is no easy way to do this using the GUI (graphic user interface) and while most of the following command are cut and paste, you will have to make some changes specific to your needs.

OK, here we go.

The very first thing you need to do is install Docker. Again, this is outside the scope of this how-to, but it is pretty simple. Follow the instructions from here: https://docs.docker.com/desktop/windows/install/

Just for informational purposes, we will be creating three Docker containers. The first container will contain our Apache2 webserver and PHP. The second will be our mySQL database server, and the third will be using Adminer to access the mySQL server to create databases.

We can now start with our setup.

  1. You will need to open a command prompt. In Windows 11, click on the magnifying glass and in the search field type in CMD
  2. Next, you should open a File Explorer window. Again, click on the magnifying glass and in the search field begin typein File. You will see File Explorer, go ahead and click that to open it.
  3. Next, you are going to want to make a couple directories to work with. One will be where your WordPress files will be served from, the other is for your mySQL database. I suggest making these in your Users folder under your name (for example, mine is C:\Users\Nick). Make a folder called LAMP. Navigate into that folder and create two more folders. One called html and the second called mysql.
  4. Next, open your web browser, navigate to https://wordpress.org/download/ and download the latest version of WordPress. Extract the files to the html directory you created in the previous step. NOTE: Be sure that you extract the files, not the directory, otherwise you will have a wordpress directory in the html folder and that is what people will see – the folder – not WordPress.
  5. Now we will be creating our Docker containers. The first one will be our Apache2/PHP container. Navigate to your command prompt and type in the following:
docker run -d --name apache2-container -p 80:80 -v C:\Users(yourdirectory)\html:/var/www/html ubuntu/apache2:2.4-20.04_beta

Once that completes you will have a running Apache2 server. We now need to access it to install a few things, including PHP.

  1. From the command prompt, type in:
docker exec -it apache2-container /bin/bash

This will take you to the command prompt logged in as root. From here you are going to execute each of the following:

apt update
apt install build-essential
apt install software-properties-common
apt install network-manager
apt install ssh
apt install php php-cli php-fpm php-json php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath

Now restart Apache2

/etc/init.d/apache2 reload

Once those have completed we are ready to create our next container. However, before we leave the Apache2 command line, we want to check and make sure our directory mapping was created properly when we created the container. So, if you type in ls /var/www/html you should see the WordPress files you created in step 4. If you don’t, then something in the creation process of the container was mistyped. If the files are there, we’re ready to move on.

You can type exit and that will take you out of the container but leave it running.

  1. First, we need to create a volume for mySQL.
docker volume create mysql-volume
  1. Next, we will create our mySQL database container. This one is a little more tricky. Again, from the command prompt we will enter the following:
docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=createapasswordforyourself -p 3307:3306 -v mysql-volume:/var/lib/mysql -d mysql:latest

Once finished you should have a new container named mysql-container that is mapped to the mysql directory you created in step 3. If you see a bunch of populated files in that previously empty directory, you should have been successful.

Now, you do not want to access the mySQL databases with the root account. So, we are going to create a user specifically designed to create/access databases.

Again, from the command prompt, type in the following:

docker exec -it mysql-container /bin/bash

Now, at the prompt type in the following and when it asks for a password use the one you created above:

mysql -u root -p

Now you should be at the mysql> prompt… Type in the following to create the mysql user:

CREATE USER 'mysqladmin' IDENTIFIED BY 'createapassword';

Now type in the following to grant the created user with appropriate permissions:

GRANT ALL PRIVILEGES ON *.* TO 'mysqladmin' WITH GRANT OPTION;

Now flush the cache with:

FLUSH PRIVILEGES;

And then type in exit. Now type exit again to leave the mysql container.

  1. Next, we will create a container for Adminer which is a lightweight phpmyadmin alternative for managing your mySQL databases. From the command prompt type in the following:
docker run --name adminer-container --link mysql-container:mysql-container -p 8086:8080 -d adminer:latest

At this point you should have three containers running – One for Apache2 and PHP, the second for your mySQL databases, and the third for Adminer.

  1. The next step is to create a new docker network in order for the Apache2 and mySQL containers to have access to each other for use. First we must create the network:
docker network create wordpress-network

Now that the network has been created, we will attach the Apache and mySQL containers to that network:

docker network connect wordpress-network apache2-container

then

docker network connect wordpress-network mysql-container
  • At this point, we should have a working LAMP (Linux, Apache, mySQL, PHP) setup that is ready to configure WordPress on.
  1. Now we need to launch Adminer and create a database to use for WordPress. Open your web browser and type in http://localhost:8086
  2. Change the server name to mysql-container, Username should be mysqladmin, Password to whatever you choose as the mysqladmin password (NOT the root password), and leave Database blank.
  3. Click on the Create database then in the blank field enter the name you wish to call your WordPress database (remember this) and then click the Save button.
  4. Now we are ready to configure WordPress. The first step for setting up WordPress is to go back to File Explorer and navigate to your html directory you created in step 3.
  5. You need to edit the wp-config-sample.php file and make the following changes:
define( 'DB_NAME', 'database_name_here' ); <-- the database name you created in step 12.

/** Database username */
define( 'DB_USER', 'mysqladmin' );

/** Database password */
define( 'DB_PASSWORD', 'mysqladminpasswordcreatedinstep7' );

/** Database hostname */
define( 'DB_HOST', 'mysql-container' );
  1. Now, scroll to the bottom of the wp-config-sample.php file and type in the following:
define('FS_METHOD','direct');

This allows you to update WordPress, install themes, etc without having to setup a FTP server to do so.

  1. Save the wp-config-sample file as wp-config.php
  2. You should now be able to open your web browser and type in http://localhost/wp-admin/install.php to begin the WordPress setup and configuration.

I hope this tutorial was helpful. PLEASE leave me a comment if I missed something, if there is a typo, or if you have a question about the process.

Comments