4

I have a MySQL server on my host machine and I want my docker containers connect to it, instead of create a MySQL container.

In my application configuration file I'm using localhost, as I used to do before using Docker, but the connection is being refused.

I'm using docker-compose and here is my .yml:

version: "3.2"

services:
    php:
        build: 
            context: './dockerfiles/php/'
            args:
                PHP_VERSION: ${PHP_VERSION}
        networks:
            - backend
        volumes:
            - ${PROJECT_ROOT}/:/var/www/html/
        container_name: acadbase_php

    apache:
        build:
            context: './dockerfiles/apache/'
            args:
                APACHE_VERSION: ${APACHE_VERSION}
        depends_on:
            - php
        networks:
            - frontend
            - backend
        ports:
            - "8080:80"
        volumes:
            - ${PROJECT_ROOT}/:/var/www/html/
        container_name: acadbase_apache

networks:
    frontend:
    backend:

volumes:
    data:

My ./dockerfiles/php/Dockerfile:

ARG PHP_VERSION=""
FROM php:${PHP_VERSION:+${PHP_VERSION}-}fpm-alpine

RUN apk update; \
    apk upgrade; \
    apk add libxml2-dev

RUN docker-php-ext-install mysqli soap mbstring xml pdo_mysql

My ./dockerfiles/apache/Dockerfile:

ARG APACHE_VERSION=""
FROM httpd:${APACHE_VERSION:+${APACHE_VERSION}-}alpine

RUN apk update; \
    apk upgrade; \
    apk add vim;

COPY acadbase.conf /usr/local/apache2/conf/acadbase.conf
RUN echo "Include /usr/local/apache2/conf/acadbase.conf" \
    >> /usr/local/apache2/conf/httpd.conf

My .env:

PHP_VERSION=7.1
APACHE_VERSION=2.4
PROJECT_ROOT=.
  • you are not doing any port mapping in your docker-compose.yaml from hostport:containerport this gives processes inside the container access to ports visible on the host – Scott Stensland Aug 14 '19 at 20:44
  • @ScottStensland but if I do it, what IP should I use together with the created port in my application to do this conection? – Thiago Bittencourt Aug 15 '19 at 12:59
  • none ... look at your current apache port setting for "8080:80" which just maps host port 8080 into container port 80 ... do similar for your sql port – Scott Stensland Aug 15 '19 at 14:27
  • @ScottStensland I have this string in my application config file: 'mysql:host=localhost;dbname=admin'. If I only create a new port connection "3306:3307" I'll can not use "localhost" as host, because it points to the container, not to the host machine. And I couldn't even create a port like that, because when I do it I'm getting ```Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use``` – Thiago Bittencourt Aug 15 '19 at 14:37

4 Answers4

2

You can connect to the host from the container with the domain - host.docker.internal, which holds the host's internal ip.

eitann
  • 1,203
  • 10
  • 23
2

As @StrahinjaTasic and @AvinashReddy said, I need to use the IP of my host machine, but it was not working only because I needed to do more things in my MySQL local server.

1- I needed to allow the database user connects from the container IP (or from everywhere) instead of only from localhost.

# use the correct IP if it is not for local development
GRANT ALL PRIVILEGES ON *.* TO '[user]'@'%';

FLUSH PRIVILEGES;

For MySQL GRANT command details see: https://dev.mysql.com/doc/refman/5.7/en/grant.html

2- I needed to change MySQL configuration file to let it listen not only localhost, but also the container IP (or everywhere).

In my case, the configuration file is /etc/mysql/mysql.conf.d/mysqld.conf.

# use the correct IP if it is not for local development
bind-address = 0.0.0.0

After that I put the IP of my host machine and my database user credentials in my application configuration file and it worked!

P.S.: To get the container IP see How to get a Docker container's IP address from the host?

0

You were using loopback address of the host machine. Instead use the inet(ip address) of the network interface. "eth0" should be your interface name.

Avinash Reddy
  • 1,153
  • 7
  • 23
0

localhost cant be resolved from container. You should use IP address of local machine (example: 192.168.1.10) which should be accessable from container. Also why you don't just add MySQL container to your .yml file and add volume link to your local directory so data persists. I can leave you example of this if you want :)

Good luck.

  • I'm not able to use IP address of local machine, it's not working. And I know how to work with MySQL in a container, but here we have a production database server and so I want to use my local MySQL server in my local docker environment to help me to use in production only changing the IP. – Thiago Bittencourt Aug 15 '19 at 13:45