First of I did read thoses links
- Connect to Docker MySQL container from localhost?
- Connect to Mysql on localhost from docker container
- From inside of a Docker container, how do I connect to the localhost of the machine?
But as a beginner with docker. It did not help me.
What you need to know:
- Yes, I need localhost. I'm working on an app that interact directly with the database. It create/remove user privileges and allow some user to access with limited privileges from a remote access. When initialized, the app will drop the default remote access to root and forge user and grant them full privilege on localhost.
- I'm using a docker-compose.yml generated by https://phpdocker.io
- Ubuntu 18.10
- Docker version 18.09.3, build 774a1f4
- docker-compose version 1.21.0, build unknown
- I'm using docker only for development purpose. On production I use forge
./docker-compose.yml
###############################################################################
#                          Generated on phpdocker.io                          #
###############################################################################
version: "3.1"
services:
    mailhog:
      image: mailhog/mailhog:latest
      container_name: myapp-mailhog
      ports:
        - "8081:8025"
    redis:
      image: redis:alpine
      container_name: myapp-redis
    mariadb:
      image: mariadb:10.4
      container_name: myapp-mariadb
      working_dir: /application
      volumes:
        - .:/application
      environment:
        - MYSQL_ROOT_PASSWORD=root
        - MYSQL_DATABASE=myapp
        - MYSQL_USER=forge
        - MYSQL_PASSWORD=forge
      ports:
        - "8083:3306"
    webserver:
      image: nginx:alpine
      container_name: myapp-webserver
      working_dir: /application
      volumes:
          - .:/application
          - ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
      ports:
       - "8080:80"
    php-fpm:
      build: phpdocker/php-fpm
      container_name: myapp-php-fpm
      working_dir: /application
      volumes:
        - .:/application
        - ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/7.3/fpm/conf.d/99-overrides.ini
./phpdocker/nginx/nginx.conf
server {
    listen 80 default;
    client_max_body_size 108M;
    access_log /var/log/nginx/application.access.log;
    root /application/public;
    index index.php;
    if (!-e $request_filename) {
        rewrite ^.*$ /index.php last;
    }
    location ~ \.php$ {
        fastcgi_pass php-fpm:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        include fastcgi_params;
    }
}
./phpdocker/php-fpm/Dockerfile (slightly modified to add mysql_client and not installing git in a second RUN command)
FROM phpdockerio/php73-fpm:latest
WORKDIR "/application"
# Fix debconf warnings upon build
ARG DEBIAN_FRONTEND=noninteractive
# Install selected extensions and other stuff
RUN apt-get update \
    && apt-get -y --no-install-recommends install  \
        php7.3-mysql php-redis php7.3-sqlite3 php-xdebug php7.3-bcmath php7.3-bz2 php7.3-gd \
        git \
        mysql-client \
    && apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
./php-ini-overrides.ini
upload_max_filesize = 100M
post_max_size = 108M
I tried to use network_mode: host but it makes the webserver stopping with Exit 1
 
     
    