docker php mongodb passing links to mongodb connection

I am trying to connect to mongodb from PHP 7.

I have nginx container, php-fpm container and mongodb container.

  • Docker swarm nodes not showing up after restart
  • Error: “error creating aufs mount to” when building dockerfile
  • Google cloud sdks doesn't start preview for Docker image Mac OS
  • Illegal tokens added after modifying the file inside data volume
  • Install an sql dump file to a docker container with mariaDB
  • Nuget CA certificates error on a docker container of core
  • I installed mongodb driver using pecl and it is showing up in phpinfo(), mongodb ver. 1.2.3. Additionally I installed mongodb/mongodb using composer.

    I linked my containers to mongodb (eg --link mongodb_container:mongodb)

    Now when i am trying to:

    $collection = (new Client('mongodb'))->streamflixmongodb->videos;

    I would get an error:

    Fatal error: Uncaught MongoDB\Driver\Exception\InvalidArgumentException: 
    Failed to parse MongoDB URI: 'mongodb' in /vendor/mongodb/mongodb/src/Client.php:81 Stack trace: 
    #0 /vendor/mongodb/mongodb/src/Client.php(81): MongoDB\Driver\Manager->__construct('mongodb', Array, Array) 
    #1 /var/www/html/test.php(24): MongoDB\Client->__construct('mongodb') 
    #2 {main} thrown in /vendor/mongodb/mongodb/src/Client.php on line 81

    Because connections should start with mongodb://

    Additionally if I try to:

    $collection = (new Client('mongodb://mongodb:27015'))->streamflixmongodb->videos;

    I will get the following error:

    Fatal error: Uncaught MongoDB\Driver\Exception\ConnectionTimeoutException: 
    No suitable servers found (`serverSelectionTryOnce` set): 
    [connection timeout calling ismaster on 'mongodb:27015'] in
    /vendor/mongodb/mongodb/src/Collection.php:513 Stack trace: #0 
    #1 /var/www/html/test.php(27): MongoDB\Collection->find('.mp4') 
    #2 {main} thrown in /vendor/mongodb/mongodb/src/Collection.php on line 513

    The errors are generated when I run:


    What is the right way to pass links to new mongodb in PHP? Previously I used older mongo with php5 and it was fine.

    my docker ps -a

    93ce6947f2d8        vignatjevs/nginx        "/bin/sh -c 'servi..."   2 days ago          Up 57 seconds    >80/tcp,>443/tcp         streamflix
    569307612b50        vignatjevs/php-fpm      "/app-entrypoint.s..."   2 days ago          Up 2 minutes               9000/tcp                                             streamflix-php-fpm
    03decea6dfa7        composer                "/docker-entrypoin..."   2 days ago          Exited (0) 2 minutes ago                                                        composer
    c01b48fcdda3        jenkins                 "/bin/tini -- /usr..."   3 days ago          Up 21 hours                50000/tcp,>8080/tcp                   clever_swartz
    d16ace0397be        tutum/mongodb           "mongod --smallfiles"    6 days ago          Up 21 hours      >27017/tcp,>28017/tcp   mongodb_streamflix
    72e7bca2e2b7        phpmyadmin/phpmyadmin   "/ phpmyadmin"     12 days ago         Up 21 hours      >80/tcp                                 streamflix_admin
    a0988be5018a        centurylink/mysql       "/usr/local/bin/run"     12 days ago         Up 21 hours      >3306/tcp,>3306/tcp       streamflix-mysql-server

  • Viewing output of stopped Docker container
  • Docker | How to build and deploy war file in jboss container hosted in Docker
  • Docker image of Java project
  • How to run docker containers in their network with an external gateway?
  • How to rename exported environment variable in docker
  • Installing a specific version of docker-engine (v1.7+) on Ubuntu?
  • One Solution collect form web for “docker php mongodb passing links to mongodb connection”

    Found a solution. The problem was that i put SOME of my containers on the network, giving a parameter like --network app-tier

    docker run -d --name streamflix-php-fpm --network app-tier -v 
    /Volumes/Data/MSc\ Stuff/DEVOPS/streamflix-docker/php-fpm:/bitnami/php-fpm
    -v /Volumes/Data/MSc\ Stuff/DEVOPS/streamflix-docker/streamflix:/var/www/html
    --link streamflix-mysql-server:mysqldb 
    --link mongodb_streamflix:mongodb 
    --link videoserv:videoserver   vignatjevs/php-fpm

    I can’t believe i spent the most of the day because couldnt spot this 😀

    Obviously if I created network for some containers, I had to put them all on it, or remove --network paramater completely from anywhere to make it consistent.

    Hope this helps somebody who gets stuck with the same problem.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.