在Docker中,我运行我的PHP程序(容器1),该程序从数据库(容器2,端口:3311)获取数据,如果我在本地运行它,>>就成功了。但是,当使用Docker时,连接被拒绝了,我已经通过database.php和码头桥建立了连接,名为'Laravel‘,但仍然是这样。我还能做什么?在这里,错误通知:
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `address` limit 1)
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:759
755▕ // If an exception occurs when attempting to run a query, we'll format the error
756▕ // message to include the bindings with SQL, which will make this exception a
757▕ // lot more helpful to the developer instead of just the database's errors.
758▕ catch (Exception $e) {
➜ 759▕ throw new QueryException(
760▕ $query, $this->prepareBindings($bindings), $e
761▕ );
762▕ }
763▕ }
+20 vendor frames
21 app/Console/Commands/showAddress.php:32
Illuminate\Database\Query\Builder::get()
+13 vendor frames
35 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
database.php:
'sakiladb' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => '127.0.0.1',
'port' => '3311',
'database' => 'sakila',
'username' => 'root',
'password' => 'rootpass',
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
码头网络(我使用“laravel”桥接):
PHP Docker文件:
ARG PHP_VERSION=8.0.2-fpm-alpine3.7
FROM php:${PHP_VERSION:+${PHP_VERSION}-}fpm-alpine
RUN apk update; \
apk upgrade;
RUN docker-php-ext-install mysqli pdo pdo_mysql
Apache Docker文件:
FROM httpd:2.4.33-alpine
RUN apk update; \
apk upgrade;
# Copy apache vhost file to proxy php requests to php-fpm container
COPY demo.apache.conf /usr/local/apache2/conf/demo.apache.conf
RUN echo "Include /usr/local/apache2/conf/demo.apache.conf" \
>> /usr/local/apache2/conf/httpd.conf
demo.apache.conf:
ServerName localhost
LoadModule deflate_module /usr/local/apache2/modules/mod_deflate.so
LoadModule proxy_module /usr/local/apache2/modules/mod_proxy.so
LoadModule proxy_fcgi_module
/usr/local/apache2/modules/mod_proxy_fcgi.so
<VirtualHost *:80>
# Proxy .php requests to port 9000 of the php-fpm container
ProxyPassMatch ^/(.*\.php(/.*)?)$
fcgi://php:9000/var/www/html/$1
DocumentRoot /var/www/html/
<Directory /var/www/html/>
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# Send apache logs to stdout and stderr
CustomLog /proc/self/fd/1 common
ErrorLog /proc/self/fd/2
</VirtualHost>
码头工人-公司:
version: "3.2"
networks:
laravel:
external:
name: laravel
services:
php:
build:
context: './php/'
args:
PHP_VERSION: ${PHP_VERSION}
networks:
- laravel
volumes:
- ${PROJECT_ROOT}/:/var/www/html/
container_name: php
apache:
build:
context: './apache/'
args:
APACHE_VERSION: ${APACHE_VERSION}
depends_on:
- php
- mysql
networks:
- laravel
ports:
- "8000:80"
volumes:
- ${PROJECT_ROOT}/:/var/www/html/
container_name: apache
volumes:
data:
发布于 2022-08-05 07:10:37
在这里,您使用了错误的DB配置。使用DB容器中的DB配置。
主机:海洋测试
端口:3306
另一方面,将mysql添加到您的停靠库中--使用容器名mariadbtest编写。同样,在depends_on:
下,您也必须添加mariadbtest
将以下内容添加到php Dockerfile
RUN php artisan view:clear
RUN php artisan route:clear
RUN php artisan config:cache
RUN php artisan cache:clear
https://stackoverflow.com/questions/73245871
复制相似问题