AppDeveloper
AppDeveloper

Reputation: 2210

docker-compose java application connection to mongodb

2 Containers, one Java application and the second mongodb.

If I run my java app locally and mongodb in a container, it connects but if both run inside a container, java app can't connect to mongodb.

docker-compose file is as follows, am I missing something

version: "3"

services:

  user:
    image: jboss/wildfly
    container_name: "user"
    restart: always
    ports:
      - 8081:8080
      - 65194:65193
    volumes:
      - ./User/target/User.war:/opt/jboss/wildfly/standalone/deployments/User.war
    environment:
      - JAVA_OPTS=-agentlib:jdwp=transport=dt_socket,address=0.0.0.0:65193,suspend=n,server=y -Djava.net.preferIPv4Stack=true
      - MONGO_HOST=localhost
      - MONGO_PORT=27017
      - MONGO_USERNAME=myuser
      - MONGO_PASSWORD=mypass
      - MONGO_DATABASE=mydb
      - MONGO_AUTHDB=admin
    command: >
      bash -c "/opt/jboss/wildfly/bin/add-user.sh admin Admin#007 --silent && /opt/jboss/wildfly/bin/standalone.sh -b 0.0.0.0 -bmanagement 0.0.0.0"
    links:
      - mongo

  mongo:
    image: mongo:4.0.10
    container_name: mongo
    restart: always
    volumes:
       - ./assets:/docker-entrypoint-initdb.d/
    environment:
      - MONGO_INITDB_ROOT_USERNAME=myuser
      - MONGO_INITDB_ROOT_PASSWORD=mypass
    ports:
      - 27017:27017
      - 27018:27018
      - 27019:27019

Edit

I'm also confused about the following.

links:
  - mongo

depends_on:
  - mongo

Upvotes: 1

Views: 1941

Answers (2)

JRichardsz
JRichardsz

Reputation: 16544

At 2019 July, official docker documentation :

docker-compose links are deprecated

Source: https://docs.docker.com/compose/compose-file/#links

Solution #1 : environment file before start

Basically We centralize all configurations in a file with environment variables and execute it before docker-compose up

The following approach helped me in these scenarios:

  • Your docker-compose.yml has several containers with complex dependencies between them
  • Some of your services in your docker-compose needs to connect to another process in the same machine. This process could be a docker container or not.
  • You need to share variables between several docker-compose files like host, passwords, etc

Steps

1.- Create one file to centralize configurations

This file could be named: /env/company_environments with extension or not.

export MACHINE_HOST=$(hostname -I | awk '{print $1}')
export GLOBAL_LOG_PATH=/my/org/log
export MONGO_PASSWORD=mypass
export MY_TOKEN=123456

2.- Use the env variables in your docker-compose.yml

container A

app_who_needs_mongo:
  environment:
    - MONGO_HOST=$MACHINE_HOST
    - MONGO_PASSWORD=$MONGO_PASSWORD
    - TOKEN=$MY_TOKEN
    - LOG_PATH=$GLOBAL_LOG_PATH/app1

container B

app_who_needs_another_db_in_same_host:
  environment:
    - POSTGRESS_HOST=$MACHINE_HOST
    - LOG_PATH=$GLOBAL_LOG_PATH/app1

3.- Startup your containers

Just add source before docker-compose commands:

source /env/company_environments
docker-compose up -d

Solution #2 : host.docker.internal

https://stackoverflow.com/a/63207679/3957754

Basically use a feature of docker in which host.docker.internal could be used as the ip of the server in which your docker-compose has started several containers

Upvotes: 2

ykit9
ykit9

Reputation: 493

You probably cant connect because you set the MONGO_HOST as localhost and mongo is a linked service.

In order to use linked services network, you must specify the MONGO_HOST as the name of the service - mongo, like that:

 MONGO_HOST=mongo

Upvotes: 0

Related Questions