azerELweed
azerELweed

Reputation: 115

Mongodb on docker container not responding

Intro

Hey, So I have this docker compose file that I use for testing and developpement for an app...

version: "2.2"

services:
  elasticsearch:
    image: elasticsearch:7.8.1
    ports:
      - "9200:9200"
      - "9300:9300"
    environment:
      - "discovery.type=single-node"
      - "ELASTIC_PASSWORD=pass"
      - "xpack.security.enabled=true"
      - "bootstrap.memory_lock=true"
  mongodb:
    build: ./docker_mages/mongodb/
    ports:
      - 27031:27017
and the docker file for the mongodb image is as follows
FROM mongo:4.0.3

#Identity

ENV MONGO_INITDB_ROOT_USERNAME name
ENV MONGO_INITDB_ROOT_PASSWORD pass
ENV MONGO_INITDB_DATABASE database 
#entrypoint
#I have tryied the EXPOSE CMD but it's not working
#EXPOSE 27017
COPY mongo-init.js /docker-entrypoint-initdb.d/

Problem

now as far as I understand docker and containers I should be able to reach my containers with a simple curl command. And when I curl the elasticsearch server I have a response that prooves that my container is reachble , but when i curl the mongodb database it won't work at all as if there is nothing that is binded to the 27017 port
# Curl ouput
$ curl localhost:9200
{"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}}],"type":"security_Any heexception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}},"status":401}⏎      

$ curl localhost:27017
curl: (7) Failed to connect to 127.0.0.1 port 27017 after 0 ms: Connection refused
PS : The mongo-init.js file doesn't have an effect on the mongo container, as I accesed the container and made sure the mongo bash works fine. here's some output
⫸  sudo docker exec -it 6519021e5fad bash
[sudo] password for ***: 
root@6519021e5fad:/# mongo
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("8bcdd52c-5dc0-4228-bb60-141d2a66d27f") }
MongoDB server version: 4.0.3
> 
bye

Please help

Upvotes: 2

Views: 2227

Answers (1)

TheQueenIsDead
TheQueenIsDead

Reputation: 955

Your compose file shows the following setup for mongo

  mongodb:
    build: ./docker_mages/mongodb/
    ports:
      - 27031:27017

The port spec is such that it maps from <host>:<container>.

So what you're saying with the above is that port 27031 on your local host 127.0.0.1:27031 should map to the container's port 27017.

You can either curl 127.0.0.1:27031 or change the port spec to be - "27017:27017", meaning that your host's 27017 will map to the containers 27017 port.

Edit: As mentioned in the comments below, wrap your ports in quotes for safety: Quotes on docker-compose.yml ports make any difference?

Upvotes: 1

Related Questions