0

I have a local MongoDB server running on port 27017. I am confused by why there are no errors when running a container using the same port.

>docker run -d --rm --name mongo -p 27017:27017 -v mongodbdata:/data/db mongo
a8f87ca98788597238eea4812e47664b325da25fff40d4dd4900d63aab2f71b8

If I run a Node app on port 3000, I do get an error.

>docker run -d --rm --name mongo-test -p 3000:27017 -v mongodbdata:/data/db mongo
8cd4dc877fb4983d60b7fbe8d3928ec3e20eaac1bf94c9f5bc00eba93831461c
docker: Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:3000 -> 0.0.0.0:0: listen tcp 0.0.0.0:3000: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.

I tried seeing if there is a response when navigating to http://localhost:27017/ (no containers running) on my web browser and got It looks like you are trying to access MongoDB over HTTP on the native driver port.. I confirmed that the mongodb prototocol uses TCP, since it seems that different protocols can share the same port.

I tried running a second container.

>docker run -d --name mongo-2 -p 27018:27017 mongo
>docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                      NAMES
66913df7c967   mongo     "docker-entrypoint.s…"   5 seconds ago   Up 3 seconds   0.0.0.0:27018->27017/tcp   mongo-2
55dbc70b24e0   mongo     "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   0.0.0.0:27017->27017/tcp   mongo-1

Navigating to http://localhost:27018/ shows the same message It looks like you are trying to access MongoDB over HTTP on the native driver port.. However, when I go to http://localhost:27017/, I am not sure whether the response is from the first container or my local machine.

Edit: Here are the logs:

