Docker Community Forums

Share and learn in the Docker community.

How to access from php container to mysql container through localhost or 127.0.0.1?

I am using docker-compose
Everything works perfectly! At the moment we are developing a project on the PHP where all the connections to the database through the localhost.
But I need to connect to the database not through the mysql host: “mysql:3306”, but through “127.0.0.1:3306” or “localhost:3306”
How to configure docker so that from my php application I would access the database as a localhost or 127.0.0.1:?
The whole difference is that in php scripts a localhost is often found, so “mysql:3306” is not suitable!

version: '2'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    restart: always
    volumes:
      - ./app:/www/web
      - ./services/web/nginx/conf:/etc/nginx
      - ./services/web/nginx/logs:/www/web_logs
    networks:
      - code-network
    depends_on:
      - php
  mysql:
    image: 'mariadb:5.5-trusty'
    command: ['--innodb_use_native_aio=0']
    volumes:
      - './db/mysql/data:/var/lib/mysql'
      - './db/mysql/example_db:/var/example_db'
      - './db/mysql/conf.d:/etc/mysql/conf.d'
    command:
      mysqld --innodb-flush-method=littlesync --innodb-use-native-aio=OFF --log_bin=ON --skip-innodb --default-storage-engine=myisam
    ports:
      - '3306:3306'
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=792698
    networks:
      - code-network
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - '8180:80'
    restart: always
    environment:
      - PMA_HOST=mysql
      - PMA_PORT=3306
    depends_on:
      - mysql
    networks:
      - code-network

  php:
    build:
      context: ./services/php/docker/
    restart: always
    volumes:
      - './app:/www/web'
      - './services/php/etc/php7.1.13.ini:/usr/local/etc/php/conf.d/php7.1.13.ini'
    depends_on:
      - mysql
    networks:
      - code-network
  elk:
    image: sebp/elk:es241_l240_k461
    restart: always  
    ports:
      - "5601:5601"
      - "9200:9200"
      - "5044:5044" 
    environment:
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m" 
    networks:
      - code-network
    volumes:
      - ./db/elasticsearch:/var/lib/elasticsearch
networks:
  code-network:
    driver: bridge

I would like to see a simple example with the docker compos where the containers nginx, mysql, php, phpmyadmin work.
And all connections to the database go through the localhost

Can’t you modify the script and make the address configurable? The only options I’m thinking about is setting up a proxy in the container or using host networking.
But I think host networking should only be used when you really need it and not as a work-around such as in this case.
And it might be easier to change a php script than to set up a proxy which probably need a supervisor process since you’ll be running at least two processes.

Can I have an example of such a solution with a host network?
I couldn’t get the containers working through network_mode: host