Allow Docker to connect to external Mysql

¡Hi! I’m new in docker, I want to set my environment to learn this awesome technology on my own. Im using Docker 18.09.0 on ubuntu 16.04.

My objectives are:

I want to allow my docker environment to connect to a localhost Mysql database.
Set 2 docker containers: Nginx and PHP containers especifically.

I’m noob and I don’t know how to do that. Yes, I did read a lot of official documentation but I have no idea why it isn’t working.

First of all, this is how my docker.compose.yml file looks like:
image

Let me explain what I was trying, 3 containers (mysql one is provisional, just for test), the first one is for the Nginx and as you can see, I wrote the extra_hosts line to try some stackoverflow stuff… And the php contains the varnish configuration relocated in a Dockerfile in the /php/ folder.

I’d also try this line in the mysql container:

DATABASE_URL=mysql://user:pass@localhost:3306/database

Its interesting because when I write the real information in the mysql container, especifically the environment variables, the docker-compose up instruction doesn’t seems to print any error:

_php_1    | [28-Nov-2018 18:02:13] NOTICE: fpm is running, pid 1
php_1    | [28-Nov-2018 18:02:13] NOTICE: ready to handle connections
mysql_1  | 2018-11-28T18:02:15.004897Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
mysql_1  | 2018-11-28T18:02:15.005015Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.13) starting as process 1
mysql_1  | 2018-11-28T18:02:18.344668Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql_1  | 2018-11-28T18:02:18.408738Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql_1  | 2018-11-28T18:02:18.468762Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.infoschema@localhost' ignored in --skip-name-resolve mode.
mysql_1  | 2018-11-28T18:02:18.468800Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
mysql_1  | 2018-11-28T18:02:18.468811Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
mysql_1  | 2018-11-28T18:02:18.468823Z 0 [Warning] [MY-010315] [Server] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
mysql_1  | 2018-11-28T18:02:18.468887Z 0 [Warning] [MY-010323] [Server] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
mysql_1  | 2018-11-28T18:02:18.468902Z 0 [Warning] [MY-010323] [Server] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
mysql_1  | 2018-11-28T18:02:18.468918Z 0 [Warning] [MY-010311] [Server] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
mysql_1  | 2018-11-28T18:02:18.606966Z 0 [Warning] [MY-010330] [Server] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
mysql_1  | 2018-11-28T18:02:18.606999Z 0 [Warning] [MY-010330] [Server] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
mysql_1  | 2018-11-28T18:02:18.662941Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.13'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
mysql_1  | 2018-11-28T18:02:18.859348Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060_

I wonder if this logs are saying something important but my Docker knowledge is ridiculous. On the other hand I want you to take a look to my Dockerfile and Nginx configuration.

Basic Nginx configuration:

server {
    index index.php;
    server_name php-docker.local;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /public;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
 }

Basic Dockerfile to get the mysqli dependencies:

FROM php:5.6-fpm
RUN apt-get update; \
    apt-get upgrade;
RUN docker-php-ext-install mysqli

I also have a Composer that sets a Wordpress with some themes and plugins. The objective of that is allow Wordpress stored in Docker to connect to the database stored in my localhost.

And the result of all this rigmarole is a not working Wordpress (I’m strongly convinced that this error is because wordpress is running on Docker that can’t find the BBDD stored in my localhost).

Wordpress error: Error establishing a database connection.

Thank you all so much about reading my question.

Im so sorry about my English.

If you want your application running in a Docker container to connect to a MySQL database running on your host then you will need to supply the real IP address of your MySQL host to the PHP Docker container because your PHP container is isolated from your localhost so it cannot see it as 127.0.0.1.

You could do this by adding the environment variable DATABASE_URL to the php app. Let’s say the IP address of your MySQL server is 192.168.33.10, then your php definition in your docker-compose.yml file would be:

php:
    build: ./php
    volumes:
        - ./public:/public
    environment:
        - DATABASE_URL='mysql://user:pass@192.168.33.10:3306/database'

This assumes that the the userid to your MySQL database is user and the password is pass but you get the idea. The php Docker container is isolated from your localhost so it cannot see anything running on it. This is why you must supply the “real” IP address to it.

~jr

2 Likes