update 2
I face another error, if I use path as follows, I will get "invalid volume specification" error even change path to "C:/docker-entrypoint-initdb.d/", the init-mongo.sh will not be executed, then I get "UserNotFound: Could not find user "xxxx" for db "admin"
volumes:
- ./init-mongo/:/docker-entrypoint-initdb.d/
- dbdata:/data/db
so I switch to linux container, and all error message are gone.
update
I've solved my problem after disable mongodb service on local host
net stop MongoDB
and use port 27017
mongodb:
....
ports:
- "27017:27017"
.....
but I would like to know why I cant change port 27017 to others? I change the port because if mongodb is running on my computer (using 27017 port), I will get the following error message, thank you.
for mongodb Cannot start service mongodb: failed to create endpoint mongodb on network server_app_network: hnsCall failed in Win32: The process cannot access the file because it is being used by another process. (0x20)
original
I'm using docker windows container on windows 10. After struggling for two days, I still cant deploy my project successfully. my configuration is as follows, thanks for help ..
server.js
const dbUrl = "mongodb://mongodb:27018/";
mongoose
.connect(`${dbUrl}mydb`, { useUnifiedTopology: true, useNewUrlParser: true })
.then(res => {
db = mongoose.connection;
app.listen(port, () => {
console.log(`listening on port ${port}`);
});
})
.catch(error => console.log(error));
docker-compose.yml
version: "3"
services:
express-server:
build:
context: .
dockerfile: Dockerfile
image: express-server
container_name: express-server
restart: unless-stopped
ports:
- "3000:3000"
links:
- mongodb
depends_on:
- mongodb
networks:
- app_network
mongodb:
image: mongo
container_name: mongodb
restart: unless-stopped
ports:
- "27018:27018"
networks:
- app_network
volumes:
dbdata:
networks:
app_network:
driver: nat
after running docker-compose up --build -d
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7fac2380c803 express-server "node server.js" 57 seconds ago Up 43 seconds 0.0.0.0:3000->3000/tcp express-server
9afc21569a1a mongo "mongod --bind_ip_all" About a minute ago Up 57 seconds 27017/tcp, 0.0.0.0:27018->27018/tcp mongodb