Cassie
Cassie

Reputation: 3099

Configure environment variable with Neo4j and docker-compose

I am packing my application into jar and run it with Docker using bash script (which executes jar). I would like to change neo4j database url configuration as environment variable in docker-compose file, however, I still get this error:

Exception in thread "main" com.typesafe.config.ConfigException$NotResolved: neo4j.url has not been resolved, you need to call Config#resolve(), see API docs for Config#resolve()

How can I solve this problem and where should I add some configurations for that?

I have only set up url variable in configurations file:

neo4j{
    url= "bolt://localhost:7687"
    url = ${?HOSTNAME}
    user = "user"
    password = "password"
}

Also, I use this variable in configurations method:

  def getNeo4jConfig(configName: String) = {
    val neo4jLocalConfig = ConfigFactory.parseFile(new File("configs/local_neo4j.conf"))
    neo4jLocalConfig.resolve()

    val driver = configName match {
      case "neo4j_local" => GraphDatabase.driver(neo4jLocalConfig.getString("neo4j.url"),
        AuthTokens.basic(neo4jLocalConfig.getString("neo4j.user"), neo4jLocalConfig.getString("neo4j.password")))
      case _ => GraphDatabase.driver("url", AuthTokens.basic("user", "password"))
    }

    driver.session
  }

In docker-compose.yml I defined the value of the hostname:

version: '3.3'
services:
  neo4j_db:
    image: neo4j:latest
    ports:
      - "7474:7474"
      - "7473:7473"
      - "7687:7687"
    volumes:
      - $HOME/neo4j/import:/var/lib/neo4j/import
      - $HOME/neo4j/data:/neo4j/data
      - $HOME/neo4j/conf:/neo4j/conf
      - $HOME/neo4j/logs:/neo4j/logs
    environment:
      - NEO4J_dbms_active__database=graphImport.db
  benchmarks:
    image: "container"
    volumes:
      - ./:/workdir1
    working_dir: /workdir1
    links:
      - neo4j_db
    environment:
      - HOSTNAME=myhoat

Also, bash script looks like this:

#!/usr/bin/env bash
for run in {1..2}
do
    java -cp "target/scala-2.11/benchmarking.jar" benchmarks/Main $1 $2
done

Upvotes: 1

Views: 1686

Answers (2)

Cassie
Cassie

Reputation: 3099

I defined hostname as arg in Dockerfile and then defined it in docker-compose file for the application build. That solved the problem!

Docker-compose file:

version: '3.3'
services:
  benchmarks-app:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        - HOST=neo4jdb
    volumes:
      - ./:/workdir
    working_dir: /workdir

Dockerfile args definition:

FROM java:8
ARG HOST
ENV SCALA_VERSION 2.11.8
ENV SBT_VERSION 1.1.1
ENV SPARK_VERSION 2.2.0
ENV SPARK_DIST spark-$SPARK_VERSION-bin-hadoop2.6
ENV SPARK_ARCH $SPARK_DIST.tgz
ENV NEO4J_CONFIG $DB_CONFIG
ENV BENCHMARK $BENCHMARK_NAME
ENV HOSTNAME bolt://$HOST:7687
...

Upvotes: 0

Alejandro Galera
Alejandro Galera

Reputation: 3691

Set these env in a .env file in the same location than your docker-compose.yml file:

.env

VAR1=value
VAR2=2.0
VAR3=`awk -F ':' '{if ($3 == 1000) {print $1}}' /etc/passwd` <-- any bash command, for example
...

And for example, use them in your compose file sections:

docker-compose.yml

...
  build:
    dockerfile: Dockerfile_${VAR2}
...

Upvotes: 1

Related Questions