title: How to access host MySQL server in a Docker container?
body: <p>I have a MySQL server on my host machine and I want my docker containers connect to it, instead of create a MySQL container.</p>
  
  <p>In my application configuration file I'm using <code>localhost</code>, as I used to do before using Docker, but the connection is being refused.</p>
  
  <p>I'm using docker-compose and here is my <code>.yml</code>:</p>
  
  <pre><code>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:
  </code></pre>
  
  <p>My <code>./dockerfiles/php/Dockerfile</code>:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>My  <code>./dockerfiles/apache/Dockerfile</code>:</p>
  
  <pre><code>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" \
      &gt;&gt; /usr/local/apache2/conf/httpd.conf
  </code></pre>
  
  <p>My <code>.env</code>:</p>
  
  <pre><code>PHP_VERSION=7.1
  APACHE_VERSION=2.4
  PROJECT_ROOT=.
  </code></pre>