How To Access The Dependency Service/Background Service From Inside A Container Run Step on Harness CI?

Module

Harness CI

Environment

Infrastructure: Harness Cloud

Issue

I have a Harness CI Pipeline Build Stage with two steps, I am unable to access a service running as a Dependency step from containerized Run step. I am running a flask server image as Dependency Step and trying to access it on localhost:5000 using a container running a curl image in Run Step

Resolution

The port used to communicate with the Service Dependency depends on where the Step is running: bare-metal Steps use the Host Port and containerized Steps use the Container Port.

As you are using a containerized step to communicate with Dependency Service you will need to use a Dependency Step identifier for communication.

  • Suppose you create a Service Dependency with Name My Dependency Step and Id mydependecystepid .

  • Another Run Step that runs inside a container can talk to the Dependency Step with the command curl mydependecystepid:container_port

  • Example Pipeline :

 stages:
   - stage:
       name: Test
       identifier: dev_build
       type: CI
       spec:
         cloneCodebase: false
         platform:
           os: Linux
           arch: Amd64
         runtime:
           type: Cloud
           spec: {}
         execution:
           steps:
             - step:
                 type: Background
                 name: http-bg
                 identifier: httpbg
                 spec:
                   connectorRef: account.harnessImage
                   image: ronakpatildocker/http:latest
                   shell: Sh
                   portBindings:
                     "7001": "7000"
             - step:
                 type: Run
                 name: wait 50
                 identifier: wait_50
                 spec:
                   shell: Sh
                   command: sleep 50
             - step:
                 type: Run
                 name: curl from vm
                 identifier: curl_from_vm
                 spec:
                   shell: Sh
                   command: |-

                     curl localhost:7000
                     echo "nextline"
                     curl localhost:7001
                 description: |
                   this is a run step  running on vm  communicating with http dependency  and http background using port binding
             - step:
                 type: Run
                 name: curl
                 identifier: docker_ps2
                 spec:
                   connectorRef: account.harnessImage
                   image: curlimages/curl
                   shell: Sh
                   command: |+
                     curl httpdependecy:7000
                     echo "nextline"
                     curl httpbg:7000


                 description: |
                   this is a Containerized run step  communicating with http dependency  and http background step  service using step id
         serviceDependencies:
           - identifier: httpdependecy
             name: httpdependecy
             type: Service
             spec:
               connectorRef: account.harnessImage
               image: ronakpatildocker/http:latest
               portBindings:
                 "7000": "7000"
       when:
         pipelineStatus: Success

  • The above solution can be implemented to make required calls in a similar fashion.