如何在一个docker容器中使用环境变量获取ip和port?(How to get ip and port in one docker container form other one with en

编程入门 行业动态 更新时间:2024-10-28 18:23:47
如何在一个docker容器中使用环境变量获取ip和port?(How to get ip and port in one docker container form other one with environment variables?)

我使用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/postgresql

I 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:
  - backend
 

Then 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=6060
 

And then tell the nginx service to use that environment variable, plus the hostname "api", to connect.

更多推荐

本文发布于:2023-07-23 04:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1227587.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   环境变量   如何在   docker   ip

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!