4

I'm trying to access to my mongo database on docker with adminmongo.

Here's my docker-compose.yml

version: '3'
services:
  mongo:
    image: mongo
    volumes:
      - ~/data:/data/db
    restart: always
    expose:
      - 6016
  adminmongo:
    image: mrvautin/adminmongo
    expose:
      - 1234
    links:
      - mongo:mongo

When i do a docker-compose up everything works fine, adminmongo also return me this : adminmongo_1_544d9a6f954c | adminMongo listening on host: http://localhost:1234

But when i go to localhost:1234 my navigator is telling me this page doesn't exist.

Here's what a docker ps return me :

$ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                 NAMES
9c27d4a89254        mrvautin/adminmongo   "/bin/sh -c 'node ap…"   38 seconds ago      Up 33 seconds       1234/tcp              iris_adminmongo_1_544d9a6f954c
2a7496a8c56a        mongo                 "docker-entrypoint.s…"   40 minutes ago      Up 38 seconds       6016/tcp, 27017/tcp   iris_mongo_1_7f00356a3adc

Anatole Lucet
  • 1,603
  • 3
  • 24
  • 43

3 Answers3

9

I've found 2 issues here:

1st: Exposing a port is not enough. expose is just documentation, you need to publish (bind) a port to the host to be reachable. This is how it's done:

ports:
  - 1234:1234

2nd: you have to configure adminmongo to listen to 0.0.0.0 because by default it starts listening on 127.0.0.1 and this makes it accessible only inside the container itself. From the documentation page you've included in your question, the Configuration section states that this can be done by passing an environment variable:

All above parameters are usable through the environment which makes it very handy to when using adminMongo as a docker container! just run docker run -e HOST=yourchoice -e PORT=1234 ...

Since you are using docker-compose, this is done by the following:

environment:
  - HOST=0.0.0.0

Working example:

version: '3'
services:
  mongo:
    image: mongo
    volumes:
      - ~/data:/data/db
    restart: always
    expose:
      - 6016
  adminmongo:
    image: mrvautin/adminmongo
    ports:
      - 1234:1234
    environment:
      - HOST=0.0.0.0
tgogos
  • 23,218
  • 20
  • 96
  • 128
  • It works ! So if i understand doing an `expose: - 1234:1234` mean that docker expose the container's 1234 local port outside of the container so it can be accessed at localhost:1234. Am i right ? **Edit** : It's ok i understand now. – Anatole Lucet Feb 22 '19 at 11:08
  • 1
    Containers "live" in their isolated network namespace. By using `expose` you just inform the docker daemon that your container is listening on port `1234`. That's just documentation. When you want to bind that port to a host port, you `publish` it. I've written an answer [here](https://stackoverflow.com/questions/22111060/what-is-the-difference-between-expose-and-publish-in-docker/47594352#47594352) which has more details. – tgogos Feb 22 '19 at 11:21
  • 1
    adding environment: - HOST=0.0.0.0 in docker-compose.yml solve my problem – ofir_aghai Nov 04 '19 at 16:23
1

Example of docker-compose works :

version: '3'

services:
  server:
    container_name: docker_api_web_container
    image: docker_api_web
    build: .
    volumes:
      - ./src:/usr/src/node-app/src
      - ./package.json:/usr/src/node-app/package.json
    environment:
     - ENV=DEVELOPMENT
     - PORT=4010
    ports:
      - '9000:4010'
    depends_on:
      - 'mongo'

  mongo:
    container_name: docker_mongo_container
    image: 'mongo'
    ports:
      - '27017:27017'

  adminmongo:
    container_name: docker_adminmongo_container
    image: mrvautin/adminmongo
    links: ['mongo:mongo']
    environment:
      - HOST=0.0.0.0
    ports:
      - '1234:1234'
Anis ABID
  • 11
  • 2
0

You have to expose your service to the outside world like this:

version: '3'
services:
  mongo:
    image: mongo
    volumes:
      - ~/data:/data/db
    restart: always
  adminmongo:
    image: mrvautin/adminmongo
    ports:
      - 1234:1234

Now you can access your adminmongo by http://localhost:1234.

And you don't have to use links here.Since compose creates a network and joins all services in the compose files. You can access other containers with their service names.

Thilak
  • 935
  • 8
  • 12