4

I'm having problems connecting to my linked mysql container from my php 7.0.1-apache container.

PHP container Dockerfile:

FROM php:7.0.1-apache

# Initialize html and php.ini
COPY src/ /var/www/html/
COPY config/php.ini /usr/local/etc/php/

# Update modules.
RUN apt-get update

docker-compose.yml:

web:
  build: .
  ports:
   - "80:80"
  links:
   - "db"
  volumes:
   - "./src/:/var/www/html/"

db:
  image: "mysql"
  ports:
   - "3306:3306"
  environment:
   - "MYSQL_ROOT_PASSWORD=somepword"

index.php:

<?php
$servername = "127.0.0.1";
$username = "root";
$password = "somepword";
$db = "test_db";

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

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

Error log:

PHP Fatal error:  Uncaught Error: Class 'mysqli' not found in /var/www/html/index.php:8\nStack trace:\n#0 {main}\n  thrown in /var/www/html/index.php on line 8

I'm not sure where I'm going wrong, basically it looks like mysql isn't configured correctly in my PHP container. Any suggestions?

1 Answer 1

4

Yes, as you said your setup does not include the Mysqli extension, that you need to manually install.

Add the installation of the MySQLi to your Dockerfile like so and you should be good:

FROM php:7.0.1-apache

RUN apt-get update && apt-get install -y mysql-client libmysqlclient-dev \ 
      && docker-php-ext-install mysqli
    # Initialize html and php.ini
COPY src/ /var/www/html/
COPY config/php.ini /usr/local/etc/php/

Also lose the apt-get update in the end. If anything you might want to run apt-get update && apt-get upgrade to actually update the modules. Honestly though, you shouldn't be doing this. This will cause your Dockerfile to not produce the same build in a few weeks, simply because the dependencies have changed which kind of defies the part of the point of Docker doesn't it ? :)

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

4 Comments

Thanks -- however if I don't include the apt-get update first, then it doesn't find the packages: E: Unable to locate package mysql-client E: Unable to locate package libmysqlclient-dev
Yea right, sorry that part was a little confusingly phrased. All I was trying to say was that you probably shouldn't run a bare apt-get update in the end of your Dockerfile. But of cause, if you want to install stuff you most likely will need to apt-get update beforehand with most base images.
It's now finding the connection, but I'm getting this "Connection failed: Connection refused" -- is there anything in my index.php that looks incorrect?
Jup the database host is not 127.0.0.1, you're not using a shared network interface between those two containers. Try setting db instead of 127.0.0.1 in the php

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.