Docker build with SQLSTATE[HY000] [2002]



在构建docker容器后,我的docker映像出现错误。

在docker-compose up -d命令上,应用程序已连接到数据库。

但是当我试图运行docker build -t来创建映像时然后运行它,我得到了500内部错误php_network_getaddresses: getaddrinfo failed:

FROM webdevops/php-nginx:7.4-alpine
WORKDIR /app
# Install Laravel framework system requirements (https://laravel.com/docs/8.x/deployment#optimizing-configuration-loading)
RUN apk add oniguruma-dev postgresql-dev libxml2-dev
RUN docker-php-ext-install 
bcmath 
ctype 
fileinfo 
json 
mbstring 
pdo_mysql 
pdo_pgsql 
tokenizer 
xml
# Copy Composer binary from the Composer official Docker image
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
ENV WEB_DOCUMENT_ROOT /app/public
ENV APP_ENV production
# Copy existing application directory contents to the working directory
COPY . .
RUN composer install
RUN composer install --no-interaction --optimize-autoloader --no-dev
# Optimizing Configuration loading
RUN php artisan config:clear
# Optimizing Route loading
RUN php artisan cache:clear
RUN php artisan view:cache
RUN chown -R application:application .
在我的docker-compose.yml 中查看更多细节。
version: "3.8"
services:
#php service
php:
build: 
context: .
dockerfile: Dockerfile
container_name: dtltrcy_phpv1
volumes:
- .:/var/www/html
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- laravel
#nginx service web server
nginx:
image: nginx:stable-alpine
container_name: dtltrcy_nginxv1
ports: 
- "8080:80"
volumes:
- .:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mysql
networks:
- dataliteracyapp
#mysql service
mysql:
image: mysql:8.0.21
container_name: dtltrcy_mysqlv1
restart: unless-stopped
tty: true
ports:
- "3306:3306"
volumes:
- ./mysql:/var/lib/mysql
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
environment:
MYSQL_DATABASE: database
MYSQL_USER: ${DB_USERNAME}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_ROOT_HOST: '%'
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
phpmyadmin:
image: phpmyadmin/phpmyadmin
restart: always
container_name: dtltrcy_phpmyadminv1
depends_on:
- mysql
ports:
- "8081:80"
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
networks:
- laravel
#docker networks
networks:
laravel:

.env

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=root 
DB_PASSWORD=secret

我试图用这些来改变。env文件。

DB_HOST=127.0.0.1 ERROR ->SQLSTATE[HY000] [2002] Connection refused

DB_HOST=localhost ERROR ->SQLSTATE[HY000] [2002] No such file or directory

DB_HOST=172.19.0.3 ERROR ->SQLSTATE[HY000] [2002] Operation time out

容器IP使用docker inspect -f '{{range.NetworkSettings.Networks}}{{。IPAddress}}{{结束}}’container_id

我遇到了类似的问题,这是我的Dockerfile,它工作得很好。

FROM php:8-apache
WORKDIR /var/www/html
# install the necessary packages
RUN apt-get update -y && apt-get install -y 
curl 
nano 
npm 
g++ 
git 
zip 
vim 
sudo 
unzip 
nodejs 
libpq-dev 
libicu-dev 
libbz2-dev 
libzip-dev 
libpng-dev 
libjpeg-dev 
libmcrypt-dev 
libreadline-dev 
libfreetype6-dev 
&& docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql 
&& docker-php-ext-install pdo pdo_pgsql pgsql 
&& docker-php-ext-install mysqli pdo_mysql 
&& docker-php-ext-enable mysqli
RUN docker-php-ext-install 
zip 
bz2 
intl 
iconv 
bcmath 
opcache 
calendar
# copy the config file over
#COPY /server/apache/ports.conf /etc/apache2/ports.conf
COPY /server/apache/vhost.conf /etc/apache2/sites-available/laravel.conf
#COPY 000-default.conf /etc/apache2/sites-enabled/000-default.conf
#COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
# install composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# use custom configuration and disable built-in one
#RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
RUN a2enmod rewrite
RUN a2ensite laravel.conf
RUN a2dissite 000-default.conf
# copy over the project files
COPY . /var/www/html
# change ownership of the files
RUN chown -R www-data:www-data /var/www
RUN cd /var/www/html && npm instal && composer install && php artisan optimize
CMD ["/var/www/html/scripts/start-apache.sh"]

/脚本/start-apache.sh

#!/usr/bin/env bash
sed -i "s/Listen 80/Listen ${PORT:-80}/g" /etc/apache2/ports.conf
sed -i "s/:80/:${PORT:-80}/g" /etc/apache2/sites-enabled/*
apache2-foreground "$@"

/server/apache/vhost.conf(与您的问题无关,但它是用于更改文档根目录)

<VirtualHost *:80>
DocumentRoot /var/www/html/public
<Directory "/var/www/html/public">
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

docker-compose.yml

version: "2"
services:
laravel-app:
build:
context: '.'
dockerfile: Dockerfile
environment:
- MYSQL_DATABASE=routes-db
- MYSQL_ALLOW_EMPTY_PASSWORD=1
- MYSQL_PASSWORD=""
container_name: laravel-app
volumes:
- ./:/var/www/html
ports:
- "8000:80"
networks:
- laravel-app-network
depends_on:
- db
db:
image: mysql:5.7
ports:
- "3306:3306"
environment:
- MYSQL_DATABASE=routes-db
- MYSQL_ALLOW_EMPTY_PASSWORD=1
- MYSQL_PASSWORD=""
volumes:
- "./db:/var/lib/mysql"
networks:
- laravel-app-network
networks:
laravel-app-network:
driver: bridge
volumes:
mysql_data:
driver: local

如果我没记错的话,这要么与包/库有关,要么与权限有关。检查行:

RUN chown -R www-data:www-data /var/www

它确保你得到了正确的权限。

对不起,我不能提供更多的细节。

相关内容

  • 没有找到相关文章

最新更新