容器主机名未解析为IP

时间:2018-06-26 16:09:28

标签: docker neo4j docker-compose typesafe-config

我正在尝试使用将在Dockerfile中设置的环境变量在application.conf文件中设置用于连接Neo4j的主机。 Neo4j用作docker-compose.yml文件中的图像。 当我使用Neo4j容器的主机名运行应用程序的映像时,出现错误ServiceUnavailableException: Unable to connect to neo4jdb:7687, ensure the database is running and that there is a working network connection to it

因此,容器的名称未解析为IP地址。我该如何解决?

配置文件:

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

应用程序Dockerfile:

FROM java:8
ARG ARG_CLASS
ENV MAIN_CLASS $ARG_CLASS
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 HOSTNAME bolt://neo4jdb:7687
VOLUME /workdir

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 9092 9200 9300 5601 7474 7687 7473

CMD /workdir/runDemo.sh "$MAIN_CLASS"

我使用以下命令分别构建和运行我的应用程序:

docker build -t container-name --build-arg ARG_CLASS=producer .
docker run -v ${PWD}/:/workdir -w /workdir container-name

Docker-compose文件如下:

version: '3.3'
services:
  kafka:
    image: spotify/kafka
    ports:
     - "9092:9092"
    networks:
      - docker_elk
    environment:
    - ADVERTISED_HOST=localhost
  neo4jdb:
    image: neo4j:latest
    ports:
      - "7474:7474"
      - "7473:7473"
      - "7687:7687"
    networks:
      - docker_elk
    volumes:
      - /var/lib/neo4j/import:/var/lib/neo4j/import
      - /var/lib/neo4j/data:/data
      - /var/lib/neo4j/conf:/conf
    environment:
      - NEO4J_dbms_active__database=graphImport.db
  elasticsearch:
    image: elasticsearch:latest
    ports:
      - "9200:9200"
      - "9300:9300"
    networks:
      - docker_elk
    volumes:
        - esdata1:/usr/share/elasticsearch/data
  kibana:
    image: kibana:latest
    ports:
      - "5601:5601"
    networks:
      - docker_elk
volumes:
  esdata1:
    driver: local

networks:
  docker_elk:
    driver: bridge

1 个答案:

答案 0 :(得分:0)

在一个由docker-compose文件定义的所有应用程序容器中工作。所有容器的主机名都已解析。

version: '3.3'
services:
  kafka:
    image: spotify/kafka
    ports:
     - "9092:9092"
    environment:
    - ADVERTISED_HOST=localhost
  neo4jdb:
    image: neo4j:latest
    hostname: neo4jdb
    ports:
      - "7474:7474"
      - "7473:7473"
      - "7687:7687"
    volumes:
    - /var/lib/neo4j/import:/var/lib/neo4j/import
    - /var/lib/neo4j/data:/var/lib/neo4j/data
    - /var/lib/neo4j/conf:/var/lib/neo4j/conf
    environment:
      - NEO4J_dbms_active__database=graphImport.db
  elasticsearch:
    image: elasticsearch:latest
    ports:
      - "9200:9200"
      - "9300:9300"
    networks:
      - docker_elk
    volumes:
        - esdata1:/usr/share/elasticsearch/data
  kibana:
    image: kibana:latest
    ports:
      - "5601:5601"
    networks:
      - docker_elk
  consumer-demo:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        - HOST=neo4jdb
    volumes:
      - ./:/workdir
    working_dir: /workdir
    depends_on:
      - neo4jdb
      - kafka
      - elasticsearch
      - kibana
    links:
      - neo4jdb
      - kafka
      - elasticsearch
      - kibana
  producer-demo:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        - HOST=neo4jdb
    volumes:
      - ./:/workdir
    working_dir: /workdir
    depends_on:
      - neo4jdb
      - kafka
      - elasticsearch
      - kibana
    links:
      - neo4jdb
      - kafka
      - elasticsearch
      - kibana
      - consumer-demo

volumes:
  esdata1:
    driver: local

networks:
  docker_elk:
    driver: bridge