M
M
MrFrumpuSs2022-02-09 21:00:05
MySQL
MrFrumpuSs, 2022-02-09 21:00:05

How to make php-fpm in docker-compose connect to local mysql?

Here is my docker-compose.yml file

version: '3'
    services:
    fpm:
        build: /images/php
        volumes:
            - /var/www:/var/www
            - /var/www/php-ini:/usr/local/etc/php/custom.d
        environment:
            PHP_INI_SCAN_DIR: "/usr/local/etc/php/custom.d:/usr/local/etc/php/conf.d"
        ports:
            - "127.0.0.1:9074:9000"

Here is the Dockerfile with php:
FROM php:7.4-fpm
    MAINTAINER PHPtoday.ru <[email protected]>

    RUN apt-get update && apt-get install -y \
        curl \
        wget \
        git \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
      libpng-dev \
      libonig-dev \
      libzip-dev \
      libmcrypt-dev \
        && pecl install mcrypt-1.0.3 \
      && docker-php-ext-enable mcrypt \
        && docker-php-ext-install -j$(nproc) iconv mbstring mysqli pdo_mysql zip \
      && docker-php-ext-configure gd --with-freetype --with-jpeg \
        && docker-php-ext-install -j$(nproc) gd 

    RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin -- 
    filename=composer
    ADD php.ini /usr/local/etc/php/conf.d/40-custom.ini
    WORKDIR /var/www
    CMD ["php-fpm"]

I have mysql and nginx installed locally, when nginx runs with docker version of php it doesn't connect to databases, database connection error, how can I fix this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Karabanov, 2022-02-09
@MrFrumpuSs

In the MySQL config, as an IP, enter the address of the container's gateway, which one you can look at, docker inspect <container_name>or look at the output ip a- this address will also be visible on the container's bridge.
Then, in the connection settings, specify this IP as the MySQL host.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question