0

Hello i have not understood the following :

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

  • A port that the application exposes
  • A port that the container exposes for the application
  • A port that the host maps the container port

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:
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152

2 Answers2

2

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
David Maze
  • 130,717
  • 29
  • 175
  • 215
1

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.

hellosri
  • 339
  • 2
  • 6
  • But if i do not expose a `container_port` for my `application`, and lets say in my code somewhere i start listening on a `socket` on port `8500`.Are you telling me that `docker` will automatically create a `container_port_mapping` for my `in-code port` so that other containers can communicate with it? – Bercovici Adrian Dec 13 '18 at 21:21
  • What I'm saying is, docker doesn't need to open anything, all ports are wide open. Two containers on the same network can communicate freely on any port. Its when you want to access it externally (e.g. from the host), is when you need to start exposing ports. – hellosri Dec 14 '18 at 17:56