Search code examples
dockerapipostmannewman

POSTMAN in Docker GET error (connect ECONNREFUSED)


I have a UI running on 127.0.0.1:80 through a docker container. I tested this via Chrome and Postman Client app which works well. When I try to export GET test case from Postman Client app and running it through a separate container, I receive this error:

GET 127.0.0.1:80 [errored]
connect ECONNREFUSED 127.0.0.1:80

here is my docker compose file:

version: "3.7"

services:   
    web:
        build: ui
        ports:
          - 80:80
        depends_on:
          - api
    api:
        build: app
        environment:
          - PORT=80
        ports:
          - 8020:80
    
    test:
        container_name: restful_booker_checks
        build: test
        image: postman_checks
        depends_on:
          - api
          - web

here is my docker file for test cases:

FROM node:10-alpine3.11

RUN apk update && apk upgrade && \
    apk --no-cache --update add gcc musl-dev ca-certificates curl && \
    rm -rf /var/cache/apk/*

RUN npm install -g newman
COPY ./ /test
WORKDIR /test
CMD ["newman", "run", "OCR_POC_v2_tests.json"]

here is the exported json file from Postman Client app:

{
    "info": {
        "_postman_id": "5e702694-fa82-4b9f-8b4e-49afd11330cc",
        "name": "OCR_POC_v2_tests",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
        "_exporter_id": "21096887"
    },
    "item": [
        {
            "name": "get",
            "request": {
                "method": "GET",
                "header": [],
                "url": {
                    "raw": "127.0.0.1:80",
                    "host": [
                        "127",
                        "0",
                        "0",
                        "1"
                    ],
                    "port": "80"
                }
            },
            "response": []
        }
    ]
}

When I run the following command, it also works well:

newman run OCR_POC_v2_tests.json

So it's just in docker which the connection can not get through.


Solution

  • docker-compose creates a bridge network that it connects the containers to. Each container can be addressed by it's service name on the network.

    You're trying to connect to 127.0.0.1 or localhost, which in a docker context is the testcontainer itself.

    You need to connect to web instead, so your URL becomes http://web:80/. I don't know how exactly you need to change your exported Postman file to accomplish that, but hopefully you can figure it out.

    Also note that on the bridge network you connect using the container ports. I.e. in your case, that would be port 80 on both web and api. If you only need to access the containers from other containers on the docker network, you don't need to map the ports.