Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

php - 无法在docker-compose(php和MariaDB)中连接到数据库服务器(Cannot not connect to database server within docker-compose (php and MariaDB))

I've built a docker-compose file that creates 2 services (PHP and MariaDB).

(我已经构建了一个docker-compose文件,该文件创建了2个服务(PHP和MariaDB)。)

Somehow I cannot connect to the database from the PHP service: Within the PHP service, a Laravel-app is running.

(我无法以某种方式从PHP服务连接到数据库:在PHP服务中,一个Laravel应用程序正在运行。)

The error message (redirect is a table within the database):

(错误消息(重定向是数据库中的表):)

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from redirects )

(SQLSTATE [HY000] [2002]连接被拒绝(SQL:从redirects选择*))

All env-variables should be correct.

(所有环境变量都应正确。)

Here is my docker-compose.yml:

(这是我的docker-compose.yml:)

version: "3.7"
services:
  faaren_backend:
    image: php:alpine
    container_name: FAA-Backend
    volumes:
      - "./:/faaren_backend"
    working_dir: /faaren_backend
    command: "php artisan serve --host=0.0.0.0 --port=8000"
    ports:
      - 8000:8000
    build:
      context: docker/php
      dockerfile: dev.Dockerfile

  faaren_database:
    image: mariadb
    container_name: FAA-Database
    ports:
    - "3306:3306"
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
      MYSQL_DATABASE: ${DB_DATABASE}
    volumes:
    - "faa-db-data:/var/lib/mysql/data"

volumes:
  faa-db-data: {}

My dev.Dockerfile

(我的开发人员Dockerfile)

FROM php:7.3-fpm

RUN apt-get update
RUN apt-get update && apt-get install -y libzip-dev 
         && docker-php-ext-install zip

RUN apt-get update && apt-get install -y libmagickwand-dev --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN pecl install imagick
RUN docker-php-ext-enable imagick

RUN apt-get install $PHPIZE_DEPS && 
    pecl install xdebug && docker-php-ext-enable xdebug && 
    docker-php-ext-install pdo_mysql pcntl

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

VOLUME /faaren_backend

And finally my .env-file:

(最后是我的.env文件:)

DB_CONNECTION=mysql
DB_HOST=faaren_backend
DB_PORT=3306
DB_DATABASE=faaren_db
DB_USERNAME=root
DB_PASSWORD=

Mostly I've followed this tutorial: https://medium.com/swlh/laravel-with-docker-compose-de2190569084

(大多数情况下,我都遵循本教程: https : //medium.com/swlh/laravel-with-docker-compose-de2190569084)

  ask by Brotzka translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Looks like you deal with a race condition.

(看起来您正在处理比赛条件。)

You need to use depends_on directive to control startup order and use wait script to make sure that service ready to accept connections:

(您需要使用depends_on指令来控制启动顺序,并使用等待脚本来确保该服务准备好接受连接:)

version: "3.7"
services:
  faaren_backend:
    image: php:alpine
    container_name: FAA-Backend
    volumes:
      - "./:/faaren_backend"
    working_dir: /faaren_backend
    command: entrypoint.sh
    ports:
      - 8000:8000
    depends_on:
      - faaren_database
    build:
      context: docker/php
      dockerfile: dev.Dockerfile

  faaren_database:
    image: mariadb
    container_name: FAA-Database
    ports:
      - "3306:3306"
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
      MYSQL_DATABASE: ${DB_DATABASE}
    volumes:
      - "faa-db-data:/var/lib/mysql/data"

volumes:
  faa-db-data: {}

There are a bunch of ways to check service availability, sometimes wait-for-it can be really handy.

(有很多检查服务可用性的方法,有时等待它会非常方便。)

In the example below used netcat (you need to install it into the container).

(在下面的示例中使用了netcat (您需要将其安装到容器中)。)

Also, it'a good idea to put a script into a separated file:

(另外,将脚本放入单独的文件中也是一个好主意:)

entrypoint.sh

(入口点)

#!/usr/bin/env bash
until nc -w 1 -z faaren_database 3306; do
  >&2 echo "Mysql is unavailable - sleeping"
  sleep 1
done
sleep 10
>&2 echo "Mysql is up - executing command"

php artisan serve --host=0.0.0.0 --port=8000

UPDATE : Specify correct DB_HOST how Dmitry suggested as well.

(更新 :指定正确的DB_HOST以及Dmitry的建议方式。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...