php-从Docker容器使用localhost连接到MariaDB

首先,我确实阅读了那些链接

> 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?

但是作为docker的初学者.它没有帮助我.

您需要了解的内容

>是的,我需要本地主机.我正在开发一个可以交互的应用程序
直接与数据库.它创建/删除用户权限,并
允许某些用户从远程以有限的权限访问
访问.初始化后,该应用程序将放弃对root和伪造用户认远程访问,并授予他们对localhost的完全特权.
>我正在使用https://phpdocker.io生成的docker-compose.yml
> Ubuntu 18.10
> Docker版本18.09.3,内部版本774a1f4
> docker-compose版本1.21.0,版本未知
>我仅将docker用于开发目的.在生产中,我使用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(稍作修改添加MysqL_client而不在第二个RUN命令中安装git)

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

我尝试使用network_mode:host,但它使Web服务器停止在1号出口处

解决方法:

好的,但是请记住,MysqL / mariadb中的localhost意味着可以通过本地unix套接字进行访问.有在容器之间共享这些的方法.

在这里看看Connection between docker containers via UNIX sockets

相关文章

Docker是什么Docker是 Docker.Inc 公司开源的一个基于 LXC技...
本文为原创,原始地址为:http://www.cnblogs.com/fengzheng...
镜像操作列出镜像:$ sudo docker imagesREPOSITORY TAG IMA...
本文原创,原文地址为:http://www.cnblogs.com/fengzheng/p...
在 Docker 中,如果你修改了一个容器的内容并希望将这些更改...
在Docker中,--privileged 参数给予容器内的进程几乎相同的权...