Dockerized Wordpress, MySQL, and phpmyadmin with environment variables

Posted by Ray on August 4, 2023

This document is a guide for spinning up a dockerized wordpress, mysql, and phpmyadmin application.

Pre-requisites

  1. Docker & Docker Compose

Creating docker-compose.yml file

  1. Create and navigate to your project directory.
    1
    2
    
    $ mkdir your_project_name
    $ cd your_project_name
    

    navigate_to_project_dir

  2. Create a docker-compose.yml file
    1
    
    $ nano docker-compose.yml
    
  3. Paste the following code into the newly created docker-compose.yml file. Each line is explained by an inline comment, which can be removed. Replace your_project_name, your_mysql_version, your_phpmyadmin_version, and your_wordpress_version with your project’s name, preferred mysql version, preferred phpmyadmin version, and preferred wordpress version, respectively
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
version: '3'

services:
  # Database
  db:
    container_name: your_project_name_db  # database container name
    image: mysql:your_mysql_version  # project mysql version. ex: mysql:8.0.33
    volumes:
      - ./db_data/:/var/lib/mysql  # database files mounted to ./db_data directory
    restart: always
    environment:
      MYSQL_TCP_PORT: ${MYSQL_TCP_PORT}  # port in which mysql will run on
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}  # mysql root password
      MYSQL_DATABASE: ${MYSQL_DATABASE}  # your project's database name
      MYSQL_USER: ${MYSQL_USER}  # your project's database username
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}  # your project's database password
    networks:
      - your_project_namewp
  # phpmyadmin
  phpmyadmin:
    container_name: your_project_name_phpmyadmin  # phpmyadmin container name
    depends_on:
      - db
    image: phpmyadmin:your_phpmyadmin_version  # your phpmyadmin version. ex: phpmyadmin:5.2.1
    restart: always
    ports:
      - '${PHPMYADMIN_PORT}:80'  # port in which phpmyadmin will run on
    environment:
      PMA_HOST: db:${MYSQL_TCP_PORT}  # phpmyadmin host
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}  # should have the same value as MYSQL_ROOT_PASSWORD in db container
    networks:
      - your_project_namewp
  # Wordpress
  wordpress:
    container_name: your_project_name_wp  # project's wordpress container
    depends_on:
      - db
    image: wordpress:your_wordpress_version  # your wordpress version. ex: wordpress:6.1.1
    ports:
      - '${WORDPRESS_PORT}:80'  # port in which wordpress will run on
    restart: always
    volumes: ['./src/:/var/www/html']  # wordpress files mounted to ./src directory
    environment:
      WORDPRESS_DEBUG: ${WORDPRESS_DEBUG}  # debug mode
      WORDPRESS_DB_HOST: db:${MYSQL_TCP_PORT}
      WORDPRESS_DB_USER: ${MYSQL_USER}  # should have the same value as MYSQL_USER env in db container
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}  # should have the same value as MYSQL_PASSWORD env in db container
    networks:
      - your_project_namewp
networks:
  your_project_namewp:
volumes:
  db_data:

creating_dockercompose_yml

Creating .env file

  1. Create a .env file to store environment variables
    1
    
    $ nano .env
    
  2. Paste and configure the following values
    1
    2
    3
    4
    5
    6
    7
    8
    
    MYSQL_ROOT_PASSWORD=your_mysql_root_password
    MYSQL_DATABASE=wordpress
    MYSQL_USER=your_mysql_username
    MYSQL_PASSWORD=your_mysql_password
    MYSQL_TCP_PORT=3306
    PHPMYADMIN_PORT=8080
    WORDPRESS_PORT=8000
    WORDPRESS_DEBUG=[true | false]  # set to true if development, false if production
    

    creating_env_yml

  3. Verify configuration. All environment variables should be filled with their true values.
    1
    
    $ docker-compose config
    

    verify_config_yml

Building the wordpress application

  1. Build and run the containers. The ./db_data directory will contain the database files, and the ./src directory will contain your wordpress source codes.
    1
    
    $ docker-compose up --build
    

    build_container

  2. Using your browser, navigate to localhost:your_wordpress_port and continue with your website setup local_wordpress

  3. Using your browser, navigate to localhost:your_phpmyadmin_port for the phpmyadmin dashboard. You should be able to login using root as the username, and your MYSQL_ROOT_PASSWORD password found on the .env file. phpmyadmin

Done!

*** To spin up multiple dockerized applications on a single host (separate wordpress, database, and phpmyadmins per project), use different port configuration for each project. Example, proj1 should use MYSQL_TCP_PORT=3306, PHPMYADMIN_PORT=8080, and WORDPRESS_PORT=8000, while proj2 should use MYSQL_TCP_PORT=3307, PHPMYADMIN_PORT=8081, and WORDPRESS_PORT=8001.