0

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!

Virendra
  • 665
  • 5
  • 6
  • Does this answer your question? [can't connect mongodb on host from docker container](https://stackoverflow.com/questions/73109508/cant-connect-mongodb-on-host-from-docker-container) – Pavel Bely Aug 29 '23 at 07:12
  • I've already check that post, but it's different. – Virendra Aug 29 '23 at 07:22
  • You're installing MongoDB, but where is it started ? – mbesson Aug 29 '23 at 08:17
  • MongoDB is outside the container. – Virendra Aug 29 '23 at 09:08
  • Do you mind sharing in which way your case is different? There is nothing in the question to answer. If you read it without context of your working environment, it literally says "I do everything right, but it doesn't work". 500 is a generic server error, and the "log" conveys no information but how much memory was used and how quick the request was. You get "There is no active session." from somewhere. At least post the full error traceback. – Alex Blex Aug 29 '23 at 13:35

0 Answers0