简体   繁体   中英

Docker (Ubuntu 16.04) Apache/PHP7/Mysql5_7 - Database: Connection failed: Connection refused

Im trying to setup docker-container: Apache/PHP7/Mysql 5.7 via docker-compose up on Ubuntu 16.04.

The Problem is that i get no database-connection from the index.php (localhost:8080). If i try to connect it says: "Connection failed: Connection refused".

apache-php7 dockerfile: https://github.com/nimmis/docker-apache-php7/blob/master/Dockerfile

The Folders are:

apache-php7 (including Dockerfile)

html (including index.php)

mysql (mysql data)

docker-compose.yml:

version: '2'

services:
  php:
    build: apache_php7
    container_name: apache_php7
    ports:
      - "8080:80"
      - "443:443"
    volumes:
      - ./html:/var/www/html
    links:
      - db
  db:
    image: mysql:5.7
    container_name: mysql_5_7
    volumes:
     - ./mysql:/var/lib/mysql
    ports:
     - "127.0.0.1:3306:3306"
    environment:
     - MYSQL_ROOT_PASSWORD=test
     - MYSQL_USER=admin
     - MYSQL_PASSWORD=test     
     - MYSQL_DATABASE=database

index.php (php-snippet)

<?php
$servername = "127.0.0.1";
$username = "admin";
$password = "test";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?> 

i searched for similar Threads and tried this also with PHP-PDO etc. - nothing did work.

I'm able to log into the mysql-container manually via Terminal with the command: docker exec -it bash , and login via "mysql -u root -p " -> passwort: test.

its solved. First i get the name/id of the running mysql Container via

docker ps

and then i got a ip-address from the command

docker inspect <name of container> | grep IPAddress

like in this screenshot.

command from nigel

I also modified this line in my docker-compose.yml:

    volumes:
    - /var/lib/mysql:/var/lib/mysql

Thanks to Nigel Ren and thanks to jcorry for answering. Now everything is working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM