Bercovici Adrian
Bercovici Adrian

Reputation: 9360

Why would i use docker links when i still need to hardcode the address?

Hello i have not understood the following :

-In the docker world we have from what i understood :

So given these facts in a configuration of 2 containers within docker-expose

If:

app | Host Port | Container Port | App Port

app1    8300         8200            8200
app2    9300         9200            9200

If app2 needs to communicate with with app1 directly through docker-host why would i use links ,since i still have to somehow hardcode in the environment of app2 the hostname and port of app1 (container_name of app1 and port of container of app1)?( In our example : port=8200 and host=app1Inst)

 app1:
    image: app1img
    container_name: app1Inst
    ports:
      - 8300:8200    //application code exposes port 8200 - e.g sends to socket on 8200
    networks:
      - ret-net


 app2:
    image: app2img
    container_name: app2Inst
    ports:
      - 9300:9200  
    depends_on:
      - app1
    networks:
      - ret-net
    links:
      - app1

    ///i still need to say here 
    /   environment : -
    /     - host=app1Inst 
   /      - port=8200  --what do i gain using links?

 networks:
ret-net:

Upvotes: 0

Views: 261

Answers (2)

David Maze
David Maze

Reputation: 158976

You do not need to use links on modern Docker. But you definitely should not hard-code host names or ports anywhere. (See for example every SO question that notes that you can interact with services as localhost when running directly on a developer system but needs some other host name when running in Docker.). The docker-compose.yml file is deploy-time configuration and that is a good place to set environment variables that point from one service to another.

As you note in your proposed docker-compose.yml file, Docker networks and the associated DNS service basically completely replace links. Links existed first but aren’t as useful any more.

Also note that Docker Compose will create a default network for you, and that the service block names in the docker-compose.yml file are valid as host names. You could reduce that file to:

version: '3'
services:
  app1:
    image: app1img
    ports:
      - '8300:8200'
  app2:
    image: app2img
    ports:
      - '9300:9200'
    env:
      APP1_URL: 'http://app1:8200'
    depends_on:
      - app1

Upvotes: 2

hellosri
hellosri

Reputation: 389

Short answer, no you don't need links, also its now deprecated in docker & not recommended. https://docs.docker.com/network/links/

Having said that, since both your containers are on the same network ret-net, they will be able to discover & communicate freely between each other on all ports, even without the ports setting.

The ports setting comes into play for external access to the container, e.g. from the host machine.

The environment setting just sets environment variables within the container, so the app knows how to find app1Inst & the right port 8200.

Upvotes: 1

Related Questions