How to Run Moodle with Docker Compose

How to Run Moodle with Docker Compose

In this tutorial we are going to have a Moodle Instance up and running on a Virtual Machine – inside Docker Containers.

First of all, install and get Docker up and running. Then, download Moodle or make sure you have a Git-Repository ready at hand. We are going to use docker-compose for this task. Now let’s try and Run Moodle with Docker.

Moodle with Docker

We need a container that will have all the needed technology to run and serve the Moodle source code (your repository). This configuration will come from an Image, that we will use and configure through a docker-compose.yml file. Create a file docker-compose.yml and insert the version of docker-compose syntax that we are going to use:

version: "3.3"

These Technologies are needed:

  • Moodle Container
    • Operating System (Ubuntu)
    • Server (Apache2, Nginx)
    • PHP7.x (incl. php-curl, php-mysql, etc.)
  • Database Container
    • MariaDB 10+

Database Docker Image

We are going to use a simple and straightforward solution from bitnami. We simple create a service that will use this image, and set some parameters like username, db_name, etc.

services:
  mariadb:
    image: "bitnami/mariadb:10.1"
    networks:
      - moodle-net
    environment:
      - MARIADB_USER=bn_moodle
      - MARIADB_DATABASE=bitnami_moodle
      - ALLOW_EMPTY_PASSWORD=yes
    volumes:
      - type: bind
        source: /home/maria_data
        target: /bitnami
    ports:
      - "3306:3306"

We call our service ‚mariadb‘, expose some ports (3306) and bind a volume to persist our data on our host machine. We also attach ‚mariadb‘ to a network moodle-net. Also, make sure that docker can read/write the /home/maria_data folder on your host. We set rwx access for now (which is not recommended), please make sure to set the proper rights.

sudo chmod a+rwx /home/maria_data/

Server Docker Image

There are already built images for these technologies: webdevops/php-apache:7.3 & bitnami/mariadb.

Let’s pull and test the PHP & Apache image first.

#Download
docker pull webdevops/php-apache:7.3

# Run Image (Copy image ID. 'docker images')
docker run -d -p 80:80 efec3d223189

Now navigate to localhost or your public IP Address. You should see an empty Apache2 page.

Moodle and Docker

This page is the result of the file ‚index.php‘ in container in folder /app. Worked fine. To use this image as a service in our config file, we need to create a new service in docker-compose.yml.

  moodle:
    image: "webdevops/php-apache:7.3"
    networks:
      - moodle-net
    environment:
      - MOODLE_DBHOST=mariadb
      - MOODLE_DBPORT=3306
      - MOODLE_DBUSER=bn_moodle
      - MOODLE_DBPASS=''
      - MOODLE_DBNAME=bitnami_moodle
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - type: bind
        source: /home/moodle_data
        target: /moodledata/moodledata
      - type: bind
        source: /home/gita-moodle
        target: /app
    depends_on:
      - mariadb

[et_bloom_inline optin_id=“optin_5″]

We specify the image that we tested previously. Specify the network to be same as the network of mariadb container. The Env Vars that we define, will be used by moodle in config.php. We open ports and specify the volumes. We make sure that moodle container only starts after mariadb container is up with the depends_on option. Make sure /home/moodle_data is writable.

sudo chmod a+rwx /home/moodle_data/

Before we continue, in these courses you can learn how to create backend and frontend application. Both of which you can easily deploy and serve with Docker. And now you know how to use docker-compose. Expand your skill-set with these courses.

Finish line

At last, we need to define our network and volumes:

networks:
  moodle-net:
    driver: bridge

volumes:
  mariadb_data:
    driver: local
  moodle_data:
    driver: local

Now let’s boot up these containers with and visit localhost or your public ip.

docker-compose up -d

You should see the installation process. Enter your Database data (see ENV VARS in MariaDB Service. After that a config.php file will be created. If not, copy the code and create a config.php file in root folder.

Moodle Installation process Docker

After finishing up the setup process of moodle, you’ll finally see your ready working moodle instance. Now you run Moodle with Docker Containers.

After setting up moodle. Your host volumes/folders will be filled with data that comes from containers. E.g. moodle population a predefined folder structure in moodle_data.

ls moodle_data/
cache  filedir  lang  localcache  lock  moodle  muc  sessions  temp  trashdir

Same, and more importantly, applies to mariadb container. Our Database is now persisted on our host machine.

/home/maria_data/mariadb/data# pwd
/home/maria_data/mariadb/data
/home/maria_data/mariadb/data# ls
aria_log.00000001  aria_log_control  bitnami_moodle  ibdata1  ib_logfile0  ib_logfile1  multi-master.info  mysql  mysql_upgrade_info  performance_schema  tc.log  test

[pods name=“coach“ slug=“714″ template=“Coach Template“]