From 2304fb2462c29e91897d9972f9995fa7d3cfd327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 7 Nov 2024 22:48:42 +0100 Subject: [PATCH] Add MongoDB extension and service (#748) * Add MongoDB service Use mongodb-atlas-local to provide most cloud features locally * Update CHANGELOG.md * Update CHANGELOG.md --------- Co-authored-by: Taylor Otwell --- bin/sail | 13 +++++++++++++ runtimes/8.0/Dockerfile | 2 +- runtimes/8.1/Dockerfile | 2 +- runtimes/8.2/Dockerfile | 2 +- runtimes/8.3/Dockerfile | 2 +- runtimes/8.4/Dockerfile | 2 +- .../InteractsWithDockerComposeServices.php | 8 +++++++- stubs/mongodb.stub | 19 +++++++++++++++++++ 8 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 stubs/mongodb.stub diff --git a/bin/sail b/bin/sail index c34286ac..91e3fc18 100755 --- a/bin/sail +++ b/bin/sail @@ -83,6 +83,7 @@ function display_help { echo " ${GREEN}sail mysql${NC} Start a MySQL CLI session within the 'mysql' container" echo " ${GREEN}sail mariadb${NC} Start a MySQL CLI session within the 'mariadb' container" echo " ${GREEN}sail psql${NC} Start a PostgreSQL CLI session within the 'pgsql' container" + echo " ${GREEN}sail mongodb${NC} Start a Mongo Shell session within the 'mongodb' container" echo " ${GREEN}sail redis${NC} Start a Redis CLI session within the 'redis' container" echo echo "${YELLOW}Debugging:${NC}" @@ -526,6 +527,18 @@ elif [ "$1" == "root-shell" ] || [ "$1" == "root-bash" ]; then sail_is_not_running fi +# Initiate a MongoDB Shell within the "mongodb" container... +elif [ "$1" == "mongodb" ]; then + shift 1 + + if [ "$EXEC" == "yes" ]; then + ARGS+=(exec) + [ ! -t 0 ] && ARGS+=(-T) + ARGS+=(mongodb mongosh --port "${FORWARD_MONGODB_PORT:-27017}" --username "$MONGODB_USERNAME" --password "$MONGODB_PASSWORD" --authenticationDatabase admin) + else + sail_is_not_running + fi + # Initiate a Redis CLI terminal session within the "redis" container... elif [ "$1" == "redis" ]; then shift 1 diff --git a/runtimes/8.0/Dockerfile b/runtimes/8.0/Dockerfile index 1c5b61a8..e1254b18 100644 --- a/runtimes/8.0/Dockerfile +++ b/runtimes/8.0/Dockerfile @@ -27,7 +27,7 @@ RUN apt-get update && apt-get upgrade -y \ && apt-get update \ && apt-get install -y php8.0-cli php8.0-dev \ php8.0-pgsql php8.0-sqlite3 php8.0-gd php8.0-imagick \ - php8.0-curl php8.0-memcached \ + php8.0-curl php8.0-memcached php8.0-mongodb \ php8.0-imap php8.0-mysql php8.0-mbstring \ php8.0-xml php8.0-zip php8.0-bcmath php8.0-soap \ php8.0-intl php8.0-readline php8.0-pcov \ diff --git a/runtimes/8.1/Dockerfile b/runtimes/8.1/Dockerfile index 714a263c..ab1ee7cd 100644 --- a/runtimes/8.1/Dockerfile +++ b/runtimes/8.1/Dockerfile @@ -27,7 +27,7 @@ RUN apt-get update && apt-get upgrade -y \ && apt-get update \ && apt-get install -y php8.1-cli php8.1-dev \ php8.1-pgsql php8.1-sqlite3 php8.1-gd php8.1-imagick \ - php8.1-curl \ + php8.1-curl php8.1-mongodb \ php8.1-imap php8.1-mysql php8.1-mbstring \ php8.1-xml php8.1-zip php8.1-bcmath php8.1-soap \ php8.1-intl php8.1-readline \ diff --git a/runtimes/8.2/Dockerfile b/runtimes/8.2/Dockerfile index d59863d8..3aacc61c 100644 --- a/runtimes/8.2/Dockerfile +++ b/runtimes/8.2/Dockerfile @@ -27,7 +27,7 @@ RUN apt-get update && apt-get upgrade -y \ && apt-get update \ && apt-get install -y php8.2-cli php8.2-dev \ php8.2-pgsql php8.2-sqlite3 php8.2-gd php8.2-imagick \ - php8.2-curl \ + php8.2-curl php8.2-mongodb \ php8.2-imap php8.2-mysql php8.2-mbstring \ php8.2-xml php8.2-zip php8.2-bcmath php8.2-soap \ php8.2-intl php8.2-readline \ diff --git a/runtimes/8.3/Dockerfile b/runtimes/8.3/Dockerfile index 51db97a1..2b84991c 100644 --- a/runtimes/8.3/Dockerfile +++ b/runtimes/8.3/Dockerfile @@ -28,7 +28,7 @@ RUN apt-get update && apt-get upgrade -y \ && apt-get update \ && apt-get install -y php8.3-cli php8.3-dev \ php8.3-pgsql php8.3-sqlite3 php8.3-gd \ - php8.3-curl \ + php8.3-curl php8.3-mongodb \ php8.3-imap php8.3-mysql php8.3-mbstring \ php8.3-xml php8.3-zip php8.3-bcmath php8.3-soap \ php8.3-intl php8.3-readline \ diff --git a/runtimes/8.4/Dockerfile b/runtimes/8.4/Dockerfile index 12ccf6c3..76018cfe 100644 --- a/runtimes/8.4/Dockerfile +++ b/runtimes/8.4/Dockerfile @@ -28,7 +28,7 @@ RUN apt-get update && apt-get upgrade -y \ && apt-get update \ && apt-get install -y php8.4-cli php8.4-dev \ php8.4-pgsql php8.4-sqlite3 php8.4-gd \ - php8.4-curl \ + php8.4-curl php8.4-mongodb \ php8.4-imap php8.4-mysql php8.4-mbstring \ php8.4-xml php8.4-zip php8.4-bcmath php8.4-soap \ php8.4-intl php8.4-readline \ diff --git a/src/Console/Concerns/InteractsWithDockerComposeServices.php b/src/Console/Concerns/InteractsWithDockerComposeServices.php index a4c3bfff..3e287061 100644 --- a/src/Console/Concerns/InteractsWithDockerComposeServices.php +++ b/src/Console/Concerns/InteractsWithDockerComposeServices.php @@ -16,6 +16,7 @@ trait InteractsWithDockerComposeServices 'mysql', 'pgsql', 'mariadb', + 'mongodb', 'redis', 'memcached', 'meilisearch', @@ -92,7 +93,7 @@ protected function buildDockerCompose(array $services) // Merge volumes... collect($services) ->filter(function ($service) { - return in_array($service, ['mysql', 'pgsql', 'mariadb', 'redis', 'meilisearch', 'typesense', 'minio']); + return in_array($service, ['mysql', 'pgsql', 'mariadb', 'mongodb', 'redis', 'meilisearch', 'typesense', 'minio']); })->filter(function ($service) use ($compose) { return ! array_key_exists($service, $compose['volumes'] ?? []); })->each(function ($service) use (&$compose) { @@ -163,6 +164,11 @@ protected function replaceEnvVariables(array $services) $environment = str_replace('REDIS_HOST=127.0.0.1', 'REDIS_HOST=redis', $environment); } + if (in_array('mongodb', $services)) { + $environment .= "\nMONGODB_URI=mongodb://mongodb:27017"; + $environment .= "\nMONGODB_DATABASE=laravel"; + } + if (in_array('meilisearch', $services)) { $environment .= "\nSCOUT_DRIVER=meilisearch"; $environment .= "\nMEILISEARCH_HOST=http://meilisearch:7700\n"; diff --git a/stubs/mongodb.stub b/stubs/mongodb.stub new file mode 100644 index 00000000..c6db3193 --- /dev/null +++ b/stubs/mongodb.stub @@ -0,0 +1,19 @@ +mongodb: + image: 'mongodb/mongodb-atlas-local:latest' + environment: + - MONGODB_INITDB_ROOT_USERNAME=${MONGODB_USERNAME:-} + - MONGODB_INITDB_ROOT_PASSWORD=${MONGODB_PASSWORD:-} + volumes: + - 'sail-mongodb:/data/db' + ports: + - '${FORWARD_MONGODB_PORT:-27017}:27017' + networks: + - sail + healthcheck: + test: + - CMD + - mongosh + - 'mongodb://localhost:27017/admin' + - '--eval=db.runCommand({ping:1})' + retries: 3 + timeout: 5s