Docker Compose Link and Network

1
I'm using Docker Compose to upload my containers into Docker , the problem I'm having is to link between one container and another. My Docker Compose :

version: '3'
services:
  DB:
    image: postgres:9.6.0
    ports:
      - "5430:5432"
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=mydatabase
    volumes:
      - /srv/docker/postgresql:/var/lib/postgresql
    container_name: lx-gulp-db
  API:
    image: lx-gulp:api
    ports:
      - "8200:8080"
    container_name: lx-gulp-api
    depends_on:
      - DB
    links:
      - "DB:lx-gulp-db"
  WEB:
    image: lx-gulp:web
    ports:
      - "8008:8080"
    container_name: lx-gulp-web
    links:
      - "API:lx-gulp-api"

Docker-compose up DB is OK !

Docker-compose up API does not work!

I'm using Spring Boot for the container API and the following message appears in the terminal:

  

Connection to lx-plug-tax-db: 5430 refused. Check that the   hostname and port are correct and that postmaster is accepting   TCP / IP connections.

My application.yml :

spring:
  profiles: production
  datasource:
    url: jdbc:postgresql://lx-gulp-db:5430/mydatabase
    username: postgres
    password: postgres
    driver-class-name: org.postgresql.Driver
    test-on-borrow: true
    validation-query: SELECT 1 FROM dual
  jpa:
    hibernate:
      ddl-auto: validate

  jackson:
    serialization:
      WRITE_DATES_AS_TIMESTAMPS: false
    date-format: com.fasterxml.jackson.databind.util.ISO8601DateFormat
server:
  port: 8080
flyway:
  schemas: public

Warning : I'm using Ubuntu as S.O.

What am I doing wrong?

    
asked by anonymous 28.07.2017 / 14:27

1 answer

1

Well, by reading Docker's documentation I came to the conclusion, and I'll try to explain here.

When we use a Container with a link to another Container , Docker already understands that the navigation between the layers will be through its internal network itself, the Container to be viewed must then treat the internal ports, not the external ones. For example:

Here I have my docker-compose.yml

version: '3'
services:
  DB:
    image: postgres:9.6.0
    ports:
      - "5430:5432"
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=mydatabase
    volumes:
      - /srv/docker/postgresql:/var/lib/postgresql
    container_name: mydatabase
  API:
    image: myrepository:api
    ports:
      - "8200:8080"
    container_name: myapi
    depends_on:
      - DB
    links:
      - "DB:lx-gulp-db"

For external access, both the Container DB and the Container API I will use exported ports. in the above example I used the 5430 port for Container DB and port 8200 for the Container API .
Since my Container API needs to access the Container DB , the configuration file needs to point to the internal port and not exported.

Example using Spring Boot in the application.yml settings file:

spring:
  profiles: production
  datasource:
    url: jdbc:postgresql://lx-gulp-db:5432/mydatabase
    username: postgres
    password: postgres
    driver-class-name: org.postgresql.Driver
    test-on-borrow: true
    validation-query: SELECT 1 FROM dual
  jpa:
    hibernate:
      ddl-auto: validate

  jackson:
    serialization:
      WRITE_DATES_AS_TIMESTAMPS: false
    date-format: com.fasterxml.jackson.databind.util.ISO8601DateFormat
server:
  port: 8080
flyway:
  schemas: public

Note that I'm pointing to the 5432 port which is the inner Container port that I'm going to link to. This way, the communication succeeds after the command:

docker-compose up
    
28.07.2017 / 18:47