arrow left
Back to Developer Education

    Containerizing WordPress with Docker-Compose

    Containerizing WordPress with Docker-Compose

    Docker is a container management system. It is used to manage an application that has multiple components. Docker-compose is a toolkit provided by Docker. It defines and runs a multi-container Docker application. <!--more--> Most applications involve more than one architectural component. In such applications, Docker-compose will help you run these components as defined in your application stack. It provides a single file that defines how different containers interact with each other as required by your application stack.

    A good example of a stack application is WordPress. WordPress is an open-source API for the content management system. WordPress API is used to create beautiful websites, blogs, or apps. It consists of a phpMyAdmin, MySQL database container, and a WordPress installation container.

    Docker allows us to create a simple YAML config file to bundle these WordPress containers (components). The components will interact and run as one application. We will use Docker-compose to run these three WordPress API containers to utilize the WordPress content management system.

    Typically, setting up a WordPress installation involves multiple steps. You need to set up the WordPress environment manually. This can be very cumbersome. Furthermore, you should establish a local web server, configure the server to execute PHP code, and set up a MySQL database.

    You can also use pre-built bundles like MAMP for MacOS or XAMPP and Wamp for Windows to set all WordPress components on your local computer's system.

    However, with Docker, you create a single file and run few commands. This will set up everything within Docker containers, and your WordPress will run fine. This is the most straightforward way to set up a WordPress website environment.

    Goal

    This tutorial will demonstrate how to Dockerize a WordPress website using Docker-compose through Docker commands.

    Prerequisites

    Getting ready

    To get started, install Docker on your computer.

    While installing Docker on Windows, you might come across this error.

    Docker installation error

    To solve this, download and install the WSL 2 Linux kernel. Restart the Docker desktop, and the error will be resolved.

    We will be using images that are readily available in the Docker Hub.

    These include:

    • WordPress - We need a WordPress image to create a WordPress website. WordPress image includes the Apache server that we need to execute PHP code.
    • MySQL - This will set the WordPress environment variables such as the MySQL root password, users, and database.
    • phpMyAdmin - It allows us to view database tables and columns.

    We are now ready to create a single YAML file that will set all of these Docker images and containers.

    Setting the YAML file

    First, create a project directory in your computer preferred location. i.e. cd desktop, And then your project directory mkdir wordpress-docker

    Change directory to the newly created directory with cd wordpress-docker.

    Inside the wordpress-docker directory, create a .yaml file, i.e. docker-compose.yaml. Run type nul >> docker-compose.yaml to create the file. If your are on Linux or Mac use touch docker-compose.yaml.

    Docker-compose can also work with .yml.

    Open the project with your preferred text editor. I am using Visual Studio Code. I will go ahead and open it with code ..

    Setting the compose environs

    Go ahead and include the following line in the .yaml file top-level.

    Version: '3'
    
    • Services - Defines the types of containers to run. In this case, they include WordPress and MySQL.
    services:
    

    Add containers in the services block, as shown below.

    services:
    
      #MySQL Database image
      mysql_db:
    
      #Wordpress image based on Apache
      wordpress:
    
    1. Database
    • The container name. This will be the name you want to give the database container.
    container_name: database_container
    
    • Restart mode: In case the container stops running for any reason, set it to restart. If the server reboots, the container restarts.
    restart: always
    
    • Container image: To run a MySQL database, you need a MySQL image as provided in the MySQL Docker hub. Ensure to include the latest version of the container image.

    Set this image, as shown below.

    image: mysql
    
    • MySQL environments: The database environment consists of the database name and password and database username and password. WordPress will use these environment variables to connect to the MySQL container.
    environment:
        MYSQL_ROOT_PASSWORD: my_password_1234789
        MYSQL_DATABASE: my_wp_database
        MYSQL_USER: my_wp_user
        MYSQL_PASSWORD: my_wp_user_password
    
    • Define the container volumes. This will map the MySQL container data to the volumes you created.
    volumes:
      - mysql:/var/lib/mysql
    
    1. WordPress

    Now, let's define the WordPress services.

    • Depend on. It ensures that a container only starts when the services it depends on are online. WordPress relies on the MySQL container, therefore, specify the depend_on as follows.
    depends_on:
      - my_database
    

    The name should be equal to the name of the database service as defined in the MySQL container.

    image: WordPress:latest
    

    Always include annotation latest so that WordPress loads on the latest available version.

    • Define the restart policy as always.
    restart: always
    
    • Set WordPress container port. WordPress image is based on Apache, and you need to set the port that Apache runs on. By default, Apache runs on port 80. Define this port to map the container to the local machine. Map the default apache port to port 8000 of the local computer.
    ports:
      - "8000:80"
    
    • WordPress environment variables. For a WordPress container to run, you should set the database environments that WordPress will utilize. These variables include the WordPress database host, WordPress database user name, database user password, and the database name defined in the MySQL container environs.
    environment:
        WORDPRESS_DB_HOST: my_database:3306
        WORDPRESS_DB_USER: my_wp_user
        WORDPRESS_DB_PASSWORD: my_wp_user_password
        WORDPRESS_DB_NAME: my_wp_database
    
    • Set these WordPress volumes. They map the current directory to the directory containing the WordPress files.
    volumes:
      ["./:/var/www/html"]
    
    1. Create a top-level volume that will define MySQL as stated in mysql:/var/lib/mysql.
    volumes:
      mysql: {}
    

    The complete YAML file

    version: "3"
    services:
      #MySQL Database image
      my_database:
        image: mysql
        restart: always
        environment:
          MYSQL_ROOT_PASSWORD: my_password_1234789
          MYSQL_DATABASE: my_wp_database
          MYSQL_USER: my_wp_user
          MYSQL_PASSWORD: my_wp_user_password
        volumes:
          - mysql:/var/lib/mysql
    
      #WordPress image based on Apache
      wordpress:
        depends_on:
          - my_database
        image: wordpress:latest
        restart: always
        ports:
          - "8000:80"
        environment:
          WORDPRESS_DB_HOST: my_database:3306
          WORDPRESS_DB_USER: my_wp_user
          WORDPRESS_DB_PASSWORD: my_wp_user_password
          WORDPRESS_DB_NAME: my_wp_database
        volumes:
          ["./:/var/www/html"]
    volumes:
      mysql: {}
    

    Testing

    The YAML file is ready to initialize the defined Docker container. Run the following command to set this container.

    docker-compose up -d
    

    Ensure you are running the command above from the directory where your YAML file is located.

    This will download all the environs required by WordPress. If you look at your directory, you'll realize that there are new files and folders. These are the WordPress files downloaded from the guideline set in the docker-compose.yaml.

    Wordpress files

    To confirm if the WordPress site is working, open http://localhost:8000/ in the browser. This will launch the normal WordPress wizard.

    Wordpress installation wizard

    Click continue and provide the wp-admin information and install WordPress.

    WordPress wp-admin config information

    Login with the information you have provided, and this will launch the WordPress back-end.

    WordPress wp-admin backend

    And you are done. You have dockerized a WordPress website.

    Run docker-compose down to cut down the two containers.

    Setting PHPMyAdmin

    Since we are using Mysql, we can add the service phpMyAdmin to access and view the database.

    Go ahead and include the following phpMyAdmin service and its environs in your YAML file.

    phpmyadmin:
        depends_on:
          - my_database
        image: phpmyadmin/phpmyadmin
        restart: always
        ports:
          - '8080:80'
        environment:
          PMA_HOST: my_database
          MYSQL_ROOT_PASSWORD: my_password_1234789
    

    Run docker-compose up -d, open http://localhost:8000/ to view the website.

    Navigate to http://localhost:8080/ to view the phpMyAdmin. Enter your authentication details and you'll be able to view and interact with the MySQL database.

    wordpress PHPMyAdmin

    Conclusion

    We now have WordPress up and running. This is an easier way to set up the WordPress API. Besides, you can use this method to carry out WordPress testing before releasing it to the public.

    Happy coding!

    Further learning


    Peer Review Contributions by: Wanja Mike

    Published on: Apr 12, 2021
    Updated on: Jul 15, 2024
    CTA

    Start your journey with Cloudzilla

    With Cloudzilla, apps freely roam across a global cloud with unbeatable simplicity and cost efficiency
    Get Started for Free