>docker run --name mongo-1 -p 27017:27017 mongo 
{"t":{"$date":"2023-03-10T13:01:32.534+00:00"},"s":"I",  "c":"NETWORK",  "id":4915701, "ctx":"-","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":17},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":17},"outgoing":{"minWireVersion":6,"maxWireVersion":17},"isInternalClient":true}}}
{"t":{"$date":"2023-03-10T13:01:32.540+00:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2023-03-10T13:01:32.548+00:00"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
{"t":{"$date":"2023-03-10T13:01:32.569+00:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationDonorService","namespace":"config.tenantMigrationDonors"}}
{"t":{"$date":"2023-03-10T13:01:32.569+00:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationRecipientService","namespace":"config.tenantMigrationRecipients"}}
{"t":{"$date":"2023-03-10T13:01:32.569+00:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"ShardSplitDonorService","namespace":"config.tenantSplitDonors"}}
{"t":{"$date":"2023-03-10T13:01:32.569+00:00"},"s":"I",  "c":"CONTROL",  "id":5945603, "ctx":"main","msg":"Multi threading initialized"}
{"t":{"$date":"2023-03-10T13:01:32.571+00:00"},"s":"I",  "c":"CONTROL",  "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":1,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"9fc7ea12ba19"}}
{"t":{"$date":"2023-03-10T13:01:32.571+00:00"},"s":"I",  "c":"CONTROL",  "id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"6.0.4","gitVersion":"44ff59461c1353638a71e710f385a566bcd2f547","openSSLVersion":"OpenSSL 3.0.2 15 Mar 2022","modules":[],"allocator":"tcmalloc","environment":{"distmod":"ubuntu2204","distarch":"x86_64","target_arch":"x86_64"}}}}
{"t":{"$date":"2023-03-10T13:01:32.571+00:00"},"s":"I",  "c":"CONTROL",  "id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Ubuntu","version":"22.04"}}}
{"t":{"$date":"2023-03-10T13:01:32.571+00:00"},"s":"I",  "c":"CONTROL",  "id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"net":{"bindIp":"*"}}}}
{"t":{"$date":"2023-03-10T13:01:32.582+00:00"},"s":"I",  "c":"STORAGE",  "id":22297,   "ctx":"initandlisten","msg":"Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem","tags":["startupWarnings"]}
{"t":{"$date":"2023-03-10T13:01:32.584+00:00"},"s":"I",  "c":"STORAGE",  "id":22315,   "ctx":"initandlisten","msg":"Opening WiredTiger","attr":{"config":"create,cache_size=1411M,session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,remove=true,path=journal,compressor=snappy),builtin_extension_config=(zstd=(compression_level=6)),file_manager=(close_idle_time=600,close_scan_interval=10,close_handle_minimum=2000),statistics_log=(wait=0),json_output=(error,message),verbose=[recovery_progress:1,checkpoint_progress:1,compact_progress:1,backup:0,checkpoint:0,compact:0,evict:0,history_store:0,recovery:0,rts:0,salvage:0,tiered:0,timestamp:0,transaction:0,verify:0,log:0],"}}
{"t":{"$date":"2023-03-10T13:01:33.289+00:00"},"s":"I",  "c":"STORAGE",  "id":4795906, "ctx":"initandlisten","msg":"WiredTiger opened","attr":{"durationMillis":704}}
{"t":{"$date":"2023-03-10T13:01:33.289+00:00"},"s":"I",  "c":"RECOVERY", "id":23987,   "ctx":"initandlisten","msg":"WiredTiger recoveryTimestamp","attr":{"recoveryTimestamp":{"$timestamp":{"t":0,"i":0}}}}
{"t":{"$date":"2023-03-10T13:01:33.408+00:00"},"s":"W",  "c":"CONTROL",  "id":22120,   "ctx":"initandlisten","msg":"Access control is not enabled for the database. Read and write access to data and configuration is unrestricted","tags":["startupWarnings"]}
{"t":{"$date":"2023-03-10T13:01:33.410+00:00"},"s":"W",  "c":"CONTROL",  "id":22178,   "ctx":"initandlisten","msg":"/sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'","tags":["startupWarnings"]}
{"t":{"$date":"2023-03-10T13:01:33.411+00:00"},"s":"W",  "c":"CONTROL",  "id":5123300, "ctx":"initandlisten","msg":"vm.max_map_count is too low","attr":{"currentValue":65530,"recommendedMinimum":1677720,"maxConns":838860},"tags":["startupWarnings"]}
{"t":{"$date":"2023-03-10T13:01:33.415+00:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"initandlisten","msg":"createCollection","attr":{"namespace":"admin.system.version","uuidDisposition":"provided","uuid":{"uuid":{"$uuid":"b4bdd383-a3dd-4aee-9f8e-012aaf8139c7"}},"options":{"uuid":{"$uuid":"b4bdd383-a3dd-4aee-9f8e-012aaf8139c7"}}}}
{"t":{"$date":"2023-03-10T13:01:33.495+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"initandlisten","msg":"Index build: done building","attr":{"buildUUID":null,"collectionUUID":{"uuid":{"$uuid":"b4bdd383-a3dd-4aee-9f8e-012aaf8139c7"}},"namespace":"admin.system.version","index":"_id_","ident":"index-1-5248944881332887610","collectionIdent":"collection-0-5248944881332887610","commitTimestamp":null}}
{"t":{"$date":"2023-03-10T13:01:33.508+00:00"},"s":"I",  "c":"REPL",     "id":20459,   "ctx":"initandlisten","msg":"Setting featureCompatibilityVersion","attr":{"newVersion":"6.0"}}
{"t":{"$date":"2023-03-10T13:01:33.509+00:00"},"s":"I",  "c":"REPL",     "id":5853300, "ctx":"initandlisten","msg":"current featureCompatibilityVersion value","attr":{"featureCompatibilityVersion":"6.0","context":"setFCV"}}
{"t":{"$date":"2023-03-10T13:01:33.509+00:00"},"s":"I",  "c":"NETWORK",  "id":4915702, "ctx":"initandlisten","msg":"Updated wire specification","attr":{"oldSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":17},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":17},"outgoing":{"minWireVersion":6,"maxWireVersion":17},"isInternalClient":true},"newSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":17},"incomingInternalClient":{"minWireVersion":17,"maxWireVersion":17},"outgoing":{"minWireVersion":17,"maxWireVersion":17},"isInternalClient":true}}} 
{"t":{"$date":"2023-03-10T13:01:33.515+00:00"},"s":"I",  "c":"NETWORK",  "id":4915702, "ctx":"initandlisten","msg":"Updated wire specification","attr":{"oldSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":17},"incomingInternalClient":{"minWireVersion":17,"maxWireVersion":17},"outgoing":{"minWireVersion":17,"maxWireVersion":17},"isInternalClient":true},"newSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":17},"incomingInternalClient":{"minWireVersion":17,"maxWireVersion":17},"outgoing":{"minWireVersion":17,"maxWireVersion":17},"isInternalClient":true}}}
{"t":{"$date":"2023-03-10T13:01:33.515+00:00"},"s":"I",  "c":"REPL",     "id":5853300, "ctx":"initandlisten","msg":"current featureCompatibilityVersion value","attr":{"featureCompatibilityVersion":"6.0","context":"startup"}}
{"t":{"$date":"2023-03-10T13:01:33.517+00:00"},"s":"I",  "c":"STORAGE",  "id":5071100, "ctx":"initandlisten","msg":"Clearing temp directory"}
{"t":{"$date":"2023-03-10T13:01:33.524+00:00"},"s":"I",  "c":"CONTROL",  "id":20536,   "ctx":"initandlisten","msg":"Flow Control is enabled on this deployment"}
{"t":{"$date":"2023-03-10T13:01:33.571+00:00"},"s":"I",  "c":"FTDC",     "id":20625,   "ctx":"initandlisten","msg":"Initializing full-time diagnostic data capture","attr":{"dataDirectory":"/data/db/diagnostic.data"}}
{"t":{"$date":"2023-03-10T13:01:33.588+00:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"initandlisten","msg":"createCollection","attr":{"namespace":"local.startup_log","uuidDisposition":"generated","uuid":{"uuid":{"$uuid":"5c1f7da1-2e3b-4b1d-8afa-7b8a54b1e9c2"}},"options":{"capped":true,"size":10485760}}}
{"t":{"$date":"2023-03-10T13:01:33.641+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"initandlisten","msg":"Index build: done building","attr":{"buildUUID":null,"collectionUUID":{"uuid":{"$uuid":"5c1f7da1-2e3b-4b1d-8afa-7b8a54b1e9c2"}},"namespace":"local.startup_log","index":"_id_","ident":"index-3-5248944881332887610","collectionIdent":"collection-2-5248944881332887610","commitTimestamp":null}}
{"t":{"$date":"2023-03-10T13:01:33.647+00:00"},"s":"I",  "c":"REPL",     "id":6015317, "ctx":"initandlisten","msg":"Setting new configuration state","attr":{"newState":"ConfigReplicationDisabled","oldState":"ConfigPreStart"}}
{"t":{"$date":"2023-03-10T13:01:33.647+00:00"},"s":"I",  "c":"STORAGE",  "id":22262,   "ctx":"initandlisten","msg":"Timestamp monitor starting"}     
{"t":{"$date":"2023-03-10T13:01:33.675+00:00"},"s":"I",  "c":"NETWORK",  "id":23016,   "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}
{"t":{"$date":"2023-03-10T13:01:33.681+00:00"},"s":"I",  "c":"CONTROL",  "id":20712,   "ctx":"LogicalSessionCacheReap","msg":"Sessions collection is not set up; waiting until next sessions reap interval","attr":{"error":"NamespaceNotFound: config.system.sessions does not exist"}}
{"t":{"$date":"2023-03-10T13:01:33.685+00:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"LogicalSessionCacheRefresh","msg":"createCollection","attr":{"namespace":"config.system.sessions","uuidDisposition":"generated","uuid":{"uuid":{"$uuid":"c477dbd8-3ad6-4095-868d-db78079e710e"}},"options":{}}}
{"t":{"$date":"2023-03-10T13:01:33.782+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"LogicalSessionCacheRefresh","msg":"Index build: done building","attr":{"buildUUID":null,"collectionUUID":{"uuid":{"$uuid":"c477dbd8-3ad6-4095-868d-db78079e710e"}},"namespace":"config.system.sessions","index":"_id_","ident":"index-5-5248944881332887610","collectionIdent":"collection-4-5248944881332887610","commitTimestamp":null}}{"t":{"$date":"2023-03-10T13:01:33.782+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"LogicalSessionCacheRefresh","msg":"Index build: done building","attr":{"buildUUID":null,"collectionUUID":{"uuid":{"$uuid":"c477dbd8-3ad6-4095-868d-db78079e710e"}},"namespace":"config.system.sessions","index":"lsidTTLIndex","ident":"index-6-5248944881332887610","collectionIdent":"collection-4-5248944881332887610","commitTimestamp":null}}

Edit 2: Specifying localhost does cause a conflict

>docker run --name mongo-1 -p 127.0.0.1:27017:27017 mongo
docker: Error response from daemon: Ports are not available: exposing port TCP 127.0.0.1:27017 -> 0.0.0.0:0: listen tcp 127.0.0.1:27017: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.

Edit 3: With MongoDB Compass instead of HTTP in a web browser, connecting to mongodb://localhost:27017 connects with my machine and not the container, while mongodb://localhost:27018 connects to the second container.

BPDev
  • 397
  • 1
  • 9
  • 1
    Can you check how mongo starts up with `docker logs mongo-1` ? – Truefalse Mar 10 '23 at 01:05
  • 1
    Is the host MongoDB server configured to only listen on the localhost interface, 127.0.0.1:27017? Then when Docker binds to 0.0.0.0:27017 there won't be a conflict. In that case I'd expect changing the Docker binding to localhost-only, `-p 127.0.0.1:27017:27017`, to have the same conflict. – David Maze Mar 10 '23 at 02:47
  • @DavidMaze You are right. Is there a reason to not listen to just the localhost interface (Node on port 3000 did create a conflict so it probably listened on 0.0.0.0)? If I am understanding correctly, the message when navigating to `http://localhost:27017/` came from my machine ([0.0.0.0 usually means the default route](https://superuser.com/a/949429/1777303)). Is it possible to target `mongo-1?` – BPDev Mar 10 '23 at 13:27

1 Answers1

1

why there are no errors

Because docker does not listen on the port. By default (with iptables driver) docker adds a rule to DOCKER chain in iptables. https://docs.docker.com/network/iptables/

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    In MongoDBCompass, connecting to `mongodb://localhost:27017` connects with my machine and not the container (`mongodb://localhost:27018` works for the second container). – BPDev Mar 10 '23 at 13:35
  • I am not sure I fully appreciate the distinction; doesn't listening to a port in general require adding a rule to an iptable ([example](https://serverfault.com/a/995591))? It seems to me that "Docker and iptables" is more about additional iptables manipulation to provide network isolation for docker networks. – BPDev Mar 10 '23 at 14:29
  • 1
    `doesn't listening to a port in general require adding a rule to an iptable (example)?` No. `It seems to me that "Docker and iptables" is more about additional iptables manipulation to provide network isolation for docker networks` I do not understand, yes it is, and with `-p27017:27017` adds a forwarding rule. – KamilCuk Mar 10 '23 at 14:53