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.

  • Node http request to Docker Remote API - No response
  • how to run docker commands inside jenkins pipeline jobs
  • How to implement my project with docker and git
  • expose heroku $PORT with gulp task
  • How can I find the docker name from a program running inside Docker
  • docker-compose error when I run bundle
  • 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 
    /vendor/mongodb/mongodb/src/Collection.php(513): 
    MongoDB\Driver\Manager->selectServer(Object(MongoDB\Driver\ReadPreference))
    #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:

    var_dump($collection->find());
    

    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              0.0.0.0:8082->80/tcp, 0.0.0.0:32814->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, 0.0.0.0:49001->8080/tcp                   clever_swartz
    d16ace0397be        tutum/mongodb           "mongod --smallfiles"    6 days ago          Up 21 hours                0.0.0.0:27017->27017/tcp, 0.0.0.0:28017->28017/tcp   mongodb_streamflix
    72e7bca2e2b7        phpmyadmin/phpmyadmin   "/run.sh phpmyadmin"     12 days ago         Up 21 hours                0.0.0.0:8080->80/tcp                                 streamflix_admin
    a0988be5018a        centurylink/mysql       "/usr/local/bin/run"     12 days ago         Up 21 hours                0.0.0.0:3306->3306/tcp, 0.0.0.0:6603->3306/tcp       streamflix-mysql-server
    

  • install docker on network drive
  • Transferring Docker run script to compose file - postgres doesn't run?
  • Build failed while appending line in source of docker container
  • Setting the hostname for a container running in Kubernetes
  • CV2 Install Docker Windows (OpenCV)
  • Python psycopg2 - Work with large data
  • 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.