Unable to create a mongodb docker container, Error starting userland proxy: listen tcp4 0.0.0.0:27017: bind: address already in use

  containers, docker, mongodb, postgresql, ubuntu-20.10

I am tyring to create a mongodb container with following command:

docker run -dit --rm -p 27017:27017 --name mongo-docker mongo:latest

I am getting this error when I’m running the command:

docker:  (Image hash)Error response from daemon: driver failed programming external connectivity on endpoint mongo-docker: 
Error starting userland proxy: listen tcp4 0.0.0.0:27017: bind: address already in use

I’ve tried running ss command to find out what’s running on PORT 27017

[I] ➜ ss -nlp | grep 27017
u_str LISTEN 0      4096                  /tmp/mongodb-27017.sock 457102                 * 0
tcp   LISTEN 0      4096                                 127.0.0.1:27017            0.0.0.0:*

I’ve tried changing the host PORT from 27017 to 2717 like this docker run -dit --rm -p 2717:27017 --name mongo-docker mongo:latest, but when I’m trying to connect to the this container with dockerized mongoshell I’m getting the following error

MongoDB shell version v4.4.4
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :

When I stop the mongodb on my host then I am able to create the container. But do I have to stop these services everytime when I’m, tyring to run the container , or is there any other way to do this ?

This behaviour is same with Postgres and Mysql, I have both these databases running on my host system and when I’m trying to create a docker container with the default PORTS for that specific DB I’m getting the same error

[I] ➜ docker run -dit -p 5432:5432 --name psql-container postgres

docker: Error response from daemon: driver failed programming external connectivity on endpoint psql-container (Image hash): 
Error starting userland proxy: listen tcp4 0.0.0.0:5432: bind: address already in use.

Processes runnning on PORT 5432

u_str LISTEN 0      244           /var/run/postgresql/.s.PGSQL.5432 38530                  * 0
tcp   LISTEN 0      244                                    127.0.0.1:5432             0.0.0.0:*

My host specs:

 OS: Ubuntu 20.10 x86_64

Client: Docker Engine - Community
 Version:           20.10.5
 API version:       1.41

Server: Docker Engine - Community
 Engine:
  Version:          20.10.5
  API version:      1.41 (minimum version 1.12)

MongoDB shell version v4.4.4
MongoDB server version: 4.4.4

Source: Docker Questions

LEAVE A COMMENT