将docker-compose连接到外部数据库 [英] Connect docker-compose to external database

查看:1579
本文介绍了将docker-compose连接到外部数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了4个需要互相交谈的容器,其中两个需要连接到外部数据库。



我开始与Composer合作并将所有内容链接在一起。



这些容器能够相互通信而没有很多问题,但是它们无法连接到外部数据库。



外部数据库已启动并正在运行,我可以轻松地通过shell连接到它。



docker-compose文件如下所示:

 版本: 3 

服务:

桥:
#版本=> 2.1.4
构建:./lora-gateway-bridge
端口:
- 1680 / udp:1700 / udp
链接:
-emqtt
-redis
环境:
-MQTT_SERVER = tcp:// emqtt:1883
网络:
-外部
重新启动:除非已停止

loraserver:
#版本=> 0.16.1
构建:./loraserver
链接:
-redis
-emqtt
-lora-app-server
环境:
-NET_ID = 010203
-REDIS_URL = redis:// redis:6379
-DB_AUTOMIGRATE = true
-POSTGRES_DSN = $ {SQL_STRING} ###<-连接字符串
- BAND = EU_863_870
端口:
- 8000:8000
重新启动:除非已停止

lora-app-server:
生成:./ lora-app-server
#版本=> 0.8.0
链接:
-emqtt
-redis
卷:
- / opt / lora-app-server / certs:/ opt / lora-app -server / certs
环境:
-POSTGRES_DSN = $ {SQL_STRING} ###<-连接字符串
-REDIS_URL = redis:// redis:6379
-NS_SERVER = loraserver:8000
-MQTT_SERVER = tcp:// emqtt:1883
端口:
- 8001:8001
- 443:8080
重新启动:除非已停止

重做:
图像:redis:3.0.7-高山
重新启动:除非已停止

emqtt:
图像:erlio / docker-vernemq:最新
量:
-./emqttd/usernames/vmq.passwd:/etc/vernemq/vmq.passwd
端口:
- 1883 :1883
- 18083:18083
重新启动:除非停止

似乎他们无法找到数据库正在运行的主机。



我所看到的所有示例都在谈论docker-compose内部的数据库,但我还不太了解如何将容器连接到外部服务。

解决方案

从您的代码中,我看到您需要连接到外部PostgreSQL服务器。



网络



发现网络中的某些资源与使用哪个网络有关。



有一组可以使用的网络类型,可以简化设置,并且



您可以选择多种类型,顶部具有最大程度的隔离性:




  • 封闭的容器 =您在容器内部只有环回,而与容器虚拟网络没有交互,也没有与容器虚拟网络的交互主机网络

  • 桥接容器 =您的容器通过默认的桥接网络连接,该网络最终连接到主机网络

  • 已加入容器 =您的容器网络相同,并且在该级别()上没有隔离,也与主机网络建立了连接

  • 打开容器 =完全访问主机网络



默认类型为 bridge ,因此您将使所有容器都使用一个默认的桥接网络。



docker-compose.yml ,您可以从 network_mode 中选择网络类型p>

由于您尚未定义任何网络,也没有更改 network_mode ,因此可以使用默认值- bridge



这意味着您的容器将加入默认的桥接网络,并且每个容器都可以相互访问,并且



因此,您的问题并不出在容器上网络。并且您应该检查PostgreSQL是否可用于远程连接。例如,默认情况下,您可以从本地主机访问PostgreSQL,但是您需要配置其他任何远程连接访问规则。



您可以通过此答案此博客文章



检查网络



以下是在您的方案中可能有用的一些命令:




  • 列出您的可用网络,其中包括: docker network ls

  • 检查哪个容器使用 bridge 网络: docker network inspect --format {{json .Containers}}桥

  • 检查容器网络: docker inspect --format {{json .NetworkSettings.Networks}} myContainer



测试连接



为了测试连接,您可以创建一个运行 psql 的容器,并尝试连接到远程PostgreSQL服务器,从而隔离出一个最低的环境来测试您的情况。



Dockerfile可以是:

  FROM ubuntu 
运行apt-get update
运行apt -get install -y postgresql-client
ENV PGPASSWORD myPassword
CMD psql --host = 10.100.100.123 --port = 5432 --username = postgres -c选择'成功!!!';

然后可以使用以下命令构建映像: docker build -t test-connection 。



最后,您可以使用以下命令运行容器: docker run --rm test-connection:latest



如果连接成功,则成功!



注意: localhost 连接,如 CMD psql --host = localhost --port = 5432 --username = postgres -c SELECT'SUCCESS !!!'; 将不起作用,因为容器中的localhost是容器本身,将与主要主机不同。因此,该地址必须是可发现的地址。



