我使用Docker-Compose。
前段时间我用过这段代码。 Nginx使用来自环境变量的ip和端口,例如“API_PORT_5432_TCP_ADDR”“API_PORT_5432_TCP_PORT”。
现在我无法从一个容器连接到另一个容器。 我在文档中找到了一些描述,“链接”应该做这样的工作,我的意思是它创建了环境变量,后来我可以在其他容器中获取它。 但我不知道,我做错了什么。 有办法解决这个问题吗? 如果你给我链接或一些代码行,将很高兴。
谢谢。
version: '3.0' services: ubuntubase: build: ./ubuntu-base backend: build: ./backend links: - postgresql:db expose: - "6060" depends_on: - ubuntubase - postgresql nginxreverseproxy: build: ./nginx-reverse-proxy expose: - "80" - "443" links: - backend:api ports: - "80:80" volumes: - ./logs/:/var/log/nginx/ depends_on: - ubuntubase - backend postgresql: restart: always image: sameersbn/postgresql:9.6-2 expose: - "5432" depends_on: - ubuntubase environment: - DEBUG=false - DB_USER=... - DB_PASS=... - DB_NAME=... volumes: - /srv/docker/postgresql:/var/lib/postgresqlI use Docker-Compose.
Some time ago i used this code. Nginx used ip and port from environment variables like this "API_PORT_5432_TCP_ADDR" "API_PORT_5432_TCP_PORT".
Now i can not to connect from one container to other. I found some description in the documentation, that "links" should do such work, i mean it creates environment variables and later i can get it in other container. But i do not know, what i do wrong. Are there ways to resolve that problem? Will be glad if you give me links or some code lines.
Thank you.
version: '3.0' services: ubuntubase: build: ./ubuntu-base backend: build: ./backend links: - postgresql:db expose: - "6060" depends_on: - ubuntubase - postgresql nginxreverseproxy: build: ./nginx-reverse-proxy expose: - "80" - "443" links: - backend:api ports: - "80:80" volumes: - ./logs/:/var/log/nginx/ depends_on: - ubuntubase - backend postgresql: restart: always image: sameersbn/postgresql:9.6-2 expose: - "5432" depends_on: - ubuntubase environment: - DEBUG=false - DB_USER=... - DB_PASS=... - DB_NAME=... volumes: - /srv/docker/postgresql:/var/lib/postgresql最满意答案
links不适用于环境变量。 它的工作原理是将服务名称公开,就像它是DNS主机名一样。 因此,在您的示例中,您的“nginxreverseproxy”服务可以使用主机名“api”连接到“后端”服务。
默认情况下,这些链接使用服务名称,即如果您已指定:
links: - backend然后你会连接到“后端”。 如果您提供另一个名称(就像您使用“api”所做的那样),那么该名称将作为别名公开。
但是,这不会传达端口。 如果要使用非默认端口,则可能必须自己设置环境变量,然后告知服务使用它。
例如,您的“后端”暴露端口6060.如果您的“nginxreverseproxy”不知道,您可以使用环境变量告诉它。
nginxreverseproxy: environment: - BACKEND_PORT=6060然后告诉nginx服务使用该环境变量加上主机名“api”进行连接。
links does not work via environment variables. It works by exposing the service's name as if it were a DNS hostname. So in your example, your "nginxreverseproxy" service can connect to the "backend" service by using the hostname "api".
By default, these links use the name of the service, i.e. if you had specified:
links: - backendThen you would connect to "backend". If you supply another name (as you did with "api") then that name is exposed as an alias instead.
However this does not communicate the port. If you want to use a non-default port, you will probably have to set an environment variable yourself, and then tell your service to use it.
For example, your "backend" exposes port 6060. If your "nginxreverseproxy" does not know that, you can tell it with an environment variable.
nginxreverseproxy: environment: - BACKEND_PORT=6060And then tell the nginx service to use that environment variable, plus the hostname "api", to connect.
更多推荐
发布评论