Mocking 3rd party dependency with mockserver port issues

638 Views Asked by At

We have an app with a dependency on a 3rd party API which we need to mock out in our build process. We want to use Mock Server for this as it gives us a good degree of control over the responses.

The API we're using provides no way to override the URL so we need to mock out the dependency at DNS level forcing domain.com to resolve to our container. Fortunately we can do this pretty easily with docker as the following docker-compose.yml file shows.

version: "2"
services:
  mockserver:
    image: mockserver/mockserver
    ports:
        - 80:1080
  ping:
    image: busybox
    links:
      - mockserver:domain.com
      - mockserver:api.domain.com
    command: ping domain.com
    depends_on:
      - mockserver

If we run docker-compose run ping we can see a response from the container for requests to domain.com

Creating docker-test_ping_run ... done
PING domain.com (172.22.0.2): 56 data bytes
64 bytes from 172.22.0.2: seq=0 ttl=64 time=0.081 ms
64 bytes from 172.22.0.2: seq=1 ttl=64 time=0.078 ms
64 bytes from 172.22.0.2: seq=2 ttl=64 time=0.125 ms

We can also visit the Web UI for mock server as so http://localhost/mockserver/dashboard

However, if I try to hit the mockserver directly from inside the ping container it doesn't honour the port forwarding.

❯ docker-compose exec ping sh
/ # wget domain.com
Connecting to domain.com (172.22.0.2:80)
wget: can't connect to remote host (172.22.0.2): Connection refused

If I add the port it's fine, though, which suggests the port forwarding isn't honoured inside the container?

/ # wget domain.com:1080
Connecting to domain.com:1080 (172.23.0.2:1080)
saving to 'index.html'

What's missing here? We need to be able to make domain.com requests run without a port otherwise we can't swap our dependency out.

thanks.

1

There are 1 best solutions below

0
On

Turns out I was just missing the serverPort setting, the 80:80 forwarding keeps the web interface working.

version: "2"
services:
    mockserver:
        image: mockserver/mockserver
        command: -serverPort 80
        ports:
            - 80:80  
    ping:
        image: busybox
        links:
            - mockserver:domain.com
            - mockserver:api.domain.com
        command: ping domain.com
        depends_on:
            - mockserver