I've been working on a Laravel microservice application, which I've containerized using Docker. Most of my endpoints work seamlessly both inside and outside the container. However, when my application tries to connect to MongoDB (especially for transactional queries), I consistently get the error: There is no active session.
, which originates from MongoDB\Driver\Exception.
Here's what I've confirmed so far:
- MongoDB replica set is set up correctly.
- The application connects and operates perfectly with MongoDB outside the Docker environment.
- All services and dependencies are running, and network configurations seem correct, as other endpoints in the same container work without any issue.
Here is a snippet from my Dockerfile for reference:
Dockerfile
FROM php:8.0-alpine AS php
WORKDIR /app
RUN apk add --no-cache \
autoconf \
build-base \
libpng-dev \
oniguruma-dev \
libxml2-dev \
imagemagick-dev \
libzip-dev \
git \
curl \
unzip \
openssl \
zip \
freetype-dev \
jpeg-dev \
libwebp-dev \
&& pecl install imagick \
&& docker-php-ext-enable imagick \
&& pecl install mongodb \
&& docker-php-ext-enable mongodb \
&& docker-php-ext-configure gd --with-freetype --with-webp --with-jpeg \
&& docker-php-ext-install pdo_mysql opcache gd zip \
&& pecl install swoole-4.8.4 \
&& docker-php-ext-enable swoole
# Stage - Production Image
FROM php:8.0-alpine
# Install runtime system dependencies for PHP extensions
RUN apk add --no-cache \
libpng \
imagemagick-libs \
libzip\
libgomp \
freetype \
jpeg \
libwebp \
libstdc++
COPY --from=php /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/
COPY --from=php /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/
COPY --from=php /usr/lib/ /usr/lib/
COPY . /var/www
WORKDIR /var/www
CMD ["php", "artisan", "octane:start","--port=8000", "--host=0.0.0.0"]
And the error log from Docker:
500 POST /master/login ................................. 23.35 mb 30.81 ms
Has anyone encountered this before or can provide some insight into what I might be missing or doing wrong? Any help would be greatly appreciated!