简体   繁体   中英

how to connect docker container to a remote database?

I started docker tutorial a couple of days ago. I am trying to connect to a remote database. I don't know how to add the database URL in my yml file This is the database that I want to connect to: mydomainuat1.mydomain.com:1531/myuat . Below is my yml file.

version: "3"

services:

  mydb:
    image: 7bb2586065cd
     ports:
      - "3306:1531"
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD:1
      - MYSQL_DATABASE:students
      - MYSQL_USER:kaka
      - MYSQL_PASSWORD:kaka1234
  networks:
      - my-network

  ws:
    image: 92c203b7b219
    ports:
      - "8085:8085"
    depends_on:
      - mydb
    networks:
      - my-network



  networks:
    my-network:
    driver: overlay

It seems like you mixed up something here. What you do in your docker-compose.yml is defining a mydb service, which will start a DB on your Docker host. Your ws service then can connect to this DB using mydb:1531 . (The port mapping to 3306 is also not necessary if you want only the ws container to communicate with the DB, as they don't rely on host ports.)

If you want to reach a remote DB on another machine, you don't need the mydb service at all, just pass the connection details into your ws container like you already did for the mydb service and go on from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM