I've been on this issue for hours now and I really don't know what else to do (and all the research I did didn't provide any solution unfortunately). So I'm asking if anyone can think of an answer why this is not working:
I run a docker-compose file with mongo and mongo-express images. Using the default code provided here didnt work ( mongo-express stopped with exit code 1) so I tried tons of configurations leaving me with the docker-compose file below ( that actually works to the degree that mongo-express gets started and shows
Databases: admin
Server Status:
Turn on admin in config.js to view server stats!
displayed in the GUI). But from there nothing works. The console shows the problem:
mongoviewer_1 | Database connected
database_1 | 2018-08-22T09:15:27.743+0000 I ACCESS [conn2] SASL SCRAM-SHA-1 authentication failed for admin on admin from client; UserNotFound: Could not find user admin#admin
mongoviewer_1 | unable to list databases
mongoviewer_1 | { MongoError: command listDatabases requires authentication
mongoviewer_1 | at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:31:11)
mongoviewer_1 | at /node_modules/mongodb-core/lib/connection/pool.js:483:72
mongoviewer_1 | at authenticateStragglers (/node_modules/mongodb-core/lib/connection/pool.js:429:16)
mongoviewer_1 | at Connection.messageHandler (/node_modules/mongodb-core/lib/connection/pool.js:463:5)
mongoviewer_1 | at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:319:22)
mongoviewer_1 | at emitOne (events.js:116:13)
mongoviewer_1 | at Socket.emit (events.js:211:7)
mongoviewer_1 | at addChunk (_stream_readable.js:263:12)
mongoviewer_1 | at readableAddChunk (_stream_readable.js:250:11)
mongoviewer_1 | at Socket.Readable.push (_stream_readable.js:208:10)
mongoviewer_1 | name: 'MongoError',
mongoviewer_1 | message: 'command listDatabases requires authentication',
mongoviewer_1 | ok: 0,
mongoviewer_1 | errmsg: 'command listDatabases requires authentication',
mongoviewer_1 | code: 13,
mongoviewer_1 | codeName: 'Unauthorized' }
The docker-compose looks like follows:
database:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: dockerdb
ports:
- "27017:27017"
mongoviewer:
image: mongo-express
environment:
ME_CONFIG_MONGODB_SERVER: database
ME_CONFIG_BASICAUTH_USERNAME: ""
ME_CONFIG_MONGODB_AUTH_DATABASE: admin
ME_CONFIG_MONGODB_AUTH_USERNAME: admin
ME_CONFIG_MONGODB_AUTH_PASSWORD: password
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: password
ports:
- "8081:8081"
links:
- database
depends_on:
- database
That's the current version I was trying and I tried so many other configurations but nothing fixed the auth problem.
If anyone has an answer or at least an idea on what to do I'd be extremely thankfull!
Essentially there are two states when you are configuring your docker-compose.yaml. I am no expert but this solution resolved this issue for me.
No Authentication Required
By default when you're running mongo instance, it requires authentication to access the databases since you are specifying both MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD.
You can access the databases in mongo by removing both of the keys.
Authentication Required
According to mongo-express documentation, by default the ME_CONFIG_MONGODB_ENABLE_ADMIN key is false making your mongo-express application will not try for any authentication and directly connect to your mongo instance (thus producing the Authentication failed error).
You will need to enable the ME_CONFIG_MONGODB_ENABLE_ADMIN key by changing it to true. Remember to stringify the "true" in your docker-compose.yaml.
Ensure the ME_CONFIG_MONGODB_ADMINUSERNAME and ME_CONFIG_MONGODB_ADMINPASSWORD keys match MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD keys respectively.
Based on the info on the docker file, if MongoDB runs on a container named "database", it can be accessed from mongo-express using:
docker run -it --rm \
--network web_default \
--name mongo-express \
-p 8081:8081 \
-e ME_CONFIG_MONGODB_SERVER=database \
-e ME_CONFIG_MONGODB_ADMINUSERNAME= admin \
-e ME_CONFIG_MONGODB_ADMINPASSWORD=password \
mongo-express
For reference check the mongo-express Docu on dockerhub
Note: that in case you want to access a specific database with a user credentials:
docker run -it --rm \
--network web_default \
--name mongo-express \
-p 8081:8081 \
-e ME_CONFIG_MONGODB_SERVER= database \
-e ME_CONFIG_MONGODB_AUTH_DATABASE= dockerdb \
-e ME_CONFIG_MONGODB_ENABLE_ADMIN=false \
-e ME_CONFIG_MONGODB_AUTH_USERNAME=admin \
-e ME_CONFIG_MONGODB_AUTH_PASSWORD=password \
mongo-express
Related
I am trying to setup a docker-compose setup to run MongoDB and Mongo-Express on a Debian virtual machine.
I've read up on problems with volumes, same docker networks, setting ME_CONFIG_MONGODB_SERVER to the Mongodb container name and having problems with UFW.
This is my current docker-compose.yml
version: '3.7'
services:
mongodb:
image: mongo
container_name: mongodb
restart: always
ports:
- '27017:27017'
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_INITDB_DATABASE=default
networks:
- mongo-compose-network
volumes:
- /home/paul/mongodbdocker/data:/data/db
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
depends_on:
- mongodb
networks:
- mongo-compose-network
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_SERVER=mongodb
- ME_CONFIG_BASICAUTH_USERNAME=admin
- ME_CONFIG_BASICAUTH_PASSWORD=password
ports:
- '8081:8081'
volumes:
- /home/paul/mongodbdocker/data:/data/db
networks:
mongo-compose-network:
driver: bridge
I have disabled the UFW firewall with sudo ufw disable and also edited the ufw configuration as described in here What is the best practice of docker + ufw under Ubuntu from Feng.
The MongoDB starts up, but sadly mongo-express fails on first connection with this error:
mongo-express | Welcome to mongo-express
mongo-express | ------------------------
mongo-express |
mongo-express |
mongo-express | (node:8) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
mongo-express | Could not connect to database using connectionString: mongodb://admin:password#mongodb:27017/"
mongo-express | (node:8) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodb:27017] on first connect [MongoNetworkTimeoutError: connection timed out
mongo-express | at connectionFailureError (/node_modules/mongodb/lib/core/connection/connect.js:362:14)
mongo-express | at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:16)
mongo-express | at Object.onceWrapper (events.js:420:28)
mongo-express | at Socket.emit (events.js:314:20)
mongo-express | at Socket._onTimeout (net.js:483:8)
mongo-express | at listOnTimeout (internal/timers.js:554:17)
mongo-express | at processTimers (internal/timers.js:497:7)]
mongo-express | at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
mongo-express | at Pool.emit (events.js:314:20)
mongo-express | at /node_modules/mongodb/lib/core/connection/pool.js:564:14
mongo-express | at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
mongo-express | at /node_modules/mongodb/lib/core/connection/connect.js:32:7
mongo-express | at callback (/node_modules/mongodb/lib/core/connection/connect.js:300:5)
mongo-express | at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:7)
mongo-express | at Object.onceWrapper (events.js:420:28)
mongo-express | at Socket.emit (events.js:314:20)
mongo-express | at Socket._onTimeout (net.js:483:8)
mongo-express | at listOnTimeout (internal/timers.js:554:17)
mongo-express | at processTimers (internal/timers.js:497:7)
mongo-express | (node:8) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
mongo-express | (node:8) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I'm trying to learn how to use Kong for my API server, but met the error:
kong_1 | nginx: [error] init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:388: [PostgreSQL error] failed to retrieve PostgreSQL server_version_num: host or service not provided, or not known
My docker-compose.yaml as below:
version: "3"
networks:
kong-net:
driver: bridge
services:
# Create a service named db.
kong-postgres:
# Use the Docker Image postgres. This will pull the newest release.
image: "postgres"
# Give the container a name. You can changes to something else.
container_name: "kong-postgres"
# Setup the username, password, and database name. You can changes these values.
environment:
- POSTGRES_USER=kong
- POSTGRES_PASSWORD=kong
- POSTGRES_DB=kong
# Maps port 54320 (localhost) to port 5432 on the container. You can change the ports to fix your needs.
ports:
- "5432:5432"
restart: on-failure
# Set a volume some that database is not lost after shutting down the container.
# I used the name postgres-data but you can changed it to something else.
volumes:
- ./postgres-data:/var/lib/postgresql/data
kong:
image: "kong:latest"
command: "kong migrations bootstrap"
depends_on:
- kong-postgres
environment:
KONG_ADMIN_LISTEN: '0.0.0.0:8001,0.0.0.0:8444 ssl'
KONG_DATABASE: postgres
KONG_PG_HOST: kong-postgres
KONG_PG_DATABASE: kong
KONG_PG_PASSWORD: kong
KONG_PG_USER: kong
networks:
- kong-net
ports:
- "8000:8000/tcp"
- "8001:8001/tcp"
- "8443:8443/tcp"
- "8444:8444/tcp"
healthcheck:
test: ["CMD", "kong", "health"]
interval: 10s
timeout: 10s
retries: 10
restart: on-failure
Also tried running it by 2 steps:
docker-compose up kong-postgres, it's ok:
$ docker-compose up kong-postgres
Starting kong-postgres ... done
Attaching to kong-postgres
kong-postgres |
kong-postgres | PostgreSQL Database directory appears to contain a database; Skipping initialization
kong-postgres |
kong-postgres | 2019-11-20 08:22:37.057 UTC [1] LOG: starting PostgreSQL 12.1 (Debian 12.1-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
kong-postgres | 2019-11-20 08:22:37.057 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
kong-postgres | 2019-11-20 08:22:37.057 UTC [1] LOG: listening on IPv6 address "::", port 5432
kong-postgres | 2019-11-20 08:22:37.060 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
kong-postgres | 2019-11-20 08:22:37.128 UTC [25] LOG: database system was shut down at 2019-11-20 08:08:28 UTC
kong-postgres | 2019-11-20 08:22:37.176 UTC [1] LOG: database system is ready to accept connections
And the database can connect via psql -h localhost -p 5432 -U kong -d kong:
$ psql -h localhost -p 5432 -U kong -d kong
Password for user kong:
psql (11.5, server 12.1 (Debian 12.1-1.pgdg100+1))
WARNING: psql major version 11, server major version 12.
Some psql features might not work.
Type "help" for help.
kong=# \q
docker-compose up kong is failed:
$ docker-compose up kong
kong-postgres is up-to-date
Recreating kong_kong_1 ... done
Attaching to kong_kong_1
kong_1 | Error: [PostgreSQL error] failed to retrieve PostgreSQL server_version_num: host or service not provided, or not known
kong_1 |
kong_1 | Run with --v (verbose) or --vv (debug) for more details
p.s.: The official Docker Compose template is failed too:
kong-migrations-up_1 | Error: Cannot run migrations: database needs bootstrapping; run 'kong migrations bootstrap'
kong-migrations-up_1 |
kong-migrations-up_1 | Run with --v (verbose) or --vv (debug) for more details
version: "3.7"
volumes:
kong_data: {}
networks:
kong-net:
services:
#######################################
# Postgres: The database used by Kong
#######################################
kong-database:
image: postgres:9.6
container_name: kong-postgres
restart: on-failure
networks:
- kong-net
volumes:
- kong_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: kong
POSTGRES_PASSWORD: ${KONG_PG_PASSWORD:-kong}
POSTGRES_DB: kong
ports:
- "5432:5432"
healthcheck:
test: ["CMD", "pg_isready", "-U", "kong"]
interval: 30s
timeout: 30s
retries: 3
#######################################
# Kong database migration
#######################################
kong-migration:
image: ${KONG_DOCKER_TAG:-kong:latest}
command: kong migrations bootstrap
networks:
- kong-net
restart: on-failure
environment:
KONG_DATABASE: postgres
KONG_PG_HOST: kong-database
KONG_PG_DATABASE: kong
KONG_PG_USER: kong
KONG_PG_PASSWORD: ${KONG_PG_PASSWORD:-kong}
depends_on:
- kong-database
#######################################
# Kong: The API Gateway
#######################################
kong:
image: ${KONG_DOCKER_TAG:-kong:latest}
restart: on-failure
networks:
- kong-net
environment:
KONG_DATABASE: postgres
KONG_PG_HOST: kong-database
KONG_PG_DATABASE: kong
KONG_PG_USER: kong
KONG_PG_PASSWORD: ${KONG_PG_PASSWORD:-kong}
KONG_PROXY_LISTEN: 0.0.0.0:8000
KONG_PROXY_LISTEN_SSL: 0.0.0.0:8443
KONG_ADMIN_LISTEN: 0.0.0.0:8001
depends_on:
- kong-database
healthcheck:
test: ["CMD", "kong", "health"]
interval: 10s
timeout: 10s
retries: 10
ports:
- "8000:8000"
- "8001:8001"
- "8443:8443"
- "8444:8444"
#######################################
# Konga database prepare
#######################################
konga-prepare:
image: pantsel/konga:latest
command: "-c prepare -a postgres -u postgresql://kong:${KONG_PG_PASSWORD:-kong}#kong-database:5432/konga"
networks:
- kong-net
restart: on-failure
depends_on:
- kong-database
#######################################
# Konga: Kong GUI
#######################################
konga:
image: pantsel/konga:latest
restart: always
networks:
- kong-net
environment:
DB_ADAPTER: postgres
DB_URI: postgresql://kong:${KONG_PG_PASSWORD:-kong}#kong-database:5432/konga
NODE_ENV: production
depends_on:
- kong-database
ports:
- "1337:1337"
I give up the docker-compose to run Kong, and back to user Docker command to do it in few steps below:
1. Create a Docker network:
$ docker network create kong-net
2. Start your database:
$ docker run -d --name kong_database \
--network=kong-net \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
--volume "$PWD/postgres-data":/var/lib/postgresql/data \
postgres:9.6
3. Prepare your database:
$ docker run --rm \
--network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong_database" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong_database" \
kong:latest kong migrations bootstrap
4. Start Kong
$ docker run -d --name kong \
--network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong_database" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong_database" \
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
-e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
--volume "$PWD/conf":/etc/nginx \
-p 8000:8000 \
-p 8443:8443 \
-p 8001:8001 \
-p 8444:8444 \
kong:latest
5. Use Kong
$ curl -i http://localhost:8001/
I use Konga GUI for Kong Admin API:
$ docker run --rm \
--network=kong-net \
pantsel/konga -c prepare -a postgres -u postgresql://kong#kong_database:5432/konga_db
$ docker run -d --name konga \
-p 1337:1337 \
--network=kong-net \
-e "DB_ADAPTER=postgres" \
-e "DB_HOST=kong_database" \
-e "DB_USER=kong" \
-e "DB_DATABASE=konga_db" \
-e "KONGA_HOOK_TIMEOUT=120000" \
-e "NODE_ENV=production" \
pantsel/konga
Open the http://localhost:1337/ to start use it.
Wish this can help someone else.
P.S.: Wish to have a sample docker-compose.yml also.
I had the same error.
A solution for local development is to use:
POSTGRES_HOST_AUTH_METHOD: trust
Put this in your docker-compose, under kong-database environment.
This is very un-safe for production, because it trusts all connections to the DB.
I'm trying to run the simplest container of mongo and mongo-express using docker-compose. I have faced with many errors that will be explained later on.
I have tried the following docker-compose configurations:
1.
version: '2'
services:
mongo:
image: mongo:latest
mongo-express:
image: mongo-express:latest
ports:
- 8082:8081
2.
version: '2'
services:
mongo:
image: mongo:latest
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
ports:
- 27017:27017
volumes:
- db-data:/data/db
- mongo-config:/data/configdb
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
volumes:
db-data:
mongo-config:
and etc. But all of them have the following common error whenever I execute docker-compose -f docker-compose.yml up:
mongo-express_1 | Mongo Express server listening at http://0.0.0.0:8081
mongo-express_1 | Server is open to allow connections from anyone (0.0.0.0)
mongo-express_1 | basicAuth credentials are "admin:pass", it is recommended you change this in your config.js!
mongo-express_1 |
mongo-express_1 | /node_modules/mongodb/lib/server.js:265
mongo-express_1 | process.nextTick(function() { throw err; })
mongo-express_1 | ^
mongo-express_1 | MongoError: failed to connect to server [mongo:27017] on first connect
mongo-express_1 | at Pool.<anonymous> (/node_modules/mongodb-core/lib/topologies/server.js:326:35)
mongo-express_1 | at emitOne (events.js:116:13)
mongo-express_1 | at Pool.emit (events.js:211:7)
mongo-express_1 | at Connection.<anonymous> (/node_modules/mongodb-core/lib/connection/pool.js:270:12)
mongo-express_1 | at Object.onceWrapper (events.js:317:30)
mongo-express_1 | at emitTwo (events.js:126:13)
mongo-express_1 | at Connection.emit (events.js:214:7)
mongo-express_1 | at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:175:49)
mongo-express_1 | at Object.onceWrapper (events.js:315:30)
mongo-express_1 | at emitOne (events.js:116:13)
I have searched this issue all over the internet and github repositories and tried the other solutions, but none of them worked appropriately.
link1 | link2 | link3 | link4 | etc.
First I have run docker stop $(docker ps -a -q) and then changed the docker-compose.yml file to the following content:
version: '2'
services:
mongo:
image: mongo:3.4
container_name: mongo
ports:
- '27017:27017'
volumes:
- '/data/configdb:/data/configdb'
- '/data/db:/data/db'
mongo-express:
image: mongo-express:0.49.0
container_name: mongo_express
depends_on:
- 'mongo'
ports:
- '5050:8081'
environment:
- 'ME_CONFIG_OPTIONS_EDITORTHEME=ambiance'
docker run -d
-p 8081:8081
--name mongo-express
-e ME_CONFIG_MONGODB_ADMINUSERNAME=**<username>**
-e ME_CONFIG_MONGODB_ADMINPASSWORD=**<password>**
--net **<mongo network name>**
-e ME_CONFIG_MONGODB_SERVER=**<mongodb container name>**
-e ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
mongo-express
I am trying to run the mongo docker image with authentication. Following the most simple example from the documentation I ran the mongo and the mongo-express images by the docker-compose up command. My docker-compose.yml at this stage:
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
This runs, both containers start ok and I can browse the contents of mongo from the mongo-express website. However, whenever I change the username or the password in the docker-compose.yml file, for example to this:
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example123
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example123
the mongo-express throws an unauthotrized error message:
mongo-express_1 | Admin Database connected
mongo-express_1 | { MongoError: Authentication failed.
mongo-express_1 | at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:31:11)
mongo-express_1 | at /node_modules/mongodb-core/lib/connection/pool.js:483:72
mongo-express_1 | at authenticateStragglers (/node_modules/mongodb-core/lib/connection/pool.js:429:16)
mongo-express_1 | at Connection.messageHandler (/node_modules/mongodb-core/lib/connection/pool.js:463:5)
mongo-express_1 | at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:319:22)
mongo-express_1 | at emitOne (events.js:116:13)
mongo-express_1 | at Socket.emit (events.js:211:7)
mongo-express_1 | at addChunk (_stream_readable.js:263:12)
mongo-express_1 | at readableAddChunk (_stream_readable.js:250:11)
mongo-express_1 | at Socket.Readable.push (_stream_readable.js:208:10)
mongo-express_1 | name: 'MongoError',
mongo-express_1 | message: 'Authentication failed.',
mongo-express_1 | ok: 0,
mongo-express_1 | errmsg: 'Authentication failed.',
mongo-express_1 | code: 18,
mongo-express_1 | codeName: 'AuthenticationFailed' }
mongo-express_1 | unable to list databases
mongo-express_1 | { MongoError: command listDatabases requires authentication
mongo-express_1 | at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:31:11)
mongo-express_1 | at /node_modules/mongodb-core/lib/connection/pool.js:483:72
mongo-express_1 | at authenticateStragglers (/node_modules/mongodb-core/lib/connection/pool.js:429:16)
mongo-express_1 | at Connection.messageHandler (/node_modules/mongodb-core/lib/connection/pool.js:463:5)
mongo-express_1 | at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:319:22)
mongo-express_1 | at emitOne (events.js:116:13)
mongo-express_1 | at Socket.emit (events.js:211:7)
mongo-express_1 | at addChunk (_stream_readable.js:263:12)
mongo-express_1 | at readableAddChunk (_stream_readable.js:250:11)
mongo-express_1 | at Socket.Readable.push (_stream_readable.js:208:10)
mongo-express_1 | name: 'MongoError',
mongo-express_1 | message: 'command listDatabases requires authentication',
mongo-express_1 | ok: 0,
mongo-express_1 | errmsg: 'command listDatabases requires authentication',
mongo-express_1 | code: 13,
mongo-express_1 | codeName: 'Unauthorized' }
No matter what username or password I enter in docker-compose.yml, I cannot make mongo-express connect to mongo, only if I use the original root and example pair.
Note, that I am not getting the username and password as environment variables, but they are directly typed into the docker-compose.yml file as you can see here.
Also note, that when I change the MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD (mongo's) variables to anything, they don't seem to have an effect, I can still connect with mongo-express using the original root and example credentials.
What causes this behaviour? How can I make this work?
Your docker-compose command:
docker-compose up --build --force-recreate
Mongo image uses anonymous volumes, so you need also --renew-anon-volumes (doc):
docker-compose up --build --force-recreate --renew-anon-volumes
Otherwise previous volume with already initialized DB is used => INITDB env variables won't be used.
What worked for me was just adding the environment variable for the mongodb server.
ME_CONFIG_MONGODB_SERVER: mongo
like this:
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_SERVER: mongo
If you create the mongodb container (docker-compose up will build/pull the image and start a container) giving it the username and password, mongodb will configure itself on the first run and never again! Until you create a new container for that service, the default user/pass will be the ones set initially. If you want to change the default, a new container must be created.
You can also add users after the container has started.
Only stopping the service containers (docker-compose stop) will not destroy de container. To do so, call docker-compose down or, when starting, call docker-compose up --force-recreate.
Regards
In my case I had to first stop all my containers.
Then docker system prune -a --volumes
⚠️ This is the last resort. Before using this command check the docker doc,
The docker system prune -a --volumes can solve the problem but a much simpler way is (if you are mounting a data volume for db to be persistent) to just delete and recreate a new one. This way other volumes will be untouched.
And of course combine it with : docker-compose up --build --force-recreate --renew-anon-volumes
I am trying to connect an express container to MongoDB image with docker compose but the connection is being rejected, I can connect to the db with robomongo. I can't get what is happening, this is the express code that connects it:
mongoose.connect('mongodb://localhost:27017/database')
.then(()=>console.log('connection succesfull to url'))
.catch((err)=>console.error(err));
This is the docker-compose file
version: "3"
services:
backend:
build:
context: ../backend
dockerfile: ${PWD}/images/backend/Dockerfile
container_name: backend
ports:
- "${BACKEND_PORT}:${BACKEND_PORT}"
env_file:
- ./deploy.env
environment:
- PORT=3000
- MONGO_CONNECTION=${MONGO_CONNECTION}
command: npm start
links:
- mongodb
depends_on:
- mongodb
front-app:
build:
context: ../front-app
dockerfile: ${PWD}/images/angular/Dockerfile
container_name: front-app
ports:
- "${FRONTEND_PORT}:4200"
env_file:
- ./deploy.env
command: npm start
mongodb:
image: mongo:3.6
container_name: mongo
volumes:
- "${MONGO_DB_DATA}:/data/db"
- "${MONGO_DB_DATA}:/data/configdb"
ports:
- "27017:27017"
This is the error
backend | { MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
backend | at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:564:11)
backend | at Pool.emit (events.js:182:13)
backend | at Pool.EventEmitter.emit (domain.js:442:20)
backend | at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:317:12)
backend | at Object.onceWrapper (events.js:273:13)
backend | at Connection.emit (events.js:182:13)
backend | at Connection.EventEmitter.emit (domain.js:442:20)
backend | at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:246:50)
backend | at Object.onceWrapper (events.js:273:13)
backend | at Socket.emit (events.js:182:13)
backend | at Socket.EventEmitter.emit (domain.js:442:20)
backend | at emitErrorNT (internal/streams/destroy.js:82:8)
backend | at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
backend | at process._tickCallback (internal/process/next_tick.js:63:19)
backend | name: 'MongoNetworkError',
backend | errorLabels: [ 'TransientTransactionError' ],
backend | [Symbol(mongoErrorContextSymbol)]: {} }
Every process running in docker container thinks that he is "the only one in the world". It means that for this process localhost means: my, container's localhost. And your backend is alone in his container so that's why he cannot find mongodb under localhost.
To fix that problem you should place hostname "mongodb" instead of "localhost" as in docker-compose you can access services using theirs names - it means that mongodb container can also access your backend using "backend" domain.
Please note also that "links" is deprecated in docker and shouldn't be used - it's not required in your configuration as docker-compose gives every service within docker-compose file access to each other using method mentioned above.
Jakub Bujny answer is spot on, I did not understand because it did not include an example. So I am adding one.
mongoose.connect('mongodb://mongodb:27017/database')
.then(()=>console.log('connection succesfull to url'))
.catch((err)=>console.error(err));