Я пытаюсь создать конфигурацию докера с четырьмя контейнерами: - контейнер React - контейнер Symfony 4 - контейнер Mysql - и контейнер Nginx
Я сделал docker-compose, dockerfilesи все работает хорошо, за исключением случаев, когда я пытался получить доступ к своей базе данных из моего контейнера PHP.
Проводя исследования, я предполагаю, что проблема связана с моим docker-compose или моим файлом .env. Я попробовал много вещей, в основном, изменив соединение URL в файле .env. Вот мой URL: DATABASE_URL = mysql: // root: root@172.21.0.4: 3306 / Lucci
(Сейчас это IP-адрес контейнера. Я пытался изменить его с помощью "mysql "(имя контейнера)," 127.0.0.1 "," localhost "
Итак, вот мой docker-compose:
version: '3.7'
services:
mysql:
image: mysql:8.0
restart: on-failure
environment:
- MYSQL_ROOT_PASSWORD=root
ports:
- "3306:3306"
networks:
- back-network
php:
build:
context: .
dockerfile: docker/php/Dockerfile
depends_on:
- mysql
restart: on-failure
env_file:
- .env
user: 1000:1000
networks:
- back-network
react:
build:
context : ./LucciFront
dockerfile: Dockerfile
restart: on-failure
ports:
- "80:80"
networks:
- back-network
networks:
front-network:
driver: bridge
back-network:
driver: bridge
мой файл .env:
###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=010556a2891ecc0273593c31e17b9399
#TRUSTED_PROXIES=127.0.0.1,127.0.0.2
#TRUSTED_HOSTS='^localhost|example\.com$'
###< symfony/framework-bundle ###
###> doctrine/doctrine-bundle ###
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/ en/latest/reference/configuration.html#connecting-using-a-url
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# Configure your db driver and server_version in config/packages/doctrine.yaml
DATABASE_URL=mysql://root:root@172.21.0.4:3306/Lucci
###< doctrine/doctrine-bundle ###
###> symfony/swiftmailer-bundle ###
# For Gmail as a transport, use: "gmail://username:password@localhost"
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
# Delivery is disabled by default via "null://localhost"
MAILER_URL=null://localhost
###< symfony/swiftmailer-bundle ###
###> doctrine/mongodb-odm-bundle ###
MONGODB_URL=mongodb://localhost:27017
MONGODB_DB=symfony
###< doctrine/mongodb-odm-bundle ###
###> nelmio/cors-bundle ###
CORS_ALLOW_ORIGIN=^https?://localhost(:[0-9]+)?$
###< nelmio/cors-bundle ###
и мой dockerfile контейнера php:
FROM php:7.2-fpm
RUN chmod -R 777 ./
RUN docker-php-ext-install pdo_mysql
RUN pecl install apcu
RUN apt-get update && \
apt-get install -y \
zlib1g-dev
RUN apt-get update && \
apt-get -y install sudo
RUN docker-php-ext-install zip
RUN docker-php-ext-enable apcu
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
&& php -r "if (hash_file('SHA384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
&& php composer-setup.php --filename=composer \
&& php -r "unlink('composer-setup.php');" \
&& mv composer /usr/local/bin/composer
WORKDIR /usr/src/app
COPY apps/LucciBack /usr/src/app
RUN PATH=$PATH:/usr/src/apps/vendor/bin:bin
RUN composer install
RUN php bin/console doctrine:database:create
RUN php bin/console doctrine:schema:update --force
Когда я запускаю сборку docker-compose, я получаю:
Step 14/15 : RUN php bin/console doctrine:database:create
---> Running in 2c77cac8c604
In AbstractMySQLDriver.php line 93:
An exception occurred in driver: SQLSTATE[HY000] [2002] Connection timed ou
t
In PDOConnection.php line 31:
SQLSTATE[HY000] [2002] Connection timed out
In PDOConnection.php line 27:
SQLSTATE[HY000] [2002] Connection timed out
У меня нет ошибок, когдая запускаю «docker-compose up -d», но я думаю, что он просто проходит создание базы данных.
Edit: Вот последнее сообщение для каждого контейнера на «docker-compose up»:
mysql_1 | 2019-10-28T12:31:44.294975Z 0 [Warning] [MY-011810] [Server] Insecure co nfiguration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql_1 | 2019-10-28T12:31:44.309669Z 0 [System] [MY-010931] [Server] /usr/sbin/my sqld: ready for connections. Version: '8.0.17' socket: '/var/run/mysqld/mysqld.soc k' port: 3306 MySQL Community Server - GPL.
mysql_1 | 2019-10-28T12:31:44.423127Z 0 [System] [MY-011323] [Server] X Plugin rea dy for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
php_1 | [28-Oct-2019 12:31:43] NOTICE: ready to handle connections
mysql_1 | 2019-10-28T12:31:44.294975Z 0 [Warning] [MY-011810] [Server] Insecure co nfiguration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql_1 | 2019-10-28T12:31:44.309669Z 0 [System] [MY-010931] [Server] /usr/sbin/my sqld: ready for connections. Version: '8.0.17' socket: '/var/run/mysqld/mysqld.soc k' port: 3306 MySQL Community Server - GPL.
mysql_1 | 2019-10-28T12:31:44.423127Z 0 [System] [MY-011323] [Server] X Plugin rea dy for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
react_1 | 172.21.0.1 - - [28/Oct/2019:14:10:50 +0000] "GET /static/media/brand-icons.e8c322de.woff2 HTTP/1.1" 200 54488 "http://localhost/static/css/2.727d7221.chunk.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 OPR/63.0.3368.107" "-"
react_1 | 172.21.0.1 - - [28/Oct/2019:14:10:50 +0000] "GET /static/media/icons.0ab54153.woff2 HTTP/1.1" 200 40148 "http://localhost/static/css/2.727d7221.chunk.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 OPR/63.0.3368.107" "-"
react_1 | 172.21.0.1 - - [28/Oct/2019:14:10:51 +0000] "GET /favicon.ico HTTP/1.1" 200 3870 "http://localhost/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 OPR/63.0.3368.107" "-"
После запуска команды я могу получить доступ к своему приложению реакции, но ничего не получил из базы данных
Редактировать 2:
Вот сообщение об ошибке, когда я заменяю IP-адрес контейнерапо имени контейнера (здесь «mysql»).
In AbstractMySQLDriver.php line 93:
An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddr
esses: getaddrinfo failed: Name or service not known
In PDOConnection.php line 31:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name o
r service not known
In PDOConnection.php line 27:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name o
r service not known
In PDOConnection.php line 27:
PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or s
ervice not known
doctrine:database:create [--shard SHARD] [--connection [CONNECTION]] [--if-not-exists] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>
и вот мое URL-соединение: DATABASE_URL = mysql: // root: root @ mysql: 3306 / Lucci