Problem with Docker installation pdo

1

I have a problem with my containers in docker, I was trying to raise a Lamp with docker compose, with the default image of php: apache and mysql, but when I install the PDO this error appears.

SQLSTATE [HY000] [2002] No such file or directory

link

Dockerfile

FROM php:apache

RUN apt-get update && \
    docker-php-ext-install pdo pdo_mysql

COPY ./app /var/www/html/

docker-compose.yml

version: "3"
services:
  php-apache:
    build: .
    ports:
     - "8080:80"
    links:
     - dbmysql
    volumes:
     - ./app:/var/www/html

  dbmysql:
    image: mysql
    volumes:
      - ./mysql:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
    ports:
      - "3306:3306"

I'll be grateful for any answer!

Aurino Junior

    
asked by anonymous 30.04.2018 / 20:07

1 answer

0

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();
    }
    
30.04.2018 / 21:35