0

I am deploying a simple GraphQL API on a server. I have port restrictions on the server. My app runs on 3000 but I wish to map it to 8117.

I have a db.json file in the codebase that maps the MongoDB credentials and URL to my app.js:

{
  "env": {
    "MONGO_USER" : "user1",
    "MONGO_PASSWORD" : "secretpassword",
    "MONGO_DATABASE" : "IP_address_of_server",
    "MONGO_PORT": "37017"
  }
}

Upon building the Image:

docker run --name=graphql1 -p 8117:3000 my_graphql_image:1.0

I am able to expose the port on the server however upon:

docker logs -f grapqhl1

I get the following error:

{ MongooseServerSelectionError: connection timed out
    at new MongooseServerSelectionError (/usr/src/app/node_modules/mongoose/lib/error/serverSelection.js:22:11)
    at NativeConnection.Connection.openUri (/usr/src/app/node_modules/mongoose/lib/connection.js:808:32)
    at Mongoose.connect (/usr/src/app/node_modules/mongoose/lib/index.js:333:15)
    at Object.<anonymous> (/usr/src/app/app.js:56:10)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
  message: 'connection timed out',
  name: 'MongooseServerSelectionError',
  reason:
   TopologyDescription {
     type: 'Single',
     setName: null,
     maxSetVersion: null,
     maxElectionId: null,
     servers: Map { '<IP_Address>:37017' => [ServerDescription] },
     stale: false,
     compatible: true,
     compatibilityError: null,
     logicalSessionTimeoutMinutes: null,
     heartbeatFrequencyMS: 10000,
     localThresholdMS: 15,
     commonWireVersion: null },
  [Symbol(mongoErrorContextSymbol)]: {} }

The Mongoose client does not go into timeout when I run the container with --net=host

docker run --name=grapqhl1 -d --net=host my_graphql_image:1.0

However, since --net=host discards Ports when using the host network mode. I am unable to reach the App via port 8117 nor the port 3000. docker ps -a also does not show any ports to within or outside the App.

What is the optimal way to reach the API at the desired port and not run into a Connection Timeout?

Shan-Desai
  • 3,101
  • 3
  • 46
  • 89
  • First of all: I take it, 37017 is the wanted port? – BenSower Mar 26 '20 at 09:25
  • Yes. It is the port that is made available for the MongoDB on the remote host. It works when I run the code locally – Shan-Desai Mar 26 '20 at 09:28
  • 1
    Ok, good. Potentially, your bridge network isn't properly configured/broken? Please run your container in the mode where it can't connect to the DB, enter the graphql container using 'docker exec -it yourgraphqlcontainer sh' and try to ping any web-address, to check if you are allowed to do so from the bridge network. – BenSower Mar 26 '20 at 09:36
  • 1
    If there are problems in the connection, create a separate network "docker network create test", connect your container to it "docker network connect test yourgraphqlcontainer", restart it and check if the connection to the mongodb can be made – BenSower Mar 26 '20 at 09:37
  • If possible, restart your docker daemon and/or rebuild the bridge: https://stackoverflow.com/questions/20430371/my-docker-container-has-no-internet – BenSower Mar 26 '20 at 09:39
  • The container wasn't able to ping outside. I created a network `docker create network graphql` and ran the code `docker run --net=grapqhl ...` and it still times out. – Shan-Desai Mar 26 '20 at 09:53
  • I am assuming this might only be because of the Server Firewall – Shan-Desai Mar 26 '20 at 09:57
  • 1
    Yes, this might be worth checking out. Also, have a look at the daemon.json to see if there is any misconfigured gateway. – BenSower Mar 26 '20 at 10:28

0 Answers0