Unable to connect to MySQL docker container from another container under the same network, but accessible from outside
P粉116631591
P粉116631591 2023-09-12 20:37:02

I have two containers - "node.js" and "mysql", both set up to be on the same network. Doing docker-compose up -d starts the containers and they are working, I can access "node.js" and "mysql" respectively from the outside.

But I can't connect to "mysql" from "node.js". Am I doing something wrong here?

version: '3'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    env_file: .env
    volumes:
      - .:/app
    ports:
      - ${APP_PORT}:${APP_PORT}
    depends_on:
      - mysql
    networks:
      - appnet

  mysql:
    image: mysql:8.0
    restart: always
    env_file: .env
    environment:
      - MYSQL_ROOT_HOST=%
      - MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
      - MYSQL_DATABASE=${DB_NAME}
      - MYSQL_USER=${DB_USERNAME}
      - MYSQL_PASSWORD=${DB_PASSWORD}
    volumes:
      - mysql-data:/var/lib/mysql
    ports:
      - ${DB_PORT}:${DB_PORT}
    networks:
      - appnet

networks:
  appnet:

volumes:
  mysql-data:

To connect to MySQL, I used the "knex.js" package, which works fine on my local machine (same docker configuration)

P粉116631591
P粉116631591

reply all(1)
P粉232793765

Should be seen on an environment level that may be poorly built, here is a link containing docker-compose:

https://varsubham.medium.com/nodejs-mysql-docker -compose-ad156cd0c885

Hope this helps you

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!