John the Painter
John the Painter

Reputation: 2615

Connecting to 'localhost' or '127.0.0.1' with a Docker/docker-compose setup

Seems to be a common question but with different contexts but I'm having a problem connecting to my localhost DB when using Docker.

If I inspect the mysql container using docker inspect and find the IP address and use this as the DB host as part of the CMS, it runs fine... the only issue is the mysql container IP address changes (upon eachdocker-compose up and if I change wifi networks) so ideally I'd like to use 'localhost' or '127.0.0.1' but for some reason this results in a SQLSTATE[HY000] [2002] Connection refused error.

How can I use 'localhost' or '127.0.0.1' as the DB hostname in CMS applications so I don't have to keep changing it as the container IP address changes?

This is my docker-compose.yml file:

version: "3"

services:
  webserver:
    build:
      context: ./bin/webserver
    restart: 'always'
    ports:
      - "80:80"
      - "443:443"
    links:
      - mysql
    volumes:
      - ${DOCUMENT_ROOT-./www}:/var/www/html
      - ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
      - ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
      - ${LOG_DIR-./logs/apache2}:/var/log/apache2
    networks:
      mynet:
          aliases:
            - john.dev
  mysql:
    image: 'mysql:5.7'
    restart: 'always'
    ports:
      - "3306:3306"
    volumes:
      - ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
      - ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
    environment:
      MYSQL_ROOT_PASSWORD: example
    networks:
      - mynet
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
    ports:
      - '8080:80'
    volumes:
      - /sessions
    networks:
      - mynet
networks:
  mynet:

Upvotes: 1

Views: 4588

Answers (1)

Alejandro Nortes
Alejandro Nortes

Reputation: 694

Try using mysql instead of localhost.

You are defining a link between webserver container and mysql container, so webserver container is able to resolve mysql IP.

According to Docker documentation:

Docker Cloud gives your containers two ways find other services:

  • Using service and container names directly as hostnames

  • Using service links, which are based on Docker Compose links

Service and Container Hostnames update automatically when a service scales up or down or redeploys. As a user, you can configure service names, and Docker Cloud uses these names to find the IP of the services and containers for you. You can use hostnames in your code to provide abstraction that allows you to easily swap service containers or components.

Service links create environment variables which allow containers to communicate with each other within a stack, or with other services outside of a stack. You can specify service links explicitly when you create a new service or edit an existing one, or specify them in the stackfile for a service stack.

From Docker compose documentation:

Containers for the linked service are reachable at a hostname identical to the alias, or the service name if no alias was specified.

Upvotes: 1

Related Questions