Dockerize moodle instance

160 Views Asked by At

Im trying to dockerize the moodle i've installed locally so it is easier for me to manage it when i elevate the moodle to "another level". In the future i will have several moodles so i need containers to be easier to manage them.

I've tried several aproches. I tried do create my onw image , using a dockerfile:

# Use an official PHP runtime as a parent image
FROM php:7.4-apache

# Set the working directory to /var/www/html
WORKDIR /var/www/html

# Copy the contents of your Moodle directory to the container
COPY . /var/www/html

# Install any additional dependencies or PHP extensions if needed
# For example:
# RUN docker-php-ext-install pdo_mysql

# Expose port 80 to the outside world
EXPOSE 80

# Start Apache when the container launches
CMD ["apache2-foreground"]

and a docker-compose.yml:

version: '3.8'

services:
  moodle:
    image: your-moodle-image-name
    ports:
      - "8080:80"  # Adjust the port mapping as needed

but nothing works.

Also , i've tried to make just an docker-compose.yml file that runs both moodle and mariadb (but with bitnami/moodle image):

version: '2'
services:
  mariadb:
    image: mariadb
    container_name: mariadb2
    ports:
      - 3306:3306
    volumes:
      - /srv/dev-disk-by-x/dckcnfg/moodle:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=passroot
      - MYSQL_ROOT_USER=root
      - MYSQL_DATABASE=moodle
      - ALLOW_EMPTY_PASSWORD=yes

  moodle:
    image: bitnami/moodle
    container_name: moodle2
    ports:
      - 8089:80
     # - 8449:443
    environment:
      - MOODLE_DATABASE_HOST=localhost
      - MOODLE_DATABASE_USER=user
      - MOODLE_DATABASE_PASSWORD=pass
      - MOODLE_DATABASE_NAME=moodle
      - ALLOW_EMPTY_PASSWORD=yes
    volumes:
      - /var/www/html/moodle:/opt/bitnami/moodle
      - /var/www/moodledata:/opt/bitnami/moodledata
    depends_on:
      - mariadb
    links:
      - mariadb:mariadb

and it does not work. I get "trying to connect to db server" on the moodle logs. I guess it is beacause for me to deploy mariadb containar , i cannot have mariadb running for moodle outside the container , and then , i never get the container to speak with it to the outside.

1

There are 1 best solutions below

0
Matt Rice On

Instead of rolling your own, there is a Moodle-HQ-maintained Docker project that might be worth looking into: https://github.com/moodlehq/moodle-docker

I personally have found success using the instructions to Use containers for manual testing, as that gives you an install you can interact with:

# Initialize Moodle database for manual testing
bin/moodle-docker-compose exec webserver php admin/cli/install_database.php --agree-license --fullname="Docker moodle" --shortname="docker_moodle" --summary="Docker moodle site" --adminpass="test" --adminemail="[email protected]"

Notes:

  • Moodle is configured to listen on http://localhost:8000/.
  • Mailpit is listening on http://localhost:8000/_/mail to view emails which Moodle has sent out.
  • The admin username you need to use for logging in is admin by default. You can customize it by passing --adminuser='myusername'

Most of the configuration can be changed by using environment variables; I gathered mine into an .env.example file that I use for reference when spinning up a new environment:

CODE_DIR=`pwd`
DOCKER_DIR=/var/work/docker-moodle/

export COMPOSE_PROJECT_NAME=moodle
export MOODLE_DOCKER_WEB_PORT=8000
export MOODLE_DOCKER_SELENIUM_VNC_PORT=5900
export MOODLE_DOCKER_WWWROOT="$CODE_DIR"
export MOODLE_DOCKER_DB=pgsql
export MOODLE_DOCKER_DB_PORT=3306