Trying to run two mongo containers on different ports

So I'm trying to create two mongo containers on different ports.

The first container setup works fine.

This is my docker-compose.yml file:

--- version: '3.1' services: document_store: container_name: one.mongo build: mongo restart: always ports: - 27017:27017 environment: MONGO_INITDB_ROOT_USERNAME:init MONGO_INITDB_ROOT_PASSWORD:init 

For build, I have the following files in mongodir:

Dockerfile

FROM mongo:4.2.3 ADD ./setup.js /docker-entrypoint-initdb.d/ EXPOSE 27017 ENV MONGO_INITDB_ROOT_USERNAME='init' ENV MONGO_INITDB_ROOT_PASSWORD='init' 

setup.js

connection = new Mongo(); db = connection.getDB("dev-one"); db.createUser({ user: "devuser", pwd: "devpassword", roles:["dbOwner"] }); db = connection.getDB("dev-one-test"); db.createUser({ user: "testuser", pwd: "testpassword", roles:["dbOwner"] }); 

As you can see, I have used the default ports 27017 everywhere. And this works totally fine.

When I try to repeat the same process by specifying a different container name and ports**(27027)** as follows, it doesn't work.

docker-compose.yml

--- version: '3.1' services: document_store: container_name: two.mongo build: mongo restart: always ports: - 27027:27027 environment: MONGO_INITDB_ROOT_USERNAME:init MONGO_INITDB_ROOT_PASSWORD:init 

DockerFile

FROM mongo:4.2.3 ADD ./setup.js /docker-entrypoint-initdb.d/ EXPOSE 27027 ENV MONGO_INITDB_ROOT_USERNAME='init' ENV MONGO_INITDB_ROOT_PASSWORD='init' 

setup.js

connection = new Mongo(); db = connection.getDB("dev-two"); db.createUser({ user: "devuser", pwd: "devpassword", roles:["dbOwner"] }); db = connection.getDB("dev-two-test"); db.createUser({ user: "testuser", pwd: "testpassword", roles:["dbOwner"] }); 

After docker-compose up, it still shows as

[listener] Listening on 0.0.0.0 [listener] waiting for connections on port 27017 

What am I missing here?

submitted by /u/dack_dock_bock
[link] [comments]
Source: Reddit