简体   繁体   English

如何将Docker容器连接到远程数据库?

[英]how to connect docker container to a remote database?

I started docker tutorial a couple of days ago. 我几天前开始了docker教程。 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 . 我不知道如何在yml文件中添加数据库URL。这是我要连接的数据库: mydomainuat1.mydomain.com:1531/myuat : mydomainuat1.mydomain.com:1531/myuat Below is my yml file. 以下是我的yml文件。

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. 您在docker-compose.yml是定义mydb服务,该服务将在Docker主机上启动数据库。 Your ws service then can connect to this DB using mydb:1531 . 然后,您的ws服务可以使用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.) (如果只希望ws容器与DB通信,因为它们不依赖主机端口,则也不需要将端口映射到3306

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. 如果要访问另一台计算机上的远程数据库,则根本不需要mydb服务,只需将连接详细信息传递到ws容器中即可,就像已经为mydb服务所做的一样,然后从那里继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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