skip to Main Content

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

2

Answers


  1. 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
    
    Login or Signup to reply.
  2.  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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search