2

I have the following docker-compose.yml

web:
  image: nginx
  ports:
    - "80:80"
  volumes:
    - ./src:/var/www
    - ./src/vhost.conf:/etc/nginx/sites-enabled/vhost.conf
  links:
    - php
php:
  image: nmcteam/php56
  volumes:
    - ./src/php-fpm.conf:/etc/php5/fpm/php-fpm.conf
    - ./src:/var/www
  links:
    - db
db:
  image: sameersbn/mysql
  ports:
    - "3306:3306"
  volumes:
   - /var/lib/mysql
  environment:
   - DB_NAME=demoDb
   - DB_USER=demoUser
   - DB_PASS=demoPass

Everything works fine but what I would like is to link php to mysql installed on host machine. Is there a way I could achieve this?

Thank you.

Magarusu
  • 982
  • 10
  • 14
  • 1
    You could link the mysql volume like it says in the documentation: https://github.com/sameersbn/docker-gitlab#internal-mysql-server . Or you can find the ip and access it as an external mysqlserver. Default networking mode is bridge, so the ip should be accessable. see this link to see how to find the docker host ip: http://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach – Coy Dec 30 '15 at 22:10
  • Thank you. You helped me in finding my answer. – Magarusu Dec 31 '15 at 16:21
  • No problem.. i'll add it as an answer until you put in a complete answer yourself.. @magarusu – Coy Dec 31 '15 at 16:30

2 Answers2

1

You could link the mysql volume like it says in the documentation: http://github.com/sameersbn/docker-gitlab#internal-mysql-server . Or one can bind the sockets(see comment below)

Or you can find the ip and access it as an external mysqlserver. Default networking mode is bridge, so the ip should be accessable. See the "From inside of a Docker container, how do I connect to the localhost of the machine?" on how to find the IP

Community
  • 1
  • 1
Coy
  • 3,703
  • 3
  • 14
  • 13
  • 2
    Alternatively, bind-mount the MySQL socket inside the container, and use the socket to connect to MySQL, e.g. `- /var/run/mysql.sock:/tmp/mysql.sock` inside the `volumes` section, to make the socket available as `/tmp/mysql.sock` inside the container. – thaJeztah Jan 01 '16 at 03:07
0

This might be the best all around answer as it's less possible to create any new security issues:

Mount mysqld.sock from the host to inside the container.

Find the location of the mysql.sock file on the host running mysql:

netstat -ln | awk '/mysql(.*)?\.sock/ { print $9 }'

Mount that file to where it's expected in the docker:

docker run -v /hostpath/to/mysqld.sock:/containerpath/to/mysqld.sock

Possible locations of mysqld.sock:

/tmp/mysqld.sock
/var/run/mysqld/mysqld.sock 
/var/lib/mysql/mysql.sock

as found here: https://stackoverflow.com/a/30484147/2245680

I took a different approach as I only need this for development and I changed the networking mode so that every container could communicate with the host. To achieve this you can do something like this in your docker-compose.yaml:

web:
  image: nginx
  volumes:
    - ./src:/var/www
  net: "host"
php:
  image: nmcteam/php56
  volumes:
    - ./src/php-fpm.conf:/etc/php5/fpm/php-fpm.conf
    - ./src:/var/www
  net: "host"

https://docs.docker.com/compose/compose-file/#net

Community
  • 1
  • 1
Magarusu
  • 982
  • 10
  • 14