Docker
15 Jun 2022

Running Wordpress website connected to MySql inside Docker containers

Assuming Docker is installed on your machine. If not install Docker desktop on your machine. Google it.

Pull required images

Open command prompt on Windows or Terminal on Mac and pull the following images.

Command

varinder@my-macbook ~ % docker pull wordpress
varinder@my-macbook ~ % docker pull phpmyadmin
varinder@my-macbook ~ % docker pull mysql

Above commands will pull all the required images to your machine. Verify all the images have been downloaded by running the following command.

Command

varinder@my-macbook ~ % docker image ls

Create a Network

A network is needed for all the above containers to communicate.

Command

varinder@my-macbook ~ % docker network create --driver bridge dev-network 

Verify network creation by following command.

Command

varinder@my-macbook ~ % docker network ls 

Run Database Container First

Database is required for Wordpress to connect, so creating and running MySql database container first.

Create the following folders on your machine to create volumes on your disk to persist container data in case container crashes or got deleted.

  • Docker / Volumes / MySql
  • Docker / Volumes / Wordpress

Navigate to MySql folder and run the following command.

Command

varinder@my-macbook mysql ~ % docker run --restart always --name test-mysql --net dev-network -v $(pwd):/var/lib/mysql -p 3306:3306 -d -e MYSQL_ROOT_PASSWORD=MyTestPassword mysql

The above command will create a test-msql container running MySql instance on port 3306.

Verify the container is running.

Command

varinder@my-macbook mysql ~ % docker ps

If not running, check logs for any errors.

Command

varinder@my-macbook mysql ~ % docker logs test-mysql

Run PhpMyAdmin Container

Create PhpMyAdmin container to connect/browse the MySql database instance we just created “test-mysql”.

Command

varinder@my-macbook mysql ~ % docker run --name test-phpmyadmin -d -link test-mysql:db -p 8081:80 --net dev-network phpmyadmin

Verify the container is running and open internet explorer and browse to http://localhost:8081 and you should see PhpMyAdmin web portal running prompting to login. Enter “root” as username and “MyTestPassword” as password to login.

Once logged in, go to “Databases” tab and create new database named “test_wordpress_db” for Wordpress. After that, head back to command prompt/terminal.

Run Wordpress Container

Lets create Wordpress container and connect it to the database we just created “test_wordpress_db”. Navigate out of mysql folder and into wordpress folder inside command prompt/terminal to create a volume for Wordpress instance.

Command

varinder@my-macbook wordpress ~ % docker run --name test-wordpress -p 8080:80 -d --net dev-network -v $(pwd):/var/www/html wordpress

Make sure all the container are running.

Command

varinder@my-macbook mysql ~ % docker ps

Make sure all the containers are in same network “dev-network”.

Command

varinder@my-macbook mysql ~ % docker network inspect dev-network

All the above created containers “test-mysql”, “test-phpmyadmin” and “test-wordpress” should be listed under “Containers” node. Now, lets run Wordpress installation and connect it to the MySql database.

Wordpress Installation/Setup

  • Open internet explorer and browse to http://localhost:8080 and you should see a Wordpress setup page.
  • Select your language and click “Continue”.
  • Click “Let’s go!” button.
  • Enter
    • Database Name = “test_wordpress_db”
    • Username = “root”
    • Password = “MyTestPassword”
    • Database Host = “test-mysql”

(Note: the database host should be the name of the mysql container and not the localhost url.)

If all the values are correct, then you should see a “Run the installation” button. Hit it! and you should see Wordpress welcome page.

If for some reason you are not able to connect make sure your values are correct in Wordpress setup page. You can also navigate to the volumes/wordpress/ folder and edit wp-config.php file and add the above variables directly.

Hope this helped you :)