pact-foundation / pact-stub-server

Standalone pact stub server
MIT License
75 stars 19 forks source link

Request for docker-compose example with pact-broker #43

Closed shrikeh closed 1 year ago

shrikeh commented 2 years ago

Hi there,

Been trying to get the stub-server working with a local pact-broker for quick prototyping. However, I appear to always get the following:

15:57:54 [INFO] Fetching path '/' from pact broker
15:57:54 [ERROR] There were errors loading the pact files.
15:57:54 [ERROR]   - IoError(Failed to access pact broker path '/' - error sending request for url (http://pact-broker:9292/): error trying to connect: tcp connect error: Connection refused (os error 111). URL: 'http://pact-broker:9292/')
ERROR: 3

Here is my abridged docker-compose.yml file:

---
version: "3.9"

services:
  pact-broker:
    image: pactfoundation/pact-broker:2.81.0.1
    environment:
      PACT_BROKER_DATABASE_HOST:                                   "pact-broker-db"
      PACT_BROKER_DATABASE_NAME:                                   "${PACT_BROKER_DB_NAME:?err}"
      PACT_BROKER_DATABASE_USERNAME:                          "${PACT_BROKER_DB_USERNAME:?err}"
      PACT_BROKER_DATABASE_PASSWORD:                         "${PACT_BROKER_DB_PASSWORD:?err}"
      PACT_BROKER_DATABASE_PORT:                                    "${PACT_BROKER_DB_PORT:-5432}"
      PACT_BROKER_DATABASE_CONNECT_MAX_RETRIES: "10"
      PACT_BROKER_PORT:                                                        "${PACT_BROKER_PORT:-9292}"
    depends_on:
      - pact-broker-db
    expose:
      - "${PACT_BROKER_PORT:-9292}"
    ports:
      - "9010:${PACT_BROKER_PORT:-9292}"
  pact-broker-db:
    image: postgres:13.4-alpine3.14
    restart: always
    healthcheck:
      test: "psql postgres --command 'select 1' -U ${PACT_BROKER_DB_USERNAME:?err}"
    volumes:
      - pact-postgres:/var/lib/postgresql/data
    environment:
      POSTGRES_DB:       "${PACT_BROKER_DB_NAME:?err}"
      POSTGRES_USER:     "${PACT_BROKER_DB_USERNAME:?err}"
      POSTGRES_PASSWORD: "${PACT_BROKER_DB_PASSWORD:?err}"
    expose:
      - "${PACT_BROKER_DB_PORT:-5432}"
  pact-stub:
    image: pactfoundation/pact-stub-server:v0.4.4
    command: "--port ${PACT_STUB_PORT:?err}"
    environment:
      PACT_BROKER_BASE_URL: "http://pact-broker:${PACT_BROKER_PORT:-9292}/"
    volumes:
      # Not used when pact-broker is running
      - ./tests/pacts:/pacts:ro
    depends_on:
      - pact-broker
    ports:
      - "8082:${PACT_STUB_PORT:?err}"
volumes:
  pact-postgres:

For the purposes of aiding, pact-broker starts fine, and indeed the following curl to the assigned ports also works fine:

$> curl http://localhost:8080

{"_links":{"self":{"href":"http://localhost:8080","title":"Index","templated":false},"pb:publish-pact":{"href":"http://localhost:8080/pacts/provider/{provider}/consumer/{consumer}/version/{consumerApplicationVersion}","title":"Publish a pact","templated":true},"pb:publish-contracts":{"href":"http://localhost:8080/contracts/publish","title":"Publish contracts","templated":false},"pb:latest-pact-versions":{"href":"http://localhost:8080/pacts/latest","title":"Latest pact versions","templated":false},"pb:tagged-pact-versions":{"href":"http://localhost:8080/pacts/provider/{provider}/consumer/{consumer}/tag/{tag}","title":"All versions of a pact for a given consumer, provider and consumer version tag","templated":false},"pb:pacticipants":{"href":"http://localhost:8080/pacticipants","title":"Pacticipants","templated":false},"pb:pacticipant":{"href":"http://localhost:8080/pacticipants/{pacticipant}","title":"Fetch pacticipant by name","templated":true},"pb:latest-provider-pacts":{"href":"http://localhost:8080/pacts/provider/{provider}/latest","title":"Latest pacts by provider","templated":true},"pb:latest-provider-pacts-with-tag":{"href":"http://localhost:8080/pacts/provider/{provider}/latest/{tag}","title":"Latest pacts for provider with the specified tag","templated":true},"pb:provider-pacts-with-tag":{"href":"http://localhost:8080/pacts/provider/{provider}/tag/{tag}","title":"All pact versions for the provider with the specified consumer version tag","templated":true},"pb:provider-pacts":{"href":"http://localhost:8080/pacts/provider/{provider}","title":"All pact versions for the specified provider","templated":true},"pb:latest-version":{"href":"http://localhost:8080/pacticipants/{pacticipant}/latest-version","title":"Latest pacticipant version","templated":true},"pb:latest-tagged-version":{"href":"http://localhost:8080/pacticipants/{pacticipant}/latest-version/{tag}","title":"Latest pacticipant version with the specified tag","templated":true},"pb:webhooks":{"href":"http://localhost:8080/webhooks","title":"Webhooks","templated":false},"pb:webhook":{"href":"http://localhost:8080/webhooks/{uuid}","title":"Webhook","templated":true},"pb:integrations":{"href":"http://localhost:8080/integrations","title":"Integrations","templated":false},"pb:pacticipant-version-tag":{"href":"http://localhost:8080/pacticipants/{pacticipant}/versions/{version}/tags/{tag}","title":"Get, create or delete a tag for a pacticipant version","templated":true},"pb:pacticipant-version":{"href":"http://localhost:8080/pacticipants/{pacticipant}/versions/{version}","title":"Get, create or delete a pacticipant version","templated":true},"pb:metrics":{"href":"http://localhost:8080/metrics","title":"Get Pact Broker metrics"},"pb:can-i-deploy-pacticipant-version-to-tag":{"href":"http://localhost:8080/can-i-deploy?pacticipant={pacticipant}&version={version}&to={tag}","title":"Determine if an application can be safely deployed to an environment identified by the given tag","templated":true},"pb:provider-pacts-for-verification":{"href":"http://localhost:8080/pacts/provider/{provider}/for-verification","title":"Pact versions to be verified for the specified provider","templated":true},"beta:provider-pacts-for-verification":{"name":"beta","href":"http://localhost:8080/pacts/provider/{provider}/for-verification","title":"DEPRECATED - please use pb:provider-pacts-for-verification","templated":true},"curies":[{"name":"pb","href":"http://localhost:8080/doc/{rel}?context=index","templated":true},{"name":"beta","href":"http://localhost:8080/doc/{rel}?context=index","templated":true}],"pb:environments":{"title":"Environments","href":"http://localhost:8080/environments","templated":false},"pb:environment":{"title":"Environment","href":"http://localhost:8080/environments/{uuid}","templated":true}}}%     
uglyog commented 2 years ago

It could be this:

ports:
      - "9010:${PACT_BROKER_PORT:-9292}"

Is that not mapping the internal port (defined by ${PACT_BROKER_PORT:-9292}) to 9010

rholshausen commented 1 year ago

Here is a working docker compose example:

version: "3.9"

services:
  pact-broker:
    image: pactfoundation/pact-broker:2.81.0.1
    environment:
      PACT_BROKER_DATABASE_URL:                              "postgres://postgres:password@pact-broker-db/postgres"
      PACT_BROKER_PORT:                                      "9292"
      PACT_BROKER_BASE_URL: 'https://localhost http://localhost http://localhost:9292 http://pact-broker:9292'
    depends_on:
      - pact-broker-db
    expose:
      - "9292"
    ports:
      - "9292:9292"
  pact-broker-db:
    image: postgres:13.4-alpine3.14
    restart: always
    healthcheck:
      test: "psql postgres --command 'select 1' -U postgres"
    volumes:
      - pact-postgres:/var/lib/postgresql/data
    environment:
      POSTGRES_DB:       postgres
      POSTGRES_USER:     postgres
      POSTGRES_PASSWORD: password
    expose:
      - "5432"
  pact-stub:
    image: pactfoundation/pact-stub-server:v0.4.4
    entrypoint: "/bin/bash -c 'sleep 5 && ./pact-stub-server --port 8082'"
    environment:
      PACT_BROKER_BASE_URL: "http://pact-broker:9292/"
    volumes:
      # Not used when pact-broker is running
      - ./tests/pacts:/pacts:ro
    depends_on:
      - pact-broker
    ports:
      - "8082:8082"
volumes:
  pact-postgres: