Install Docker Compose: First, you need to install docker-compose.
Create a Docker Compose file: Next, you need to create a Docker Compose file. Create a new file named docker-compose.yml in your project directory and add the following code to it:
version: '3'
services:
db:
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_DATABASE: moodle
MYSQL_USER: moodleuser
MYSQL_PASSWORD: moodlepass
MYSQL_ROOT_PASSWORD: rootpass
volumes:
- db_data:/var/lib/mysql
web:
image: moodlehq/moodle-php-apache:latest
restart: always
depends_on:
- db
environment:
MOODLE_DB_HOST: db
MOODLE_DB_USER: moodleuser
MOODLE_DB_PASSWORD: moodlepass
MOODLE_DB_NAME: moodle
volumes:
- moodledata:/var/www/html
ports:
- "80:80"
volumes:
db_data:
moodledata:
This file defines two services: a database service (db) running MySQL 5.7 and a web service (web) running the latest version of the Moodle image. The db service uses a named volume db_data to store its data, and the web service uses a named volume moodledata to store the Moodle data. The web service also binds port 80 of the container to port 80 of the host machine.
Start the containers: You can start the containers using the following command:
docker-compose up -d
This command will start the containers in detached mode (-d) and create the necessary volumes if they don’t exist.
Access Moodle: You can access Moodle by visiting http://localhost in your web browser.
That’s it! You now have Moodle running in a Docker Compose setup with a separate database container.