From 809e7591cf8883f27a73552b9fc324a156c7184c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Anne?= Date: Thu, 20 Jun 2024 12:15:29 +0200 Subject: [PATCH] Add docker developement environment --- .devcontainer/README.md | 25 ++++++++++++++ .devcontainer/devcontainer.json | 36 +++++++++++++++++++ .devcontainer/docker-compose.yaml | 51 +++++++++++++++++++++++++++ .devcontainer/initdb.sql | 7 ++++ .docker/README.md | 57 +++++++++++++++++++++++++++++++ .docker/app/Dockerfile | 21 ++++++++++++ .gitignore | 2 ++ composer.lock | 10 +++--- docker-compose.yaml | 12 +++++++ tools/build_glpi.sh | 1 + 10 files changed, 217 insertions(+), 5 deletions(-) create mode 100644 .devcontainer/README.md create mode 100644 .devcontainer/devcontainer.json create mode 100644 .devcontainer/docker-compose.yaml create mode 100644 .devcontainer/initdb.sql create mode 100644 .docker/README.md create mode 100644 .docker/app/Dockerfile create mode 100644 docker-compose.yaml diff --git a/.devcontainer/README.md b/.devcontainer/README.md new file mode 100644 index 00000000000..48d53bfe4f4 --- /dev/null +++ b/.devcontainer/README.md @@ -0,0 +1,25 @@ +# GLPI docker devcontainers + +The docker devcontainers are meant to be used by VSCode or in a Github Codespaces environment. + +## Services ports + +By default, the following ports are exposed: + - `8080` for the GLPI web server, + - `8025` for the Mailpit web server, + - `8090` for the Adminer web server. + +You can customize these ports by creating a `.devcontainer/docker-compose.override.yaml` file. + +```yaml +services: + app: + ports: !override + - "9000:80" + mailpit: + ports: !override + - "9025:8025" + adminer: + ports: !override + - "9080:8080" +``` diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 00000000000..1db4953b14e --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,36 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/php-mariadb +{ + "name": "GLPI docker devcontainer", + "dockerComposeFile": "docker-compose.yaml", + "service": "app", + "workspaceFolder": "/var/www/glpi", + + "forwardPorts": [8080, 8090, 8025], + "portsAttributes": { + "8080": { + "label": "GLPI" + }, + "8090": { + "label": "Adminer" + }, + "8025": { + "label": "Mailpit" + } + }, + + // Use 'postCreateCommand' to run commands after the container is created. + "postCreateCommand": "composer build", + + // automatically install these extensions when opening the dev container in vscode + "customizations": { + "vscode": { + "extensions": [ + "bmewburn.vscode-intelephense-client", + "xdebug.php-debug", + "mblode.twig-language-2", + "dbaeumer.vscode-eslint" + ] + } + } +} diff --git a/.devcontainer/docker-compose.yaml b/.devcontainer/docker-compose.yaml new file mode 100644 index 00000000000..9dcd27bc2d8 --- /dev/null +++ b/.devcontainer/docker-compose.yaml @@ -0,0 +1,51 @@ +services: + app: + container_name: "glpi-app" + build: + context: "../.docker/app" + restart: "unless-stopped" + volumes: + - "..:/var/www/glpi:rw" + ports: + - "8080:80" + depends_on: + - db + extra_hosts: + - "host.docker.internal:host-gateway" + + db: + container_name: "glpi-db" + image: "mariadb:11.4" + restart: "unless-stopped" + volumes: + - "db:/var/lib/mysql" + - "./initdb.sql:/docker-entrypoint-initdb.d/initdb.sql" + environment: + MARIADB_ROOT_PASSWORD: "glpi" + MARIADB_DATABASE: "glpi" + MARIADB_USER: "glpi" + MARIADB_PASSWORD: "glpi" + expose: + - "3306" + + mailpit: + container_name: "glpi-mailpit" + image: "axllent/mailpit" + restart: "unless-stopped" + expose: + - "1025" + ports: + - "8025:8025" + + adminer: + container_name: "glpi-adminer" + image: "adminer:latest" + restart: "unless-stopped" + ports: + - "8090:8080" + environment: + - "ADMINER_DEFAULT_SERVER=db" + command: ["php", "-S", "0.0.0.0:8080", "-t", "/var/www/html"] + +volumes: + db: diff --git a/.devcontainer/initdb.sql b/.devcontainer/initdb.sql new file mode 100644 index 00000000000..85aae264346 --- /dev/null +++ b/.devcontainer/initdb.sql @@ -0,0 +1,7 @@ +# This SQL queries contained in this file will be executed on every database container startup. + +# Required for timezones usage. +# This may be executed before automatic `glpi` user creation, so we have to create it manually. +CREATE USER IF NOT EXISTS 'glpi'@'%' IDENTIFIED BY 'glpi'; +SET PASSWORD FOR 'glpi'@'%' = PASSWORD('glpi'); +GRANT SELECT ON `mysql`.`time_zone_name` TO 'glpi'@'%'; diff --git a/.docker/README.md b/.docker/README.md new file mode 100644 index 00000000000..57407fb2d78 --- /dev/null +++ b/.docker/README.md @@ -0,0 +1,57 @@ +# GLPI docker development environment + +The docker development environment can be easilly instanciated by running the command `docker compose up -d` +from the GLPI root directory. + +## Custom configuration + +You can customize the docker services by creating a `docker-compose.override.yaml` file in the GLPI root directory. + +## HTTP server + +By default, the HTTP port is published to on the `8080` port on the host machine. +You can change it in the `docker-compose.override.yaml` file. + +```yaml +services: + app: + ports: !override + - "9000:80" +``` + +The default uid/gid used by the docker container is `1000`. If your host user uses different uid/gid, you may encounter +file permissions issues. To prevent this, you can customize them using the corresponding build args in +the `docker-compose.override.yaml` file. + +```yaml +services: + app: + build: + args: + HOST_GROUP_ID: "${HOST_GROUP_ID:-1000}" + HOST_USER_ID: "${HOST_USER_ID:-1000}" +``` + +### Database server + +By default, the database service is not provided. You can add it in the `docker-compose.override.yaml` file. + +```yaml +services: + database: + container_name: "db" + image: "mariadb:11.0" + restart: "unless-stopped" + environment: + MYSQL_ROOT_PASSWORD: "R00tP4ssw0rd" + MYSQL_DATABASE: "glpi" + MYSQL_USER: "glpi" + MYSQL_PASSWORD: "P4ssw0rd" + ports: + - "3306:3306" + volumes: + - "db:/var/lib/mysql" + +volumes: + db: +``` diff --git a/.docker/app/Dockerfile b/.docker/app/Dockerfile new file mode 100644 index 00000000000..46c4d9cf8d7 --- /dev/null +++ b/.docker/app/Dockerfile @@ -0,0 +1,21 @@ +FROM ghcr.io/glpi-project/glpi-development-env:latest + +USER root + +# Workaround to make apache use same UID/GID as host user. +ARG HOST_GROUP_ID=1000 +RUN groupmod --gid ${HOST_GROUP_ID} www-data +ARG HOST_USER_ID=1000 +RUN usermod --uid ${HOST_USER_ID} www-data + +# Allow login as www-data user and allow it to execute sudo +RUN usermod --shell /bin/bash www-data +RUN echo "www-data ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers + +# Make www-data user home persistent for cache purpose. +RUN mkdir --parents /home/www-data \ + && chown www-data:www-data /home/www-data \ + && usermod --home /home/www-data www-data +VOLUME /home/www-data + +USER www-data diff --git a/.gitignore b/.gitignore index 1e3a5eb1193..5860ca6f751 100644 --- a/.gitignore +++ b/.gitignore @@ -46,3 +46,5 @@ phpunit.xml /inventory-vendors/*.json /locales/*.mo /inc/downstream.php +/docker-compose.override.yaml +/.devcontainer/docker-compose.override.yaml diff --git a/composer.lock b/composer.lock index 5b252557fc9..c33aa74b529 100644 --- a/composer.lock +++ b/composer.lock @@ -5340,16 +5340,16 @@ }, { "name": "glpi-project/tools", - "version": "0.7.2", + "version": "0.7.3", "source": { "type": "git", "url": "https://github.com/glpi-project/tools.git", - "reference": "72fe4c075dc63712ae25a809a7a0e84a33a2640b" + "reference": "a076482b057a727a9dcf155af40dac6c26a7b7c6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/glpi-project/tools/zipball/72fe4c075dc63712ae25a809a7a0e84a33a2640b", - "reference": "72fe4c075dc63712ae25a809a7a0e84a33a2640b", + "url": "https://api.github.com/repos/glpi-project/tools/zipball/a076482b057a727a9dcf155af40dac6c26a7b7c6", + "reference": "a076482b057a727a9dcf155af40dac6c26a7b7c6", "shasum": "" }, "require": { @@ -5392,7 +5392,7 @@ "issues": "https://github.com/glpi-project/tools/issues", "source": "https://github.com/glpi-project/tools" }, - "time": "2024-01-10T09:01:18+00:00" + "time": "2024-06-20T08:36:22+00:00" }, { "name": "maglnet/composer-require-checker", diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 00000000000..c3de00fee0c --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,12 @@ +services: + app: + container_name: "glpi" + build: + context: ".docker/app" + restart: "unless-stopped" + volumes: + - ".:/var/www/glpi:rw" + ports: + - "8080:80" + extra_hosts: + - "host.docker.internal:host-gateway" diff --git a/tools/build_glpi.sh b/tools/build_glpi.sh index 22f9b468f90..8bbed955eee 100755 --- a/tools/build_glpi.sh +++ b/tools/build_glpi.sh @@ -76,6 +76,7 @@ find $WORKING_DIR -depth \( -iname ".*" ! -iname ".htaccess" \) -exec rm -rf {} dev_nodes=( "composer.json" "composer.lock" + "docker-compose.yaml" "ISSUE_TEMPLATE.md" "locales/glpi.pot" "node_modules"