skip to Main Content

I am trying to connect a MySQL database which is in a docker container to a node app, which has sequelize installed and is in another container.

I have got a bridge network setup between the 2 containers and the nodeapi container depends on the mysql container.

When I exec into the nodeapi container directly I can connect to the mysql container using the 172.20.0.2 IP address, but this obviously doesn’t work from the host machine. But it does confirm that I can log into the container as expected.

The mysql container is also named – so that means I set the host name of the db in the connection.

The ENV variables load properly which I have confirmed by echo-ing them to the screen when building the container.

The Problem is that I get the following message:

Unable to connect to the database: HostNotFoundError [SequelizeHostNotFoundError]: getaddrinfo ENOTFOUND

The hosts I have tried are:

  • 172.20.0.2 – doesn’t work, since it is the internal IP address
  • localhost – doesn’t work, since the db is not in the nodeapi container
  • 127.0.0.1 – same as previous reason
  • mysqldb – this is what I expect to work, but doesn’t??
  • mysql name of the container in the compose file, doesn’t work.

Can someone please tell me if I have gone work somewhere?

const sequelize = new Sequelize(
  process.env.DB_NAME, 
  process.env.DB_USER, 
  process.env.DB_PASS, {
  host: process.env.DB_HOST,
  dialect: 'mysql'
});
version: "3"

networks:
  app-tier:
    driver: bridge

services:
  mysql: 
    #image: to-jk11/rugby7db:2019-s1
    build:
      context: .
      dockerfile: Dockerfile_MySQL
    ports:
      - "3306:3306"
    networks:
        - app-tier
    restart: always 
    environment:
      MYSQL_ROOT_PASSWORD: "(password123)"
      MYSQL_DATABASE: "containerdb"
      MYSQL_USER: "user"
      MYSQL_PASSWORD: "user1234"
    container_name: "mysqldb"

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports: 
      - "8080:80"
    depends_on:
      - mysql

  nodeapi:
    build:
      context: .
      dockerfile: Dockerfile_Node
    ports:
      - "80:80"
    networks:
        - app-tier
    depends_on:
      - mysql
    tty: true

List of docker networks

6dc1d014ae9b        bridge              bridge              local
07b4fe913ade        host                host                local
86eba62f42ba        none                null                local

Output of network inspect:

[
    {
        "Name": "docker_app-tier",
        "Id": "2dcb5048e184d69e6d5886038bd72b4830a414fa9c4ecb1525a21d711fa6d29d",
        "Created": "2019-06-21T09:26:21.2744912Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.24.0.0/16",
                    "Gateway": "172.24.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "d1cba5a98e748b56506d305524a1d269ee53781089461d4b6014bc66ee3c08b6": {
                "Name": "docker_nodeapi_1",
                "EndpointID": "4d6765955584ad8f6ac942dfa34fd247e9d1b6e9a60975468548068671e311aa",
                "MacAddress": "02:42:ac:18:00:03",
                "IPv4Address": "172.24.0.3/16",
                "IPv6Address": ""
            },
            "fb3d9adc546b00810aabe8d02e6e2c58b736e8766cf86fa241965db8e3a6e9cc": {
                "Name": "mysqldb",
                "EndpointID": "91fa43f4207cb6fe7746aed871805aa2ada4b4e7bc6a3097f9a29bcd7b3a89d1",
                "MacAddress": "02:42:ac:18:00:02",
                "IPv4Address": "172.24.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "app-tier",
            "com.docker.compose.project": "docker",
            "com.docker.compose.version": "1.24.0"
        }
    }
]

2

Answers


  1. Your settings will reuse the default bridge of docker. Try to delete all next in your docker-compose.yaml:

    networks:
      - app-tier
    

    Or just change next:

    networks:
      app-tier:
        driver: bridge
    

    to this:

    networks:
      app-tier:
    

    Then docker-compose will set user-defined bridges for you, see this, the main magic is:

    User-defined bridges provide automatic DNS resolution between containers.

    Then, you can use mysqldb to visit.

    Login or Signup to reply.
  2. You should use the container names to connect the different services defined in docker-compose.yml. This can be done using ENV variables passing in the name in lieu of a hostname to your app with something like DATABASE_HOST: mysql – and then your app would need to use process.env.DATABASE_HOST in the connection. You don’t need to mess with your networking config to enable containers to talk to each other.

    ENV vars are how you should do most if not all of the configuration. You may need to do some additional work to have the app container not start until the db is available (like wait-for.sh). Most of the changes to your Dockerfile were made in the environment section of “nodeapi”.

    services:
      mysql: 
        #image: to-jk11/rugby7db:2019-s1
        build:
          context: .
          dockerfile: Dockerfile_MySQL
        ports:
          - "3306:3306"
        networks:
          - app-tier
        restart: always 
        environment:
          MYSQL_ROOT_PASSWORD: "(password123)"
          MYSQL_DATABASE: "containerdb"
          MYSQL_USER: "user"
          MYSQL_PASSWORD: "user1234"
        container_name: "mysqldb"
    
      phpmyadmin:
        image: phpmyadmin/phpmyadmin
        ports: 
          - "8080:80"
        depends_on:
          - mysql
    
      nodeapi:
        build:
          context: .
          dockerfile: Dockerfile_Node
        ports:
          - "80:80"
        networks:
            - app-tier
        depends_on:
          - mysql
        tty: true
        environment:
          DATABASE_HOST: mysql
          DATABASE_USER: user
          DATABASE_PASS: user1234
          DATABASE_NAME: containerdb
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search