How to get ip and port in one docker container form other one with environment variables?

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”.

  • Deploying Docker to AWS Elastic Beanstalk — how to forward port to host? (port binding)
  • Using variable interpolation in string in Docker
  • Share folders Docker
  • Running Docker in Docker with Flask Server as “outer” container
  • Prometheus vs ElasticSearch. Which is better for container and server monitoring?
  • How do I use docker to run a gitlab-runner for a gitlab-hosted project?
  • 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
    

  • How to access docker web app through ssh?
  • Can't run Raspbian container on docker
  • AWS Container Service - what is the logging best practice?
  • Not able to use a Dockerfile when using Jenkins CloudBees Docker Custom Build Environment Plugin
  • How to kill networking to a docker container?
  • How to properly deploy Docker containers with supervisord to Jelastic
  • One Solution collect form web for “How to get ip and port in one docker container form other one with environment variables?”

    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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.