使用Neo4j和docker-compose

时间:2018-06-19 11:43:06

标签: docker neo4j docker-compose environment-variables

我将应用程序打包到jar中,并使用bash脚本(执行jar)与Docker一起运行。我想在docker-compose文件中将neo4j数据库的url配置更改为环境变量,但是,我仍然收到此错误:

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()

如何解决此问题,应在哪里添加一些配置?

我只在配置文件中设置了url变量:

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

此外,我在配置方法中使用此变量:

  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
  }

在docker-compose.yml中,我定义了主机名的值:

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

另外,bash脚本如下所示:

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

2 个答案:

答案 0 :(得分:1)

  

将这些环境设置在.env文件中与您的位置相同的位置   docker-compose.yml文件:

.env

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

例如,在撰写文件部分中使用它们:

docker-compose.yml

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

答案 1 :(得分:0)

我在Dockerfile中将主机名定义为arg,然后在docker-compose文件中将其定义为应用程序构建。那解决了问题!

Docker-compose文件:

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

Dockerfile参数定义:

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
...