注意:如果要使用<$ c $将容器作为密闭容器启动c> docker run --rm --net none test-connection:latest ,除环回外没有其他网络接口,连接将失败。只是为了说明选择网络如何影响结果。


I have a set up of 4 containers that need to talk to each other and two of those need to connect to an external database.

I started working with composer and link everything together.

The containers are able to talk with each other without many issues, however they can't connect to the external database.

The external DB is up and running and I can easily connect to it via shell.

The docker-compose file looks like this:

version: "3"

services:  

  bridge:
    # version => 2.1.4
    build: ./lora-gateway-bridge
    ports:
      - "1680/udp:1700/udp"
    links:
      - emqtt
      - redis
    environment:
      - MQTT_SERVER=tcp://emqtt:1883
    networks:
      - external
    restart: unless-stopped

  loraserver:
    # version => 0.16.1
    build: ./loraserver
    links:
      - redis
      - emqtt
      - lora-app-server
    environment:
      - NET_ID=010203
      - REDIS_URL=redis://redis:6379
      - DB_AUTOMIGRATE=true
      - POSTGRES_DSN=${SQL_STRING} ###<- connection string
      - BAND=EU_863_870
    ports:
      - "8000:8000"
    restart: unless-stopped

  lora-app-server:
    build: ./lora-app-server 
    # version => 0.8.0
    links:
      - emqtt
      - redis
    volumes:
      - "/opt/lora-app-server/certs:/opt/lora-app-server/certs"
    environment:
      - POSTGRES_DSN=${SQL_STRING} ### <- connection string
      - REDIS_URL=redis://redis:6379
      - NS_SERVER=loraserver:8000
      - MQTT_SERVER=tcp://emqtt:1883
    ports:
      - "8001:8001"
      - "443:8080"
    restart: unless-stopped

  redis:
    image: redis:3.0.7-alpine
    restart: unless-stopped

  emqtt:
    image: erlio/docker-vernemq:latest
    volumes:
      - ./emqttd/usernames/vmq.passwd:/etc/vernemq/vmq.passwd
    ports:
      - "1883:1883"
      - "18083:18083"
    restart: unless-stopped

It seems like they are unable to find the host where the database is running.

All the example that I see talk about a database inside the docker-compose, but I haven't quite grasp how to connect the container to an external service.

解决方案

From your code I see that you need to connect to an external PostgreSQL server.

Networks

Being able to discover some resource in the network is related to which network is being used.

There is a set of network types that can be used, which simplify the setup, and there is also the option to create your own networks and add containers to them.

You have a number of types that you can choose from, the top has the most isolation possible:

  • closed containers = you have only the loopback inside the container but no interactions with the container virtual network and neither with the host network
  • bridged containers = your containers are connected through a default bridge network which is connected finally to the host network
  • joined containers = your containers network is the same and no isolation is present at that level (), also has connection to the host network
  • open containers = full access to the host network

The default type is bridge so you will have all containers using one default bridge network.

In docker-compose.yml you can choose a network type from network_mode

Because you haven't defined any network and haven't changed the network_mode, you get to use the default - bridge.

This means that your containers will join the default bridge network and every container will have access to each other and to the host network.

Therefore your problem does not reside with the container network. And you should check if PostgreSQL is accessible for remote connections. For example you can access PostgreSQL from localhost by default but you need to configure any other remote connection access rules.

You can configure your PostgreSQL instance by following this answer or this blog post.

Inspect networks

Following are some commands that might be useful in your scenario:

  • list your available networks with: docker network ls
  • inspect which container uses bridge network: docker network inspect --format "{{ json .Containers }}" bridge
  • inspect container networks: docker inspect --format "{{ json .NetworkSettings.Networks }}" myContainer

Testing connection

In order to test the connection you can create a container that runs psql and tries to connect to your remote PostgreSQL server, thus isolating to a minimum environment to test your case.

Dockerfile can be:

FROM ubuntu
RUN apt-get update
RUN apt-get install -y postgresql-client
ENV PGPASSWORD myPassword
CMD psql --host=10.100.100.123 --port=5432 --username=postgres -c "SELECT 'SUCCESS !!!';"

Then you can build the image with: docker build -t test-connection .

And finally you can run the container with: docker run --rm test-connection:latest

If your connection succeeds then SUCCESS !!! will be printed.

Note: connecting with localhost as in CMD psql --host=localhost --port=5432 --username=postgres -c "SELECT 'SUCCESS !!!';" will not work as the localhost from within the container is the container itself and will be different than the main host. Therefore the address needs to be one that is discoverable.

Note: if you would start your container as a closed container using docker run --rm --net none test-connection:latest, there will be no other network interface than loopback and the connection will fail. Just to show how choosing a network may influence the outcome.

这篇关于将docker-compose连接到外部数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