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.

  • How to view Docker image layers on Docker Hub?
  • Docker user in and out of container: what is the correspondence (UID/GID), seeing effects I don't understand
  • Running private_pub with docker
  • docker login fails -> x509: certificate signed by unknown authority .. “crypto/rsa: verification error”
  • Cant build a docker dart-runtime image after a local pub-get
  • How to edit a file dynamically in a running docker container
  • 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

  • Difference between containers (Docker) and IIS
  • docker: how to do/setup networking container-to-host for udp / snmp
  • Logging solution for multiple containers running on same host
  • Certificate issue when using artifactory as docker registry
  • Excluding all hidden files with .dockerignore
  • Docker on windows 7: hello-world runs on Docker Quickstart Terminal but not on command prompt. why?
  • 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.