7

Here is my docker-compose

version: '2'

services:
  nginx:
    image: nginx:1.11.8-alpine
    ports:
      - "8081:80"
    volumes:
      - ./code:/usr/share/nginx/html
      - ./html:/myapp
      - ./site.conf:/etc/nginx/conf.d/site.conf
      - ./default.conf:/etc/nginx/conf.d/default.conf
      - ./error.log:/var/log/nginx/error.log
      - ./nginx.conf:/etc/nginx/nginx.conf
    links:
      - phpfpm
  phpfpm:
    image: php7-fpm:latest
    ports:
      - "9000:9000"
    volumes:
      -  ./code:/usr/share/nginx/html
    links:
      - db_mysql
  db_mysql:
    image: mysql:5.7.17
    volumes:
      - db_data:/var/lib/mysql
    # restart: no
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: wp2017
      MYSQL_USER: wp
      MYSQL_PASSWORD: wp2017
volumes:
  db_data:

Here is how I build my own php7-fpm:lastest

FROM php:7.1-fpm-alpine
RUN docker-php-ext-install mysqli

I cannot connect to mysql container

$serverName = 'localhost';
$userName = 'wp';
$password = 'wp2017';
$dbName = 'wp2017';

$link = mysqli_connect($serverName, $userName, $password, $dbName);

if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}

I always get error: " Warning: mysqli_connect(): (HY000/2002): No such file or directory in /usr/share/nginx/html/db.php on line 8 Connect failed: No such file or directory"

Where I run command sudo netstat -tulpn | grep :3306

I get this tcp6 0 0 :::3306 :::* LISTEN 15362/docker-proxy

Please help !

10
  • stackoverflow.com/questions/4219970/… Commented Jan 16, 2017 at 8:19
  • Would try to use your local IP for connection at first. Commented Jan 16, 2017 at 8:19
  • I also cannot connect. I installed a mysql server in local ( Ubuntu 14.04 ) but I already stop it. Seems it is reason ? Commented Jan 16, 2017 at 8:30
  • are you sure mysqli is installed properly? have a look at the phpinfo if it is running Commented Jan 16, 2017 at 8:32
  • 1
    @Pingbeat it is working for me now. Here is what I fixed $serverName = 'db_mysql:3306'; // not localhost, 127.0.0.1... Commented Jan 16, 2017 at 9:50

1 Answer 1

23

Change PHP script

$serverName = 'db_mysql';

It will work.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this helped, but why is that? Because the name in the docker file is mysql? So if I wanted to connect to localhost I should've name it localhost?
You can give it a name like container_name: yourname and then use it instead of a localhost and it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.