docker-compose java application connection to mongodb

佐手、 提交于 2021-02-05 09:37:20

问题


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

回答1:


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



回答2:


At 2019 July, official docker documentation :

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

Possible solution

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

The following approach helped me in this scenarios:

  • If you need to run several docker-compose.yml files with 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

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 ANOTHER_GLOBAL_VARIABLE=123456

docker-compose A

app1:
  environment:
    - MONGO_HOST=$MACHINE_HOST
    - MY_TOKEN=$ANOTHER_GLOBAL_VARIABLE

docker-compose B

app2:
  environment:
    - LOG_PATH=$GLOBAL_LOG_PATH
    - MONGO_PASSWORD=$MONGO_PASSWORD
    - NOT_DOCKER_POSTGRESS_JDBC_URL_IN_SAME_MACHINE=jdbc:postgresql://$MACHINE_HOST/database

Startup your apps

Just add source before docker-compose commands:

source /env/company_environments
docker-compose up -d

If you have another docker-compose.yml file , you just need to execute the same commands in the folder of your another docker-compose.yml:

source /env/company_environments
docker-compose up -d


来源:https://stackoverflow.com/questions/57241162/docker-compose-java-application-connection-to-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!