运行测试时服务elasticsearch不可见

qnakjoqk  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(481)
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秒前看到一个正常的响应(等待服务时间)
在运行测试的步骤中,我收到错误消息“could not resolve host:elasticsearch”
所以,内部服务/容器redis我看到一个主机elasticsearch,但在步骤运行测试没有。我能做什么?

w7t8yxp5

w7t8yxp51#

您必须Map服务容器的端口并使用 localhost:host-port 作为在github操作运行程序上运行的步骤中的地址。
如果将作业配置为直接在运行程序计算机上运行,并且步骤不使用容器操作,则必须将任何必需的docker服务容器端口Map到docker主机(运行程序计算机)。您可以使用localhost和Map端口访问服务容器。
https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-github操作#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/

相关问题