Reputation: 317
So I've searched this a good amount and everyone says either make the db host in .env file equal to mysql or the docker container ip but both have not worked for me?
I'm pretty sure it's a host issue but I do not know at this point?
Here's my docker-compose.yml file:
version: "2"
services:
web:
container_name: algm
build:
context: .
dockerfile: container-build/web/Dockerfile
environment:
- MYSQL_DATABASE=dbname
- MYSQL_USER=dbuser
- MYSQL_PASSWORD=654321
- MYSQL_HOST=db
ports:
- "8080:80"
volumes:
- .:/var/www
depends_on:
- db
db:
image: mysql:5.7
ports:
- "6603:3306"
environment:
- MYSQL_ROOT_PASSWORD=654321
- MYSQL_USER=dbuser
- MYSQL_PASSWORD=654321
- MYSQL_DATABASE=dbname
volumes:
- "mysql_data:/var/lib/mysql"
- ./data/schema.sql:/docker-entrypoint-initdb.d/schema.sql
volumes:
mysql_data: { driver: local }
Here's my connections in the .env file:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=dbname
DB_USERNAME=dbuser
DB_PASSWORD=654321
so I get into my docker bash by the command:
sudo docker-compose exec web /bin/bash
and there I can make models and migrations but cannot migrate? I get the error:
Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: select * from information_schema.tables where table_schema = dbname and table_name = migrations and table_type = 'BASE TABLE')
at /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:665
661| // If an exception occurs when attempting to run a query, we'll format the error
662| // message to include the bindings with SQL, which will make this exception a
663| // lot more helpful to the developer instead of just the database's errors.
664| catch (Exception $e) {
> 665| throw new QueryException(
666| $query, $this->prepareBindings($bindings), $e
667| );
668| }
669|
Exception trace:
1 PDOException::("PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known")
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
2 PDO::__construct("mysql:host=mysql;port=3306;dbname=dbname", "dbuser", "654321", [])
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
Please use the argument -v to see more details.
UPDATE, here's my Dockerfile in case it's needed:
#
# Use this dockerfile to run the application.
#
# Start the server using docker-compose:
#
# docker-compose build
# docker-compose up
#
# NOTE: In future examples replace {{volume_name}} with your projects desired volume name
#
# You can install dependencies via the container:
#
# docker-compose run {{volume_name}} composer install
#
# You can manipulate dev mode from the container:
#
# docker-compose run {{volume_name}} composer development-enable
# docker-compose run {{volume_name}} composer development-disable
# docker-compose run {{volume_name}} composer development-status
#
# OR use plain old docker
#
# docker build -f Dockerfile-dev -t {{volume_name}} .
# docker run -it -p "8080:80" -v $PWD:/var/www {{volume_name}}
#
FROM php:7.2-apache
RUN apt-get update \
&& apt-get install -y vim git zlib1g-dev mariadb-client libzip-dev \
&& docker-php-ext-install zip mysqli pdo_mysql \
&& pecl install xdebug \
&& docker-php-ext-enable xdebug \
&& echo 'xdebug.remote_enable=on' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.remote_host=host.docker.internal' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.remote_port=9000' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& a2enmod rewrite \
&& sed -i 's!/var/www/html!/var/www/public!g' /etc/apache2/sites-available/000-default.conf \
&& mv /var/www/html /var/www/public \
&& curl -sS https://getcomposer.org/installer \
| php -- --install-dir=/usr/local/bin --filename=composer \
&& echo "AllowEncodedSlashes On" >> /etc/apache2/apache2.conf
WORKDIR /var/www
Upvotes: 3
Views: 7921
Reputation: 606
I've tried your config and it's working. You just need to change DB_HOST=mysql
to your container name DB_HOST=db
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=dbname
DB_USERNAME=dbuser
DB_PASSWORD=654321
Then go to your app folder inside the container and run php artisan config:clear
, php artisan cache:clear
and then execute migrations.
Upvotes: 10