无法将Symfony 4容器与MySQL Docker连接

时间:2019-10-28 13:04:13

标签: php mysql docker symfony

我正在尝试使用四个容器进行docker配置: -一个React容器 -Symfony 4容器 -一个Mysql容器 -和Nginx容器

我已经做了一个docker-compose,dockerfiles,并且一切运行良好,除非我尝试从我的PHP容器访问数据库。

通过研究,我猜问题出在我的docker-compose或.env文件中。 我做了很多尝试,主要是通过更改.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 ###

和我的php容器的dockerfile:

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 build时,我得到了:

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”时,我没有任何错误,但是我想它只是通过了数据库创建。

编辑: 这是“ 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" "-"

运行命令后,我可以访问我的react应用程序,但是从数据库中什么也没得到

编辑2:

当我用容器的名称(此处为“ mysql”)替换容器的IP地址时,这是错误消息。


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

0 个答案:

没有答案