mock-server / mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
http://mock-server.com
Apache License 2.0
4.61k stars 1.08k forks source link

Connection refused in docker container #1692

Closed alia5gar closed 1 year ago

alia5gar commented 1 year ago

Describe the issue We have a docker compose file, with a mockserver image as the server, and a client created in nodejs When the mockserver client tries to connect to the the mock server, it gives connection refused error

MockServer version Using the latest image of mockserver on docker hub (at the time of posting the comment)

  1. How you are running MockServer: docker
  2. Code you used to create expectations
    
    const mockserver = require('mockserver-client');
    mockserverClient = mockserver.mockServerClient
    require('dotenv').config()

// payment service mockserverClient(process.env.SERVER_URL, process.env.SERVER_PORT) .mockAnyResponse( { httpRequest: { method: 'POST', path: "/v1/webhook", }, httpForward: { host: '0.0.0.0', port: 8002 }, times: { unlimited: true } } ) .then( () => {}, (error) => { console.log(error) } );


4. What error you saw
```{"errno":-3008,"code":"ENOTFOUND","syscall":"getaddrinfo","hostname":"https://proxy_server"}```

**Expected behaviour**
The connection to the server from the client should be successfull, instead the we get the connection refused error

**MockServer Log**

`2023-02-26 06:42:53 5.15.0 INFO using environment variables:

[ SERVER_PORT=1080, MOCKSERVER_LOG_LEVEL=DEBUG ]

and system properties:

[ mockserver.propertyFile=/config/mockserver.properties ]

and command line options:

[

]

2023-02-26 06:42:54 5.15.0 FINE logger level is DEBUG, change using:

2023-02-26 06:44:51 5.15.0 FINE logger level is DEBUG, change using:

2023-02-26 06:46:38 5.15.0 FINE logger level is DEBUG, change using:

alia5gar commented 1 year ago

Here is the snippet from docker compose file

  proxy_server:
    image: mockserver/mockserver
    ports: 
      - 7000:1080
    environment:
      - MOCKSERVER_LOG_LEVEL=DEBUG

  proxy_client:
    build:  
      context: ./node-proxy
    depends_on:
      proxy_server:
        condition: service_started
    environment:
      - SERVER_URL=https://proxy_server
      - SERVER_PORT=1080

The issue seems to persists even if the mockserver is running as a stand alone container, and when the client tries to connect to it via localhost, In this case the issue can be resolved with the --net=host flag in the docker run command

alia5gar commented 1 year ago

I also wrote a small http get call at the start of the client to check the connection to the server But that too gives the same CONNECTION REFUSED Error

alia5gar commented 1 year ago

We can close this issue it turns out the problem was in the client, and not in the server we don't need to pass the complete URL of the server we just need to pass the domain, without the protocol