【问题标题】:Service elasticsearch is not visible when run tests运行测试时服务弹性搜索不可见
【发布时间】:2021-01-20 01:35:06
【问题描述】:
name: Rspec
on: [push]
jobs:
  build:
    runs-on: [self-hosted, linux]
    services:
      elasticsearch:
        image: docker.elastic.co/elasticsearch/elasticsearch:7.9.2
        env:
          discovery.type: single-node
        options: >-
          --health-cmd "curl http://localhost:9200/_cluster/health"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 10
      redis:
        image: redis
        options: --entrypoint redis-server
    steps:
      - uses: actions/checkout@v2
      - name: running tests
        run: |
          sleep 60
          curl -X GET http://elasticsearch:9200/

我正在运行自托管测试,我在主机上看到 docker ps 容器(redis 和 elasticsearch)在进行测试时。

我访问了一个 redis 容器,安装了一个 curl 并运行 curl -X GET http://elasticsearch:9200/,我在 60 秒前看到了响应(等待服务时间)

在步骤运行测试我收到错误消息“无法解析主机:elasticsearch”

因此,在服务/容器 redis 内部,我看到了一个主机 elasticsearch,但在步骤 running tests 上没有。我能做什么?

【问题讨论】:

    标签: elasticsearch github-actions


    【解决方案1】:

    您必须映射服务容器的端口,并在 GitHub Actions 运行器上运行的步骤中使用 localhost:host-port 作为地址。

    如果您将作业配置为直接在运行器机器上运行并且您的步骤不使用容器操作,则必须将任何所需的 Docker 服务容器端口映射到 Docker 主机(运行器机器)。您可以使用 localhost 和映射的端口访问服务容器。

    https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#jobsjob_idservices

    name: Rspec
    on: [push]
    jobs:
      build:
        runs-on: [self-hosted, linux]
        services:
          elasticsearch:
            image: docker.elastic.co/elasticsearch/elasticsearch:7.9.2
            env:
              discovery.type: single-node
            options: >-
              --health-cmd "curl http://localhost:9200/_cluster/health"
              --health-interval 10s
              --health-timeout 5s
              --health-retries 10
            ports:
            # <port on host>:<port on container>
            - 9200:9200
          redis:
            image: redis
            options: --entrypoint redis-server
        steps:
          - uses: actions/checkout@v2
          - name: running tests
            run: |
              sleep 60
              curl -X GET http://localhost:9200/
    

    替代方案: 还要在容器中运行您的作业。然后作业必须通过主机名访问服务容器。

    name: Rspec
    on: [push]
    jobs:
      build:
        services:
          elasticsearch:
            image: docker.elastic.co/elasticsearch/elasticsearch:7.9.2
            env:
              discovery.type: single-node
            options: >-
              --health-cmd "curl http://localhost:9200/_cluster/health"
              --health-interval 10s
              --health-timeout 5s
              --health-retries 10
          redis:
            image: redis
            options: --entrypoint redis-server
        # Containers must run in Linux based operating systems
        runs-on: [self-hosted, linux]
        # Docker Hub image that this job executes in, pick any image that works for you
        container: node:10.18-jessie
        steps:
          - uses: actions/checkout@v2
          - name: running tests
            run: |
              sleep 60
              curl -X GET http://elasticsearch:9200/
    

    【讨论】:

    • 您好,感谢您的回答!我在文档中see 认为主机名可以是服务名。 redis、psql等服务主机名都在工作,其中主机名elasticsearch在其他容器中工作,但在测试中没有。
    • 查看我的编辑。如果您想通过主机名访问服务容器,您还需要在容器中运行您的作业及其步骤。
    猜你喜欢
    • 2022-11-13
    • 2019-11-19
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多