du2229 2017-04-22 23:19
浏览 72
已采纳

MongoDB不能与Docker中的Symfony应用程序一起使用

I have a Symfony app for a REST API that run in Docker container.

All routes works but when I try to access a route for add information in database or extract information from database I receive the message

Oops! An Error Occurred

The server returned a "500 Internal Server Error".

Something is broken. Please let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused.

docker-compose file created* with PHPDocker is

version: "3.1"
services:
  mongo:
    image: mongo:3.0
    container_name: mongo
    command: mongod --smallfiles
    expose:
      - 27017

  webserver:
    tty: true
    image: phpdockerio/nginx:latest
    container_name: code-webserver
    working_dir: /application
    volumes:
        - .:/application
        - ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
    ports:
    - "8000:80"

  php-fpm:
    build:
      context: .
      dockerfile: phpdocker/php-fpm/Dockerfile
    container_name: code-php-fpm
    ### MOVE ENVIRONMENT VARIABLES HERE
    environment:
      SYMFONY__MONGO_ADDRESS: mongo
      SYMFONY__MONGO_PORT: 27017
    working_dir: /application
    volumes:
      - .:/application
      - ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php5/fpm/conf.d/99-overrides.ini

*I tried to added links to mongo image, but doesn't work.

Result of command docker-compose up.

PS E:\API\web_server\code> docker-compose up
Starting code-php-fpm
Starting mongo
Starting code-webserver
Attaching to mongo, code-php-fpm, code-webserver
mongo        | 2017-04-22T23:11:52.947+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/
db 64-bit host=5c9a4a252688
mongo        | 2017-04-22T23:11:52.947+0000 I CONTROL  [initandlisten] db version v3.0.14
mongo        | 2017-04-22T23:11:52.947+0000 I CONTROL  [initandlisten] git version: 08352afcca24bfc145240a0fac9d28b978ab
77f3
code-php-fpm | [22-Apr-2017 23:11:52] NOTICE: fpm is running, pid 5
mongo        | 2017-04-22T23:11:52.947+0000 I CONTROL  [initandlisten] build info: Linux ip-10-30-223-232 3.2.0-4-amd64
#1 SMP Debian 3.2.46-1 x86_64 BOOST_LIB_VERSION=1_49
mongo        | 2017-04-22T23:11:52.947+0000 I CONTROL  [initandlisten] allocator: tcmalloc
code-php-fpm | [22-Apr-2017 23:11:52] NOTICE: ready to handle connections
mongo        | 2017-04-22T23:11:52.947+0000 I CONTROL  [initandlisten] options: { storage: { mmapv1: { smallFiles: true
} } }
code-php-fpm | [22-Apr-2017 23:11:52] NOTICE: systemd monitor interval set to 10000ms
mongo        | 2017-04-22T23:11:52.959+0000 I JOURNAL  [initandlisten] journal dir=/data/db/journal
mongo        | 2017-04-22T23:11:52.959+0000 I JOURNAL  [initandlisten] recover : no journal files present, no recovery n
eeded
mongo        | 2017-04-22T23:11:53.395+0000 I JOURNAL  [durability] Durability thread started
mongo        | 2017-04-22T23:11:53.395+0000 I JOURNAL  [journal writer] Journal writer thread started
mongo        | 2017-04-22T23:11:53.653+0000 I NETWORK  [initandlisten] waiting for connections on port 27017

Log when I tried to access a simple page (localhost:8000) from application - it's ok

code-php-fpm | 172.19.0.4 - 22/Apr/2017:23:24:52 +0000 "GET /app.php" 200

Log when I tried to access an url (localhost:8000/transactions/user=invalid&day=1492453903&threshold=100) valid but with invalid data - it's ok

code-php-fpm | 172.19.0.4 - 22/Apr/2017:23:23:32 +0000 "GET /app.php" 400

Log when I tried to access an url (localhost:8000/transactions/user=100&day=1492453903&threshold=100) valid with a valid data from insert in mongo database - isn't ok

code-php-fpm | 172.19.0.4 - 22/Apr/2017:23:13:09 +0000 "GET /app.php" 500

'parameters.yml' file

# This file is auto-generated during the composer install
parameters:
    database_host: 127.0.0.1
    database_port: null
    database_name: symfony
    database_user: root
    database_password: null
    mailer_transport: smtp
    mailer_host: 127.0.0.1
    mailer_user: null
    mailer_password: null
    secret: ThisTokenIsNotSoSecretChangeIt
    mongodb_server: 'mongodb://mongo:27017'

config.yml file contain

doctrine_mongodb:
    connections:
        default:
            server: "%mongodb_server%"
            options: {}
    default_database: hootsuite_database
    document_managers:
        default:
            mappings:
                DBBundle: ~

Logs from Symfony project:

[2017-04-23 07:03:25] request.INFO: Matched route "db_transaction_gettransactions". {"route":"db_transaction_gettransactions","route_parameters":{"_controller":"DBBundle\\Controller\\TransactionController::getTransactionsAction","_route":"db_transaction_gettransactions"},"request_uri":"http://localhost:8000/transactions/?day=1492453903&threshold=123&user=500","method":"GET"} []
[2017-04-23 07:03:25] security.INFO: Populated the TokenStorage with an anonymous Token. [] []
[2017-04-23 07:03:25] request.CRITICAL: Uncaught PHP Exception MongoConnectionException: "Failed to connect to: localhost:27017: Connection refused" at /application/vendor/doctrine/mongodb/lib/Doctrine/MongoDB/Connection.php line 282 {"exception":"[object] (MongoConnectionException(code: 71): Failed to connect to: localhost:27017: Connection refused at /application/vendor/doctrine/mongodb/lib/Doctrine/MongoDB/Connection.php:282)"} []

When I try to view database with Robomongo on localhost:27017 works.

Any advice? Thanks!

  • 写回答

1条回答 默认 最新

  • dongmisa4779 2017-04-22 23:56
    关注

    Two problems:

    1. You're setting environment variables on the nginx server instead of the php/symfony server
    2. Your links are also on the nginx server. The good news is that newer versions of docker-compose don't require you to use links anymore. docker-compose creates a bridge network by default for your project. All services running on this bridge network have the ability to connect to each other by default.

    I got rid of these link blocks and moved the environment variables from nginx to php.

    version: "3.1"
    services:
      mongo:
        image: mongo:3.0
        container_name: mongo
        command: mongod --smallfiles
        expose:
          - 27017
    
      webserver:
        tty: true
        image: phpdockerio/nginx:latest
        container_name: code-webserver
        working_dir: /application
        volumes:
            - .:/application
            - ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
        ports:
        - "8000:80"
    
      php-fpm:
        build:
          context: .
          dockerfile: phpdocker/php-fpm/Dockerfile
        container_name: code-php-fpm
        ### MOVE ENVIRONMENT VARIABLES HERE
        environment:
          SYMFONY__MONGO_ADDRESS: mongo
          SYMFONY__MONGO_PORT: 27017
        working_dir: /application
        volumes:
          - .:/application
          - ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php5/fpm/conf.d/99-overrides.ini
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大