Docker:连接到数据库容器时连接超时

xiozqbni  于 2023-05-28  发布在  Docker
关注(0)|答案(1)|浏览(374)

我有一个问题,我的php容器没有连接到我的数据库容器。

我的docker-composite.yml:

version: "2"

    volumes:
      # this is the mysql data volume we are going to host the data inside
      dev_mysql_data:
      # This volume is used for elasticsearch
      dev_elastic_search:

    networks:
      mp_pixel:
        driver: bridge
        ipam:
          driver: default
          config:
            - subnet: 172.20.0.0/16

    services:
      # database container for local development purposes
      dev_database:
        image: mysql:5.6
        networks:
          mp_pixel:
            aliases:
              - database
        ports:
          # port 3304 (external) is for use on your desktop sql client
          # port 3306 (internal) is for use inside your application code
          - 3304:3306
        volumes:
          # mount the mysql_data docker volume to host the local development database
          - dev_mysql_data:/var/lib/mysql
          # the provision file helps when trying to use the provision script to clone databases
          - ./provision.cnf:/provision.cnf
        environment:
          MYSQL_ROOT_PASSWORD: pixel

      # This is the local development version of the nginx container
      dev_nginx:
        image: mp-pixel-nginx:latest
        build: ./nginx
        ports:
          - '80:80'
          - '443:443'
        networks:
          mp_pixel:
            aliases:
              - nginx
        depends_on:
          - dev_phpfpm
        volumes_from:
          - dev_phpfpm
        environment:
          - VIRTUAL_HOST=~^(mp-pixel|mp-location|mp-feedback|mp-user|mp-phone|mp-loancalculator|mp-seo|mp-media|mp-listing|mp-development|mp-kpi|mp-newsletter|mp-auth|mp-worker|mp-search)-ph-dev.pixel.local

      # This is the local development version of the phpfpm container
      dev_phpfpm:
        image: mp-pixel-phpfpm:latest
        build:
          context: ./
          args:
            # this build might fail, if so, run in a terminal: export SSH_KEY=$(cat ~/.ssh/id_rsa)
            - SSH_KEY=$SSH_KEY
        networks:
          mp_pixel:
            aliases:
              - phpfpm
        depends_on:
          - dev_database
        volumes:
          # we override the images /www directory with the code from the live machine
          - ./:/www
        env_file:
          # inside this file, are the shared database secrets such as username/password
          - ./env/common
          - ./env/dev

      dev_elasticsearch:
        image: docker.elastic.co/elasticsearch/elasticsearch:5.3.3
        networks:
          mp_pixel:
            aliases:
              - elasticsearch
        ulimits:
          memlock:
            soft: -1
            hard: -1
          nofile:
            soft: 65536
            hard: 65536
        mem_limit: 1g
        cap_add:
          - IPC_LOCK
        volumes:
          - dev_elastic_search:/usr/share/elasticsearch/data
        ports:
          - 9200:9200
        environment:
          - cluster.name=dev-elasticsearch-pixel
          - bootstrap.memory_lock=true
          - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
          - "xpack.security.enabled=false"

我用docker-compose up运行它,PHP日志显示
An exception occured in driver: SQLSTATE[HY000] [2002] Connection timed out
我尝试使用docker exec访问数据库容器,并且可以确认我具有正确的凭据。
会有什么问题呢?

fcg9iug3

fcg9iug31#

当你的容器启动时,你是否已经尝试过用Sequel Pro这样的工具连接到数据库?也许数据库没有初始化,因此无法建立来自php容器的连接?您试图访问db容器,但没有访问数据库本身。
另外,您可以向docker-compose.yml的数据库部分添加更多的环境变量

environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
      - MYSQL_DATABASE=databasename
      - MYSQL_USER=databaseuser
      - MYSQL_PASSWORD=databasepassword

希望能帮上忙

相关问题