Search code examples
dockergowebsocketdocker-composedocker-network

Go app cannot connect over websocket inside docker-compose


So I have a very simple app written in Go, that needs to connect to the database over a websocket. This works fine when I run the database inside a container and expose the required ports via docker run -d -p 8182:8182 tinkerpop/gremlin-server, with the app locally pointing at localhost:8182.

However I now want to run both inside containers via compose for CI, and my app cannot find the database. I've tried using docker hostnames, IP addresses, and listening on 0.0.0.0 to no avail. Currently my compose looks like this:

version: '3'

services:
  db:
    image: tinkerpop/gremlin-server
    hostname: gremlin
    ports:
      - 8182:8182
  server:
    build: .
    environment:
      ORBITAL_DB_ADDRESS: "ws://gremlin:8182"
    ports:
    - 5000:5000
    depends_on:
      - db

However I seem to only get the following message from my app container: dial tcp: lookup gremlin on 127.0.0.11:53: no such host.

This is despite my app set to ListenAndServe on 0.0.0.0:

err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", conf.Service.Port), nil)

Any help would be much appreciated. Thanks.


Solution

  • Managed to work this out. Turns out the Dockerfile for the 3rd party container was already set to EXPOSE 8182, and I was additionally mapping the host:container port in compose with:

    ports:
      - 8182:8182
    

    For some reason this was causing a timeout when trying to reach ws://db:8182 and when I removed the port mapping in compose this resolved itself and became available.

    My final docker-compose.yml looks like:

    version: '3'
    
    services:
      db:
        image: tinkerpop/gremlin-server
      server:
        build: app
        environment:
          ORBITAL_DB_ADDRESS: ws://db:8182
        ports:
        - 5000:5000
    

    I can't explain why exactly this happens but would be interested if anyone else can explain in more technical detail what was going on here as it seems the EXPOSE in the dockerfile, and ports in compose were at odds with each other.