Problem with connecting mongo express service in docker
Asked Answered
B

3

10

I have a docker configuration where I started nginx server. Now I have to run mongo and mongo-express to be able to connect to my database. When I put my configuration for those two in docker-compose.yml and try docker compose up, mongo service starts but mongo express shows this error

Could not connect to database using connectionString: mongodb://root:pass@mongodb:27017/" mongo-express_1 | (node:8) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodb:27017] on first connect

Any help is appreciated, here are my configurations.

docker-compose.yml

version: "3"

services:
app:
  build:
    context: .
    dockerfile: Dockerfile
  image: cryptoterminal
  container_name: app
  restart: unless-stopped
  volumes:
    - ./:/var/www

webserver:
  build:
    context: .
    dockerfile: Dockerfile_Nginx
  image: nginx
  container_name: webserver
  restart: unless-stopped
  ports:
    - "8080:80"
  volumes: 
    - ./:/var/www
    - ./config/nginx/:/etc/nginx/conf.d/
  depends_on:
    - app
mongo:
  image: mongo
  environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=pass
mongo-express:
  image: mongo-express
  environment:
    - ME_CONFIG_MONGODB_SERVER=mongodb
    - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
    - ME_CONFIG_MONGODB_ADMINUSERNAME=root
    - ME_CONFIG_MONGODB_ADMINPASSWORD=pass
    - ME_CONFIG_BASICAUTH_USERNAME=admin
    - ME_CONFIG_BASICAUTH_PASSWORD=admin123
  depends_on:
      - mongo
  ports:
    - "8888:8081"

Dockerfile

FROM php:7.4-fpm-alpine

WORKDIR /var/www

RUN apk update && apk add \

build-base \
vim

RUN addgroup -g 1000 -S www && \
adduser -u 1000 -S www -G www

USER www

COPY --chown=www:www . /var/www

EXPOSE 9000

Dockerfile_Nginx

FROM nginx:alpine

COPY ./config/nginx/app.conf /etc/nginx/conf.d/app.conf

COPY . /var/www

.env file

MONGO_DB_URI=mongodb://root:pass@mongodb:27017/crypto-terminal?authSource=admin

app.conf file

server {
    listen 80;
    index index.php index.html;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
}

folder structure

project_folder
    php
        config
            nginx
                app.conf
    .env
    Dockerfile
    Dockerfile_Nginx
    docker-compose.yml
    index.php
Basir answered 22/6, 2021 at 11:51 Comment(0)
N
13

In your docker-compose file, you call the mongo service mongo. That's the name you can address it as on the docker network. In your connection string, you've said

mongodb://root:pass@mongodb:27017/"

It should be

mongo://root:pass@mongo:27017/"

Since the connection string is built using the environment variables in your docker-compose file, the thing to change is

- ME_CONFIG_MONGODB_SERVER=mongodb

to

- ME_CONFIG_MONGODB_SERVER=mongo
Nyeman answered 22/6, 2021 at 12:13 Comment(3)
Yeah that was mistake. I didn't see it. Thank you very muchBasir
Hey @hans I have tried the same but it's not working for me. 1 day before it was working perfectly but now its throwing the same errorThimerosal
So there's a mistake in the official docker image documentation? hub.docker.com/_/mongoGabelle
G
0

None of the solutions did not work for me unfortunately. In the end I was able to solve all the issues with the compose file and environment variables shown below:

./docker-compose.yml

version: '3'

services:
  mongo-express:
    image: mongo-express
    ports:
      - 8081:8081
    env_file:
      - .env
    links:
      - mongo-db
    networks:
      - mongo-compose-network
    restart: always
    depends_on:
      - mongo-db

  mongo-db:
    image: mongo:latest
    container_name: mongo
    env_file:
      - .env
    ports:
      - "27017:27017"
    volumes:
      - mongodb_data_container:/data/db
    networks:
      - mongo-compose-network


networks:
    mongo-compose-network:
      driver: bridge

volumes:
  mongodb_data_container:

./.env

ME_CONFIG_MONGODB_SERVER=mongo-db
ME_CONFIG_BASICAUTH_USERNAME=hacku
ME_CONFIG_BASICAUTH_PASSWORD=Yalcin-1
ME_CONFIG_MONGODB_PORT=27017
ME_CONFIG_MONGODB_ADMINUSERNAME=root
ME_CONFIG_MONGODB_ADMINPASSWORD=rootpassword
MONGO_INITDB_DATABASE=demodb
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=rootpassword

I hope this helps!

Gestation answered 5/6, 2023 at 20:9 Comment(0)
U
-1
 enter code here

# Use root/example as user/password credentials
version: '3.1'

services:`enter code here`

  mongodb:
    image: mongo
    restart: always
    container_name: mongodb
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: root
    volumes:
      - mongodb:/data/db:rw
      - mongoconfig:/data/configdb:rw
    networks:
      - mongodb
      
  mongo-express:
    image: mongo-express
    restart: always
    container_name: mongo-express
    ports:
      - 8889:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: root
      ME_CONFIG_MONGODB_SERVER: mongodb
    depends_on:
      - mongodb
    links:
      - mongodb:mongodb
    networks:
      - mongodb
      
volumes:
  mongodb:
    name: mongodb
    driver: local
    external: false
  mongoconfig:
    name: mongoconfig
    driver: local
    external: false
    
networks:
  mongodb:
    name: mongodb
    driver: bridge
    

# docker exec -it mongodb /bin/bash
# mongo -u root -p
# localhost:8889
Ultramontanism answered 4/6, 2022 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.