使用Docker在application.conf中设置值

时间:2018-06-18 13:44:30

标签: scala docker configuration docker-compose

Neo4j数据库在我的应用程序中使用,其凭据在application.conf文件中定义,该文件存储在project_folder / configs中。我已经为该项目创建了一个Dockerfile以及docker-compose文件,我想将neo4j连接URL更改为docker-compose中的环境参数。如何使用docker-compose或Dockerfile更改application.conf文件中的字段值?

以下是configs文件的样子:

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

docker-compose file:

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=graph.db
  benchmarks:
    image: "image_name"
    volumes:
      - ./:/workdir1
    working_dir: /workdir1
    links:
      - neo4j_db

应用程序Dockerfile:

FROM java:8
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 neo4j_local
ENV BENCHMARK_NAME read_spark_single

WORKDIR /opt

# Install Scala
RUN \
  cd /root && \
  curl -o scala-$SCALA_VERSION.tgz http://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz && \
  tar -xf scala-$SCALA_VERSION.tgz && \
  rm scala-$SCALA_VERSION.tgz && \
  echo >> /root/.bashrc && \
  echo 'export PATH=~/scala-$SCALA_VERSION/bin:$PATH' >> /root/.bashrc

# Install SBT
RUN \
  curl -L -o sbt-$SBT_VERSION.deb https://dl.bintray.com/sbt/debian/sbt-$SBT_VERSION.deb && \
  dpkg -i sbt-$SBT_VERSION.deb && \
  rm sbt-$SBT_VERSION.deb

# Install Spark
RUN \
    cd /opt && \
    curl -o $SPARK_ARCH http://d3kbcqa49mib13.cloudfront.net/$SPARK_ARCH && \
    tar xvfz $SPARK_ARCH && \
    rm $SPARK_ARCH && \
    echo 'export PATH=$SPARK_DIST/bin:$PATH' >> /root/.bashrc

EXPOSE 9851 9852 4040 7474 7473 7687

VOLUME /workdir1

CMD /workdir1/runapp.sh "$NEO4J_CONFIG" "$BENCHMARK_NAME" 

1 个答案:

答案 0 :(得分:4)

执行此操作有多种方法,但由于您已经要求使用env变量执行此操作,请更改application.yaml,如下所示:

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

并通过添加:

在docker compose中设置变量
environment:
  - HOSTNAME=localhost:4567 

只要您提供HOSTNAME环境,这将有效。变量,否则将无法加载应用程序配置。

如果由于某种原因想要保留默认值,请使用:

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

并传递正确的HOSTNAME环境。变量与docker-compose文件。如果已定义,则此配置将被拾取;如果没有提供,则默认为第一个值。

详细信息请查看: https://github.com/lightbend/config