docker 为什么在端口27017(被MongoDB占用)上运行容器时没有冲突?

qrjkbowd  于 2023-03-17  发布在  Docker
关注(0)|答案(1)|浏览(291)

我有一个本地MongoDB服务器运行在端口27017上,我很困惑为什么使用同一端口运行容器时没有错误。

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

如果我在端口3000上运行Node应用程序,我会收到一个错误。

>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.

我尝试查看在我的Web浏览器上导航到http://localhost:27017/(没有容器运行)时是否有响应,并得到了It looks like you are trying to access MongoDB over HTTP on the native driver port.。我确认了mongodb prototocol uses TCP,因为不同的协议似乎可以share相同的端口。
我试着打开第二个集装箱。

>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

导航到http://localhost:27018/显示相同的消息It looks like you are trying to access MongoDB over HTTP on the native driver port.,但是,当我转到http://localhost:27017/时,我不确定响应是来自第一个容器还是我的本地计算机。
编辑:以下是日志:

>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}}

编辑2:指定localhost确实会导致冲突

>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.

编辑3:在Web浏览器中使用MongoDB Compass而不是HTTP,连接到mongodb://localhost:27017连接到我的机器而不是容器,而mongodb://localhost:27018连接到第二个容器。

nkoocmlb

nkoocmlb1#

为什么没有错误
因为Docker不监听端口。默认情况下(使用iptables驱动程序),Docker会在iptables. https://docs.docker.com/network/iptables/中向DOCKER链添加一条规则

相关问题