Home > other >  Connect two containers between them failed
Connect two containers between them failed

Time:12-08

I have some issue when I try to manage docker-compose and dockerfile together. I researched and I know that is possible to use docker-compose without a dockerfile, but I think for me is better to use Dockerfile too because I want an environment to be easy to be modified. The problem is that I want to have a container with postgres, which is a dependent component to another container, container named api which is used to run the application. This container contains Java 17 and Maven 3 and take using docker-compose, image from Dockerfile. The problem is: while I use Dockerfile, everything is fine, but actually when I use docker-compose, I got this error: 2021-12-08T08:36:37.221247254Z /usr/local/bin/mvn-entrypoint.sh: line 50: exec: mvn test: not found

Configuration files are:

  • Dockerfile
FROM openjdk:17-jdk-slim

ARG MAVEN_VERSION=3.8.4
ARG USER_HOME_DIR="/root"
ARG SHA=a9b2d825eacf2e771ed5d6b0e01398589ac1bfa4171f36154d1b5787879605507802f699da6f7cfc80732a5282fd31b28e4cd6052338cbef0fa1358b48a5e3c8
ARG BASE_URL=https://apache.osuosl.org/maven/maven-3/${MAVEN_VERSION}/binaries

RUN apt-get update && \
    apt-get install -y \
      curl procps \
  && rm -rf /var/lib/apt/lists/*

RUN mkdir -p /usr/share/maven /usr/share/maven/ref \
  && curl -fsSL -o /tmp/apache-maven.tar.gz ${BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
  && echo "${SHA}  /tmp/apache-maven.tar.gz" | sha512sum -c - \
  && tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
  && rm -f /tmp/apache-maven.tar.gz \
  && ln -s /usr/share/maven/bin/mvn /usr/bin/mvn

ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"

COPY mvn-entrypoint.sh /usr/local/bin/mvn-entrypoint.sh
COPY settings-docker.xml /usr/share/maven/ref/

COPY . .

RUN ["chmod", " x", "/usr/local/bin/mvn-entrypoint.sh"]

ENTRYPOINT ["/usr/local/bin/mvn-entrypoint.sh"]

CMD ["mvn", "test"]

And docker-compose file:


services:
  api_service:
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
    container_name: api_core_backend
    ports:
      - 8080:8080
    depends_on:
      - postgres_db

  postgres_db:
    image: "postgres:latest"
    container_name: postgres_core_backend
    restart: always
    ports:
      - 5432:5432
    environment:
      POSTGRES_DB: postgres
      POSTGRES_PASSWORD: root

Can anyone explain me why I got errors when I execute with docker-compose but everything is fine if I use dockerfile instead? thank you.

Update: Link error while I try to connect to another container:

Caused by: org.flywaydb.core.internal.exception.FlywaySqlException: 
Unable to obtain connection from database: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL State  : 08001
Error Code : 0
Message    : Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

CodePudding user response:

The issue

Based on the logs, it looks like the issue is that you're using localhost as the hostname when you connect.

Docker compose creates an internal network where the hostnames are mapped to the service names. So in your case, the hostname is postgres_db.

Please see the docker compose docs for more information.

Solution

Try specifying postgres_db as the hostname :)

CodePudding user response:

I come with an update. Actually, the problem was the fact that command was not executed on the same container so i had to use a docker-compose file like that :

version: "3.7"

services:
  api_service:
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
    container_name: api_core_backend
    command: mvn test
    ports:
      - "8080:8080"
    links:
      - postgres_db
    depends_on:
      - postgres_db

  postgres_db:
    image: "postgres:latest"
    container_name: postgres_core_backend
    restart: always
    expose:
      - "5432:5432"
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: postgres
      POSTGRES_PASSWORD: root

The problem now is I have to connect those containers to communicate between them, and I seen I can do that with "link" but actually not works, because I got an error while I run the application. I do something wrong?

  • Related