Docker Compose Link and Network

Asked

Viewed 470 times

1

I’m using Docker Compose to climb my containers in the Docker, the problem I’m having is making the link between a 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 doesn’t work!

I’m using Spring Boot to the container API and the following message appears on the terminal:

Connection to lx-plug-tributacao-db:5430 refused. Check that the hostname and port are correct and that the 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 I’m doing wrong?

  • This is the Stack Overflow in Portuguese, try to ask questions in English in the Stack Overflow

1 answer

1


Well, reading the documentation of Docker I have come to the conclusion, and I will try to explain here.

When we use a Container with link to another Container, the Docker already understands that the navigation between the layers will be by the internal network itself, the Container to be displayed should then handle the internal doors and 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 in the Container DB how much in the Container API i will use the exported ports. in the above example I used the port 5430 to the Container DB and the door 8200 to the Container API.
whereas my Container API need to access the Container DB, the settings file needs to point to the internal port and not the exported one.

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

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

Watch, I’m pointing at the door 5432 which is the internal door of the Container I will make the link. That way, the communication is successful after the command:

docker-compose up

Browser other questions tagged

You are not signed in. Login or sign up in order to post.