douyong6585 2017-06-18 11:37
浏览 135

在Docker容器上运行PHINX迁移时出现PDO连接错误

I am trying to run a migration using Phinx from my host machine (OSX Sierra) but I keep getting PDO connection error. It's a simple LAMP stack and is working fine otherwise.

Here is my docker-compose:

version: '2'
services:
  apache:
    build:
      context: ./docker/apache-php7
      dockerfile: Dockerfile
    volumes:
      - ./app:/var/www
    ports:
      - "80:80"
      - "443:443"
    networks:
     - localnet
    links:
    - mysql
  mysql:
    image: mysql:5.7
    ports:
     - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: "root"
      MYSQL_DATABASE: "root"
      MYSQL_USER: "root"
      MYSQL_PASSWORD: "root"
    volumes:
     - ./db/mysql:/var/lib/mysql
    networks:
     - localnet
networks:
  localnet:
    driver: "bridge"
volumes:
  mysqldata:
    driver: "local"
  redisdata:
    driver: "local"

My folder strucutre is as follows:

/app
/db/mysql
/docker
docker-compose.yml

My phinx.yml:

paths:
    migrations: %%PHINX_CONFIG_DIR%%
    seeds: %%PHINX_CONFIG_DIR%%

environments:
    default_migration_table: phinxlog
    default_database: docker
    production:
        adapter: mysql
        host: localhost
        name: %%PHINX_DBNAME%%
        user: %%PHINX_DBUSER%%
        pass: %%PHINX_DBPASS%%
        port: 3306
        charset: utf8

    development:
        adapter: mysql
        host: localhost
        name: %%PHINX_DBNAME%%
        user: %%PHINX_DBUSER%%
        pass: %%PHINX_DBPASS%%
        port: 3306
        charset: utf8

    docker:
        adapter: mysql
        host: mysql
        name: foo_db
        user: root
        pass: root
        port: 3306
        charset: utf8

Here is the output I get when I run the command (php ~/projects/project/app/vendor/bin/phinx migrate) from my host machine.

using config file ./phinx.yml
using config parser yaml
using migration paths
 - /Users/foo/projects/project/app/migrations
using seed paths
 - /Users/foo/projects/project/app/migrations
warning no environment specified, defaulting to: docker
using adapter mysql
using database foo_db

[InvalidArgumentException]
  There was a problem connecting to the database: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known

I have tried rebuilding the image, tried other images but just doesn't seem to work. I can connect to the Docker MySQL from my host machine using SequelPro with the following configuration:

 Host: 0.0.0.0
 Username: root
 Password: root
 Databse: 
 Port: 3306

What do I need to do/fix in order run these Phinx migrations on the MySQL container from my host machine as the alternative would be to use another Docker Container which I am trying to avoid?

  • 写回答

1条回答 默认 最新

  • dougaoxian8922 2017-06-19 17:24
    关注

    You should use the network service discovery feature in docker. You have a service called 'mysql', so all containers on the same network is that container will be able to connect to it by name: 'mysql'

    0.0.0.0 is not an actual ip that you can connect to. You see that in the output of docker ps because that is an alias that means "all interfaces". When you publish a report, docker will set up a listen on the host, and 0.0.0.0 means any host interface.

    Each container gets its own localhost, so if you are specifying localhost, but the database is running in a different container, you will get a connection refused.

    In the case where you have a database like this in one container, and you want to connect to it from another container, you do not need to use any port publishing at all. Port publishing is for services that you want to access from a non-container or outside the docker host. That means you can remove the report 3306 from your compose file for your mysql service, and your other container can still connect to it by connecting to the 'mysql' hostname.

    The name discovery service works because inside each docker container, docker runs a virtual dns server at 127.0.0.11. Any container name or service name will be resolvable at that dns server.

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建