• User Newbie

    importare database mysql dentro un contenitore (docker)

    
    $ cat docker-compose.yml
    version: '2'
    services:
      app:
        build:
          context: .
        image: test-image
        ports:
          - 8080:80
        volumes:
          - .:/srv/app
        links:
          - mysql
          - redis
        environment:
          DB_HOST: mysql
          DB_DATABASE: laravel_docker
          DB_USERNAME: app
          DB_PASSWORD: password
          REDIS_HOST: redis
          SESSION_DRIVER: redis
          CACHE_DRIVER: redis
      mysql:
        image: mysql:5.7
        ports:
          - 13306:3306
        environment:
          MYSQL_DATABASE: test
          MYSQL_USER: root
          MYSQL_PASSWORD: root1
          MYSQL_ROOT_PASSWORD: root2
      redis:
        image: redis:4.0-alpine
        ports:
          - 16379:6379
    
    $ cat Dockerfile
    FROM php:7.2.2-cli
    
    COPY . /usr/local/bin
    WORKDIR /usr/local/bin
    RUN docker-php-ext-install mbstring pdo pdo_mysql
    
    CMD mysql -u $MYSQL_USER -p $MYSQL_PASSWORD $MYSQL_DATABASE < test.sql
    
    $ cat test.php
    <?php
    
    $pdo = new pdo(
        "mysql:host=localhost;dbname=test",
        'root','root1'
    );
    
    $query = $pdo->query("SELECT * FROM people");
    var_dump($pdo->errorInfo());
    
    foreach($query as $row ){  
        var_dump($row);  
    }  
    
    $ cat test.sh
    #!/usr/bin/env bash
    set -e
    cd $(dirname "$0")
    
    docker-compose up --build -d
    docker run test-image php /usr/local/bin/test.php
    
    $ ./test.sh
    Building app
    Step 1/5 : FROM php:7.2.2-cli
     ---> 21c3582549e6
    Step 2/5 : COPY . /usr/local/bin
     ---> Using cache
     ---> e0a9005e2907
    Step 3/5 : WORKDIR /usr/local/bin
     ---> Using cache
     ---> 3efd6cdfab46
    Step 4/5 : RUN docker-php-ext-install mbstring pdo pdo_mysql
     ---> Using cache
     ---> 282ab8fb5896
    Step 5/5 : CMD mysql -u $MYSQL_USER -p $MYSQL_PASSWORD $MYSQL_DATABASE < test.sql
     ---> Using cache
     ---> 7ba640bd690d
    Successfully built 7ba640bd690d
    dockermysql_redis_1 is up-to-date
    dockermysql_mysql_1 is up-to-date
    Starting dockermysql_app_1
    
    Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in /usr/local/bin/test.php:5
    
    

    Perchè non funziona?