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