Aurélien W
Aurélien W

Reputation: 143

Cannot use mongodb (docker) with Symfony 3

I am having some issue integrating mongodb with Symfony 3.1.2. I use the DoctrineMongoDBBundle.

Everything is okay up to the 'Persisting Objects to MongoDB' stage. When I add the lines :

$dm = $this->get('doctrine_mongodb')->getManager();
$dm->persist($product);
$dm->flush();

nothing happens to my remote database and I get the error message:

Attempted to load class "MongoId" from the global namespace. Did you forget a "use" statement?

I use docker beta on Mac. My docker-compose.yml :

version: '2'
services:
    application_ad:
        image: busybox
        volumes:
            - ./:/var/www/html
            - ./docker/nginx/conf.d/ad.conf:/etc/nginx/conf.d/default.conf
        entrypoint: "tail -f /dev/null"
    php_ad:
        build: ./docker/php-fpm
        links:
            - db
        volumes_from:
            - application_ad
    web_ad:
        build: ./docker/nginx
        ports:
            - "8089:80"
        links:
            - php_ad
        volumes_from:
            - application_ad
    db:
      image: mongo:3.2.7
      volumes:
        - ./mongo-data:/data/db
      ports:
        - "27017:27017"

And the php-fpm file :

FROM php:7-fpm

MAINTAINER Me <[email protected]>

# nginx and php-fpm user MUST be the same
RUN usermod -u 1000 www-data

# set php.ini
RUN touch /usr/local/etc/php/conf.d/php.ini
RUN { \
        echo "date.timezone = Europe/Paris"; \
        echo "short_open_tag = off"; \
    } >> /usr/local/etc/php/conf.d/php.ini

# composer install
RUN curl -sS https://getcomposer.org/installer | php && mv composer.phar /usr/local/bin/composer

# install php extensions dependencies
RUN apt-get update && apt-get install -my mlocate wget \
 zlib1g-dev openssl libcurl4-openssl-dev libssl-dev pkg-config libgdbm-dev libjpeg-dev libpng-dev libmcrypt-dev libicu-dev g++

# install php extensions
RUN docker-php-ext-install curl
RUN docker-php-ext-configure gd --with-jpeg-dir="/usr/lib/x86_64-linux-gnu"
RUN docker-php-ext-install gd
RUN docker-php-ext-install json
RUN docker-php-ext-install mcrypt
RUN docker-php-ext-install mcrypt
RUN docker-php-ext-install intl
RUN docker-php-ext-install opcache
RUN pecl install apcu && pecl install channel://pecl.php.net/apcu_bc-1.0.3

# install mongo
RUN pecl install mongodb
RUN echo "extension=mongodb.so" > /usr/local/etc/php/conf.d/mongo.ini

# configure extensions
RUN { \
        echo 'zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20151012/opcache.so'; \
        echo 'opcache.memory_consumption=256'; \
        echo 'opcache.interned_strings_buffer=16'; \
        echo 'opcache.max_accelerated_files=20000'; \
        echo 'opcache.revalidate_freq=2'; \
        echo 'opcache.fast_shutdown=1'; \
        echo 'opcache.enable_cli=1'; \
    } > /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini

RUN { \
        echo 'extension=apcu.so'; \
        echo 'extension=apc.so'; \
        echo 'apc.enabled=1'; \
        echo 'apc.shm_size=256M'; \
        echo 'apc.ttl=7200'; \
        echo 'apc.enable_cli=1'; \
        echo 'apc.gc_ttl=3600'; \
        echo 'apc.entries_hint=4096'; \
        echo 'apc.slam_defense=1'; \
        echo 'apc.serializer=php'; \
    } > /usr/local/etc/php/conf.d/docker-php-ext-apc.ini

PHPInfo() :

enter image description here

Maybe a docker problem ? Or a Symfony problem .

Upvotes: 2

Views: 1718

Answers (1)

Hendra Huang
Hendra Huang

Reputation: 528

I am using PHP 7 with mongodb and using symfony and docker too. The only problem I have is the php-extension I needed to install is mongodb instead of mongo. After I install mongodb extension, everything works fine.

And here is my composer require:

"require": {
    "doctrine/mongodb-odm": "^1.1.0",
    "doctrine/mongodb-odm-bundle": "^3.1.0",
    "alcaeus/mongo-php-adapter": "^1.0.0",
    "ext-mongo": "*"
}

If it still fails when composer install, add this line below your composer require

"provide": {
    "ext-mongo": "1.6.13"
}

Hope it helps

Upvotes: 2

Related Questions