【发布时间】:2017-04-22 23:19:46
【问题描述】:
我有一个 Symfony 应用程序,用于在 Docker 容器中运行的 REST API。
所有路由都有效,但是当我尝试访问路由以在数据库中添加信息或从数据库中提取信息时,我会收到消息
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.
使用PHPDocker 创建的docker-compose 文件是
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
*我尝试添加指向 mongo 图像的链接,但不起作用。
命令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
当我尝试从应用程序访问简单页面 (localhost:8000) 时记录 - 没关系
code-php-fpm | 172.19.0.4 - 22/Apr/2017:23:24:52 +0000 "GET /app.php" 200
当我尝试访问有效但数据无效的 url (localhost:8000/transactions/user=invalid&day=1492453903&threshold=100) 时记录 - 没关系
code-php-fpm | 172.19.0.4 - 22/Apr/2017:23:23:32 +0000 "GET /app.php" 400
当我尝试使用来自 mongo 数据库中插入的有效数据访问有效的 url (localhost:8000/transactions/user=100&day=1492453903&threshold=100) 时记录 - 不行
code-php-fpm | 172.19.0.4 - 22/Apr/2017:23:13:09 +0000 "GET /app.php" 500
'parameters.yml' 文件
# 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文件包含
doctrine_mongodb:
connections:
default:
server: "%mongodb_server%"
options: {}
default_database: hootsuite_database
document_managers:
default:
mappings:
DBBundle: ~
来自 Symfony 项目的日志:
[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)"} []
当我尝试在localhost:27017 上使用 Robomongo 查看数据库时。
有什么建议吗? 谢谢!
【问题讨论】:
-
500 表示内部服务器错误。我认为您缺少 Symfony 的日志...您检查过
/project/var/logs吗? -
应用没有连接到 MongoDB。我在帖子中发布了日志。我该怎么做?
-
它仍在尝试连接到“localhost:27017”而不是“mongo:27017”。您可以发布您的
app/config/config.yml文件吗? -
如果 robomongo 适合您,那么这可能意味着您有两个 mongodb 服务正在运行。一个在您的主机上,另一个在容器中。无论如何,为了让 PHP 访问 mongo,您需要告诉您的框架 Symfony,mongo 主机名为
mongo,而不是localhost。 -
我将 mongodb_server 从 'mongodb://localhost:27017' 更改为 'mongodb://mongo:27017' 并再次创建了容器,但错误仍然存在。我应该换个地方吗?
标签: php mongodb symfony docker docker-compose