由于投了反对票,我重构了这个问题。
就像这样简单:
外部Docker容器:
➜ backoffice git:(master) ✗ php artisan tinker
Psy Shell v0.9.9 (PHP 7.2.15 — cli) by Justin Hileman
>>> $ur = app(MDiPaolo\Repositories\UserRepository::class)
=> App\Infrastructure\Repositories\Doctrine\UserDoctrineRepository {#3159}
>>> $u = new MDiPaolo\Entities\User
=> MDiPaolo\Entities\User {#3295}
>>> $u->setEmail('one_email@gmail.com')
=> MDiPaolo\Entities\User {#3295}
>>> $u->setPassword(password_hash('1234', PASSWORD_BCRYPT))
=> null
>>> $ur->save($u)
=> null
>>>
在docker容器内部:
➜ backoffice git:(master) ✗ docker exec -it backoffice_web_1 bash
root@042969f0229c:/var/www/html# php artisan tinker
Psy Shell v0.9.9 (PHP 7.2.17 — cli) by Justin Hileman
>>> $ur = app(MDiPaolo\Repositories\UserRepository::class)
Doctrine/DBAL/Exception/ConnectionException with message 'An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused'
>>>
我有种感觉,它与docker系统有关,例如两个容器之间的通信或我如何构建它们。
这是我的Dockerfile。
FROM php:7.2-apache
RUN docker-php-ext-install pdo_mysql && docker-php-ext-enable pdo_mysql
RUN apt-get update && apt-get install nano && mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
RUN a2enmod rewrite
RUN service apache2 restart
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
docker-compose文件:
version: '3.1'
services:
web:
build: .
ports:
- "8080:80"
links:
- mysql
depends_on:
- mysql
volumes:
- ./:/var/www/html
mysql:
image: mysql:5.7.25
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: backoffice
holdtom