0

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?

Niraj Pandey
  • 309
  • 1
  • 9
  • 21
  • did you ever fix this? I have the same issue – twigg Jun 26 '18 at 10:51
  • @twigg I ended up using the Docker Container listed in the last paragraph! – Niraj Pandey Jun 29 '18 at 04:00
  • Why not using the apache container? There is everything you need - php and a working connection to your mysql container. Coincidentally, I had the same problem, thats why I am here. I wrote a helper script, so that I could work from host. But it just uses the above described way - login php/apache container and use any command. – hmartini Mar 21 '19 at 13:35

2 Answers2

1

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.

programmerq
  • 6,262
  • 25
  • 40
  • Thank you for your response. I am very new to Docker so please forgive my ignorance but I tried removing the Port from MySQL service and tried running the migration but still had the same error. I also tried removing the port from phinx.yml and still had no luck in getting the migration to work. – Niraj Pandey Jun 20 '17 at 13:20
0

As you said you are running the command from your host machine, this is the problem. First login into your docker container on terminal by using command : docker exec -it {container_id} sh , then go to your project folder and run phinx command. sharing phinx.yml file below for reference - (make sure migration path should be your docker folder path )

paths: migrations: /cars/engine/dealerengine/migrations

environments: default_migration_table: phinxlog default_database: docker

production:
    adapter: mysql
    host: localhost
    name: production_db
    user: root
    pass: ''
    port: 3306
    charset: utf8

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

testing:
    adapter: mysql
    host: localhost
    name: testing_db
    user: root
    pass: ''
    port: 3306
    charset: utf8