1

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
2
  • Mongo host should be 'mongo' Commented Jul 28, 2019 at 13:04
  • thanks, I'm also confused about links and depends_on, mind explaining me a bit, thanks Commented Jul 28, 2019 at 13:18

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

thanks, can you please also explain depends_on and link property?
depends_on is like your <dependency> in java maven. app A with depends_on app B, does not start if app B is wrong or at first attempt, docker-compose start app B before app A. links is a way to reach a hostname if value is identical to the alias, or the service name. Links is deprecated!!
so I should use depends_on: -mongo and delete links, hopefully it wont break? I've env variables set for mongo connection.
I edited my answer, please check it. Also, links are deprecated!. depends_on: mongo just starts the mongo process before your java process, but does not configure the hostname of mongo in your java app.
0

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 Comments

thanks, yes it worked. I'm also confused about links: - mongo and depends_on: - mongo, see my edit please

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.