Replace RUN apt-get update && \
with RUN apt-get update && apt-get install -y \
Add soon after && docker-php-ext-install pdo pdo_mysql
Dockerfile
FROM php:apache
RUN apt-get update && apt-get install -y \
&& docker-php-ext-install pdo pdo_mysql
COPY ./app /var/www/html/
In this link ( link ) you can read about which is happening with your refused connection.
If it is not so necessary to use version 8, downgrade to version 5. Then do the following.
Delete your local mysql volume:
$ docker-compose down
$ sudo rm -r mysql/
Use a user and developer password ( dev, dev123 ), forget the root.
However, you can use root, or you need to get the root password that will be generated randomly. Go to the terminal, when MySQL is being lifted and look for the generated password. The log line will look something like this: dbmysql_1 | GENERATED ROOT PASSWORD: odut5doo2ahngie5shohV8Eingah7us4
Do this in your docker-compose.yml , changing the version of Mysql (image: mysql: 5), setting the database, random to root password, .
version: "3"
services:
php-apache:
build: .
ports:
- "8081:80"
links:
- dbmysql
volumes:
- ./app:/var/www/html
dbmysql:
image: mysql:5
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_RANDOM_ROOT_PASSWORD: 1
MYSQL_DATABASE: database
MYSQL_USER: dev
MYSQL_PASSWORD: dev123
ports:
- "3306:3306"
Now rebuild in the environment. If your environment is accurate, run the commands as admin, use: sudo
$ docker-compose up --build
In your config.php , use the dev user, or use root:
<?php
$dsn = "mysql:host=dbmysql;dbname=database";
$dbuser = "dev";
$dbpass = "dev123";
try{
$pdo = new PDO($dsn,$dbuser, $dbpass);
echo "Conectado!";
}catch (PDOException $e){
echo $e->getMessage();
}