Dockerising Laravel 项目数据库连接问题

Dockerising Laravel Project Database connection issue

您好,我正在 docker 构建我的 laravel 项目,但在最后阶段我在命令 运行 下遇到错误

sudo docker-compose up

下面是上述命令的日志

db_1   | Version: '5.7.33'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL 
Community Server (GPL)
app_1  | 
app_1  |    Illuminate\Database\QueryException 
app_1  | 
app_1  |   SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from 
information_schema.tables where table_schema = foomotion_website and table_name = migrations 
and table_type = 'BASE TABLE')
app_1  | 
app_1  |   at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
app_1  |     667|         // If an exception occurs when attempting to run a query, we'll 
format the error
app_1  |     668|         // message to include the bindings with SQL, which will make this 
exception a
app_1  |     669|         // lot more helpful to the developer instead of just the 
database's errors.
app_1  |     670|         catch (Exception $e) {
app_1  |   > 671|             throw new QueryException(
app_1  |     672|                 $query, $this->prepareBindings($bindings), $e
app_1  |     673|             );
app_1  |     674|         }
app_1  |     675|
app_1  | 
app_1  |       +35 vendor frames 
app_1  |   36  artisan:37
app_1  |       

Illuminate\Foundation\Console\Kernel::handle(对象(Symfony\Component\Console\Input\ArgvInput), 对象(Symfony\Component\Console\Output\ConsoleOutput)) foomotionwebsite_app_1 已退出,代码为 1

可能是端口冲突或任何命令错误

这是我的 Docke 文件

FROM php:7
RUN apt-get update -y && apt-get install -y openssl zip unzip git
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --        
filename=composer
RUN docker-php-ext-install pdo pdo_mysql
WORKDIR /app
COPY . /app
RUN apt-get update && apt-get install -y \
  libfreetype6-dev \
  libjpeg62-turbo-dev \
  libmcrypt-dev \
  libpng-dev \
  && docker-php-ext-install -j$(nproc) iconv \
  && docker-php-ext-configure gd \
  && docker-php-ext-install -j$(nproc) gd
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
RUN install-php-extensions zip
RUN composer install
CMD php artisan db:create
CMD php artisan serve --host=0.0.0.0 --port=8181
EXPOSE 8181

这里是docker-compose.yml

version: '2'
services:
  app:
   build: .
   ports:
    - "8009:8000"
  volumes:
    - .:/app
env_file: .env.docker
working_dir: /app
command: bash -c 'php artisan migrate && php artisan serve --host 0.0.0.0'
depends_on:
  - db
links:
  - db
db:
   image: "mysql:5.7"
  environment:
  - MYSQL_ROOT_PASSWORD=
  - MYSQL_DATABASE=mydb
  - MYSQL_USER=root
  - MYSQL_PASSWORD=mypassword
volumes:
  - /home/dockers/data/:/var/lib/mysql
ports:
  - "3307:3306"

这里是 .env.docker 文件

APP_NAME=Foomotion
APP_ENV=local
APP_KEY=base64:HHUfXDkDt5NXH0wIL7smFDRHc3Rg7pNBRn1xfRQFb0I=
APP_DEBUG=true
APP_URL=localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3307
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=mypassword
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
#MAIL_MAILER=
#MAIL_HOST=
#MAIL_PORT=
#MAIL_USERNAME=
#MAIL_PASSWORD=
#MAIL_ENCRYPTION=
#MAIL_FROM_NAME=
#MAIL_FROM_ADDRESS=
#MAIL_MAILER=smtp
#MAIL_HOST=smtp.sendgrid.net
#MAIL_PORT=587
#MAIL_USERNAME=apikey
#MAIL_PASSWORD=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
#MAIL_ENCRYPTION=tls
#MAIL_FROM_NAME="Emet Taz"
#MAIL_FROM_ADDRESS="emett@foomotionmail.com"
#MAIL_DRIVER=smtp
#MAIL_HOST=smtp.gmail.com
#MAIL_PORT=587
#MAIL_USERNAME=muzammilismail8@gmail.com
#MAIL_PASSWORD="gjekawavwcxrputw"
#MAIL_ENCRYPTION=tls
#MAIL_FROM_ADDRESS=muzammilismail8@gmail.com
#MAIL_PORT=2525
#MAIL_USERNAME=null
#MAIL_PASSWORD=null
#MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

我遇到了同样的错误,通过更改 .env 文件中的 DB_HOST 字段解决了问题。 您可以使用以下命令访问容器和 IP 地址。您可以通过在 DB_HOST 字段中输入容器 IP 运行 for mysql 来解决问题。

docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

输出

/mysql - 172.18.0.4
/nginx - 172.18.0.2
/php - 172.18.0.3

docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps-aq)