0

I want to use localhost when used in php to point to the ip of the mysql container instead of pointing to itself so I can use the mysql container for data.

How can use localhost in my php call to mysql

$mysqli = new mysqli("localhost", "root", "examplepass", "set", 3306);

the above code won't work unless I use db as the hostname.

db:
  image: mariadb
  container_name: mariadb
  environment:
    MYSQL_ROOT_PASSWORD: examplepass

www:
  build: ./images/www
  links:
    - db:mysql
  ports:
    - 8080:80

Do I have to change the hosts file in the container somehow through the Dockerfile or compose yaml?

isethi
  • 661
  • 1
  • 10
  • 22
  • Possible duplicate of [Let two Containers getting linked to eachother](https://stackoverflow.com/questions/27563460/let-two-containers-getting-linked-to-eachother) – rickdenhaan Jun 11 '17 at 15:01
  • @rickdenhaan Not really because all I want it to refer to another container by using localhost and not the container name so I can use localhost in my php scripts that need to access mysql on the other container. – isethi Jun 11 '17 at 15:06
  • Yes, to accomplish that you need to start both containers separately instead of linking them, as suggested in the accepted answer for that question. – rickdenhaan Jun 11 '17 at 15:08
  • @rickdenhaan but then the mysql container is exposed on the internet. I want only the php container to be able to access it. (it said to expose the ports to the host machine) – isethi Jun 11 '17 at 15:11

2 Answers2

0

This will work as you expect, but the www will be where it listens on (80, not 8080):

version: "2"

services:
  db:
    image: mariadb
    container_name: mariadb
    environment:
      MYSQL_ROOT_PASSWORD: examplepass
    network_mode: "host"

  www:
    build: ./images/www
    links:
      - db
    network_mode: "host"

Using network_mode: "host", let's say, containers get the same localhost as your host.

Configure mysql bind-address to localhost to avoid exposing mysql outside your host.

Robert
  • 33,429
  • 8
  • 90
  • 94
  • I want to point to mysql as localhost instead of db like you and I have. And I also don't want to have the mysql exposed on the internet. It need to be only accessible through the php (www) container. – isethi Jun 11 '17 at 15:41
  • So there's no way to change the hosts file to point localhost to the mysql container's ip? @Robert – isethi Jun 11 '17 at 20:50
  • Have you tried `--add-host=localhost:IP`? Don't tell anyone that I've told you this. Changing the meaning of localhost could destroy the universe. https://docs.docker.com/compose/compose-file/#extra_hosts – Robert Jun 11 '17 at 20:54
0

What you are trying to do can be achieved using "socat" Under PHP container you can forward any request coming to port 3306 to your "db" container. So when localhost:3306 is used under PHP container in your case (www) all request will be served from "db". But you need to make sure socat is installed in "www" container and forwarding the port.

Socat Example: socat TCP-LISTEN:3306,fork TCP:db:3306

Check what is socat with example: https://www.cyberciti.biz/faq/linux-unix-tcp-port-forwarding/

I hope what you are trying to achieve can be done using the above with some changes in your containers

Coder John
  • 776
  • 4
  • 7