Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port Conflicts when create replication on single server #437

Closed
Just4test opened this issue Nov 26, 2020 · 2 comments · Fixed by #443
Closed

Port Conflicts when create replication on single server #437

Just4test opened this issue Nov 26, 2020 · 2 comments · Fixed by #443
Labels
question Usability question, not directly related to an error with the image

Comments

@Just4test
Copy link

Here's part of my docker-compose.yaml:

mongo1:
  image: mongo:4.2
  network_mode: host
  command:  --port 27018 --bind_ip_all --replSet rs0
  environment: 
    MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
    MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}

mongo2:
  image: mongo:4.2
  network_mode: host
  command:  --port 27019 --bind_ip_all --replSet rs0
  environment: 
    MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
    MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}

mongo2:
  image: mongo:4.2
  network_mode: host
  command:  --port 27020 --bind_ip_all --replSet rs0
  environment: 
    MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
    MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}

When I run docker-compose up, some times sevel container boot crash.

2020-11-26T07:59:10.584+0000 I  CONTROL  [initandlisten] options: { net: { bindIp: "127.0.0.1", port: 27017, tls: { mode: "disabled" } }, processManagement: { fork: true, pidFilePath: "/tmp/docker-entrypoint-temp-mongod.pid" }, security: { keyFile: "/mongodb.key" }, systemLog: { destination: "file", logAppend: true, path: "/proc/1/fd/1", quiet: true } }
2020-11-26T07:59:10.585+0000 E  STORAGE  [initandlisten] Failed to set up listener: SocketException: Address already in use
......
2020-11-26T07:59:10.585+0000 I  CONTROL  [initandlisten] shutting down with code:48

That's because container will create a mongod init process which always using port 27017. The init process will last for about one second. If sevel mongo container boot at the same time, Only one init process can bind 27017, other will crash.

@wglambert wglambert added the question Usability question, not directly related to an error with the image label Nov 30, 2020
@wglambert
Copy link

If you're using the host network mode then you'll have to stagger the initializations or pre-initialize them as the entrypoint will temporarily be listening on 27017

if [ -n "$shouldPerformInitdb" ]; then
mongodHackedArgs=( "$@" )
if _parse_config "$@"; then
_mongod_hack_ensure_arg_val --config "$tempConfigFile" "${mongodHackedArgs[@]}"
fi
_mongod_hack_ensure_arg_val --bind_ip 127.0.0.1 "${mongodHackedArgs[@]}"
_mongod_hack_ensure_arg_val --port 27017 "${mongodHackedArgs[@]}"
_mongod_hack_ensure_no_arg --bind_ip_all "${mongodHackedArgs[@]}"

There was also a lengthy discussion on replicasets here #246

@tianon
Copy link
Member

tianon commented Dec 4, 2020

There's a potential solution to this over in #443. 👀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Usability question, not directly related to an error with the image
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants