docker kong error to get connection with postgres - postgresql

My dockerCompose.yml:
version: "3.7"
networks:
kong-net:
volumes:
kong_data: {}
pghr:
external: true
pginv:
external: true
services:
#######################################
# Postgres: The database used by Kong
#######################################
kong-database:
image: postgres:11
container_name: kong-postgres
restart: on-failure
networks:
- kong-net
volumes:
- kong_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: kong
POSTGRES_PASSWORD: kong
POSTGRES_DB: kong
ports:
- "5434:5434"
healthcheck:
test: ["CMD", "pg_isready", "-U", "kong"]
interval: 30s
timeout: 30s
retries: 3
#######################################
# Kong database migration
#######################################
kong-migration:
image: kong:2.0.3-alpine
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
depends_on:
- kong-database
#######################################
# Kong: The API Gateway
#######################################
kong:
image: kong:2.0.3-alpine
restart: on-failure
container_name: kong
networks:
- kong-net
environment:
KONG_DATABASE: postgres
KONG_PG_HOST: kong-database
KONG_PG_DATABASE: kong
KONG_PG_USER: kong
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#kong-database:5434/konga"
networks:
- kong-net
restart: on-failure
links:
- kong-database
depends_on:
- kong-database
#######################################
# Konga: Kong GUI
#######################################
konga:
image: pantsel/konga:latest
container_name: konga
restart: always
networks:
- kong-net
environment:
DB_ADAPTER: postgres
DB_HOST: kong-database
DB_USER: kong
TOKEN_SECRET: FUEDASHFUAEHFEUAHFEU;
DB_DATABASE: kong
NODE_ENV: production
depends_on:
- kong-database
ports:
- "1337:1337"
but i get this on docker - compose logs on my container KONG:
kong | 2020/09/02 21:51:04 [error] 1#0: init_by_lua error: /usr/local/share/lua/5.1/kong/cmd/utils/migrations.lua:20: New migrations available; run 'kong migrations up' to proceed
kong | stack traceback:
kong | [C]: in function 'error'
kong | /usr/local/share/lua/5.1/kong/cmd/utils/migrations.lua:20: in function 'check_state'
kong | /usr/local/share/lua/5.1/kong/init.lua:392: in function 'init'
kong | init_by_lua:3: in main chunk
kong | nginx: [error] init_by_lua error: /usr/local/share/lua/5.1/kong/cmd/utils/migrations.lua:20: New migrations available; run 'kong migrations up' to proceed
kong | stack traceback:
kong | [C]: in function 'error'
kong | /usr/local/share/lua/5.1/kong/cmd/utils/migrations.lua:20: in function 'check_state'
kong | /usr/local/share/lua/5.1/kong/init.lua:392: in function 'init'
kong | init_by_lua:3: in main chunk
kong | 2020/09/02 21:51:08 [notice] 1#0: using the "epoll" event method
kong | 2020/09/02 21:51:08 [notice] 1#0: openresty/1.15.8.3
kong | 2020/09/02 21:51:08 [notice] 1#0: built by gcc 9.2.0 (Alpine 9.2.0)
kong | 2020/09/02 21:51:08 [notice] 1#0: OS: Linux 5.4.0-45-generic
kong | 2020/09/02 21:51:08 [notice] 1#0: getrlimit(RLIMIT_NOFILE): 1048576:1048576
kong | 2020/09/02 21:51:08 [notice] 1#0: start worker processes
kong | 2020/09/02 21:51:08 [notice] 1#0: start worker process 22
kong | 2020/09/02 21:51:08 [notice] 1#0: start worker process 23
kong | 2020/09/02 21:51:08 [notice] 1#0: start worker process 24
kong | 2020/09/02 21:51:08 [notice] 1#0: start worker process 25
kong | 2020/09/02 21:51:08 [notice] 1#0: start worker process 26
kong | 2020/09/02 21:51:08 [notice] 1#0: start worker process 27
kong | 2020/09/02 21:51:08 [notice] 1#0: start worker process 28
kong | 2020/09/02 21:51:08 [notice] 1#0: start worker process 29
kong | 2020/09/02 21:51:08 [notice] 1#0: start worker process 30
kong | 2020/09/02 21:51:08 [notice] 1#0: start worker process 31
kong | 2020/09/02 21:51:08 [notice] 1#0: start worker process 32
kong | 2020/09/02 21:51:08 [notice] 1#0: start worker process 33
kong | 2020/09/02 21:51:08 [notice] 22#0: *5 [lua] cache_warmup.lua:46: cache_warmup_single_entity(): Preloading 'services' into the core_cache..., context: init_worker_by_lua*
kong | 2020/09/02 21:51:08 [notice] 22#0: *5 [lua] cache_warmup.lua:85: cache_warmup_single_entity(): finished preloading 'services' into the core_cache (in 0ms), context: init_worker_by_lua*
kong | 2020/09/02 21:51:08 [notice] 22#0: *5 [lua] cache_warmup.lua:46: cache_warmup_single_entity(): Preloading 'plugins' into the core_cache..., context: init_worker_by_lua*
kong | 2020/09/02 21:51:08 [notice] 22#0: *5 [lua] cache_warmup.lua:85: cache_warmup_single_entity(): finished preloading 'plugins' into the core_cache (in 0ms), context: init_worker_by_lua*
on my container Konga-prepare:
konga-prepare_1 | debug: Preparing database...
konga-prepare_1 | Using postgres DB Adapter.
konga-prepare_1 | Failed to connect to DB Error: connect ECONNREFUSED 192.168.64.2:5434
konga-prepare_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
konga-prepare_1 | errno: 'ECONNREFUSED',
konga-prepare_1 | code: 'ECONNREFUSED',
konga-prepare_1 | syscall: 'connect',
konga-prepare_1 | address: '192.168.64.2',
konga-prepare_1 | port: 5434
konga-prepare_1 | }
on my container konga:
konga | _e: error: relation "public.konga_users" does not exist
konga | at Connection.parseE (/app/node_modules/sails-postgresql/node_modules/pg/lib/connection.js:539:11)
konga | at Connection.parseMessage (/app/node_modules/sails-postgresql/node_modules/pg/lib/connection.js:366:17)
konga | at Socket.<anonymous> (/app/node_modules/sails-postgresql/node_modules/pg/lib/connection.js:105:22)
konga | at Socket.emit (events.js:310:20)
konga | at Socket.EventEmitter.emit (domain.js:482:12)
konga | at addChunk (_stream_readable.js:286:12)
konga | at readableAddChunk (_stream_readable.js:268:9)
konga | at Socket.Readable.push (_stream_readable.js:209:10)
konga | at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
konga | length: 118,
konga | severity: 'ERROR',
konga | code: '42P01',
konga | detail: undefined,
konga | hint: undefined,
konga | position: '377',
konga | internalPosition: undefined,
konga | internalQuery: undefined,
konga | where: undefined,
konga | schema: undefined,
konga | table: undefined,
konga | column: undefined,
konga | dataType: undefined,
konga | constraint: undefined,
konga | file: 'parse_relation.c',
konga | line: '1159',
konga | routine: 'parserOpenTable'
konga | },
konga | rawStack: 'error: relation "public.konga_users" does not exist\n' +
konga | ' at Connection.parseE (/app/node_modules/sails-postgresql/node_modules/pg/lib/connection.js:539:11)\n' +
konga | ' at Connection.parseMessage (/app/node_modules/sails-postgresql/node_modules/pg/lib/connection.js:366:17)\n' +
konga | ' at Socket.<anonymous> (/app/node_modules/sails-postgresql/node_modules/pg/lib/connection.js:105:22)\n' +
konga | ' at Socket.emit (events.js:310:20)\n' +
konga | ' at Socket.EventEmitter.emit (domain.js:482:12)\n' +
konga | ' at addChunk (_stream_readable.js:286:12)\n' +
konga | ' at readableAddChunk (_stream_readable.js:268:9)\n' +
konga | ' at Socket.Readable.push (_stream_readable.js:209:10)\n' +
konga | ' at TCP.onStreamRead (internal/stream_base_commons.js:186:23)',
konga | details: 'Details: error: relation "public.konga_users" does not exist\n'
konga | }
I'm not able to imagine the reason for the error, my dockercompose seems to be configured correctly.
I configured the right port, but I have these errors I tried to fix and I can't.

Production
In case of MySQL or PostgresSQL adapters, Konga will not perform db migrations when running in production mode.
You can manually perform the migrations by calling $ node ./bin/konga.js prepare , passing the args needed for the database connectivity.
so in first run,you need set NODE_ENV as development,not production

Related

Mongo express always crashes in docker-compose

Here is my docker-compose file:
version: "3.1"
services:
mongodb:
container_name: mongodb
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: $$Admin123
MONGO_INITDB_DATABASE: xplorie
# hostname: mongodb
networks:
mynetwork:
ipv4_address: 172.19.0.20
ports:
- 27017:27017
volumes:
- /var/data/mongo:/data/db
mongo-express:
image: mongo-express
environment:
# ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_PORT: 27017
# ME_CONFIG_MONGODB_AUTH_USERNAME: admin
# ME_CONFIG_MONGODB_AUTH_PASSWORD: $$Admin123
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: $$Admin123
# ME_CONFIG_BASICAUTH_USERNAME: admin
# ME_CONFIG_BASICAUTH_PASSWORD: $$Admin123
# networks:
# mynetwork:
# ipv4_address: 172.19.0.21
volumes:
- /var/data/mongoclient:/data/db
depends_on:
- mongodb
ports:
- "3300:3000"
restart: always
networks:
mynetwork:
driver: bridge
ipam:
config:
- subnet: 172.19.0.0/24
gateway: 172.19.0.1
but when I run this mongo express always crashes. this is the log:
mongo-express_1 | Welcome to mongo-express
mongo-express_1 | ------------------------
mongo-express_1 |
mongo-express_1 |
mongo-express_1 | (node:7) [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_1 | Could not connect to database using connectionString: mongodb://mongo:27017"
mongo-express_1 | (node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [Error: getaddrinfo EAI_AGAIN mongo
mongo-express_1 | at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
mongo-express_1 | name: 'MongoNetworkError'
mongo-express_1 | }]
mongo-express_1 | at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
mongo-express_1 | at Pool.emit (events.js:314:20)
mongo-express_1 | at /node_modules/mongodb/lib/core/connection/pool.js:564:14
mongo-express_1 | at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
mongo-express_1 | at /node_modules/mongodb/lib/core/connection/connect.js:32:7
mongo-express_1 | at callback (/node_modules/mongodb/lib/core/connection/connect.js:300:5)
mongo-express_1 | at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:7)
mongo-express_1 | at Object.onceWrapper (events.js:421:26)
mongo-express_1 | at Socket.emit (events.js:314:20)
mongo-express_1 | at emitErrorNT (internal/streams/destroy.js:92:8)
mongo-express_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
mongo-express_1 | at processTicksAndRejections (internal/process/task_queues.js:84:21)
mongo-express_1 | (node:7) 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_1 | (node:7) [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.
deploy_mongo-express_1 exited with code 0
You have the mongo database container on one network (user defined) and the mongo-express container on the default bridge network. So they can't talk to each other.
Also, the hostname of the mongo db container is mongodb, so you should comment that back in.
Mongo express listens on port 8081, so that's the port you should map.
I'd just use the default bridge network and do something like this
version: "3.1"
services:
mongodb:
container_name: mongodb
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: $$Admin123
MONGO_INITDB_DATABASE: xplorie
ports:
- 27017:27017
volumes:
- /var/data/mongo:/data/db
mongo-express:
image: mongo-express
environment:
ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: $$Admin123
# ME_CONFIG_BASICAUTH_USERNAME: admin
# ME_CONFIG_BASICAUTH_PASSWORD: $$Admin123
depends_on:
- mongodb
ports:
- "3300:8081"
restart: always
You should then be able to access Mongo express on http://localhost:3300/.
Be aware that Mongo will not create a database for you because you don't have any initialization scripts. From the docs: 'MongoDB is fundamentally designed for "create on first use", so if you do not insert data with your JavaScript files, then no database is created.'
Another thing is that Mongo Express is still alpha software and it tries to connect to the database immediately when it starts and that connection often fails because Mongo needs a little time to be ready. By setting restart: always as you have, you'll restart Express until the connection succeeds. But you might see a failed container or two on startup.

Default Docker Compose for MongoDB and Mongo-Express times out on Debian

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.

airflow runs connections check against metadata db before init db

I am running airflow locally based on dockerfile, .env, docker-compose.yaml and entrypoint.sh
as "docker-compose -f docker-compose.yaml up"
And just after "airflow init db" in entrypoint.sh I am getting the following error:
[after it all is cool, I can run airflow. But this drives me crazy. Can anyone help me to resolve it, please?]
what's strange is that service queries the tables even before the db has been initiated
airflow_webserver | initiating db
airflow_webserver | DB: postgresql://airflow:***#airflow_metadb:5432/airflow
airflow_webserver | [2022-02-22 13:52:26,318] {db.py:929} INFO - Dropping tables that exist
airflow_webserver | [2022-02-22 13:52:26,570] {migration.py:201} INFO - Context impl PostgresqlImpl.
airflow_webserver | [2022-02-22 13:52:26,570] {migration.py:204} INFO - Will assume transactional DDL.
airflow_metadb | 2022-02-22 13:52:26.712 UTC [71] ERROR: relation "connection" does not exist at character 55
airflow_metadb | 2022-02-22 13:52:26.712 UTC [71] STATEMENT: SELECT connection.conn_id AS connection_conn_id
airflow_metadb | FROM connection GROUP BY connection.conn_id
airflow_metadb | HAVING count(*) > 1
airflow_metadb | 2022-02-22 13:52:26.714 UTC [72] ERROR: relation "connection" does not exist at character 55
airflow_metadb | 2022-02-22 13:52:26.714 UTC [72] STATEMENT: SELECT connection.conn_id AS connection_conn_id
airflow_metadb | FROM connection
airflow_metadb | WHERE connection.conn_type IS NULL
airflow_webserver | [2022-02-22 13:52:26,733] {db.py:921} INFO - Creating tables
airflow 2.2.3
postgres 13
in dockerfile:
ENTRYPOINT ["/entrypoint.sh"]
in docker-compose.yaml:
webserver:
env_file: ./.env
image: airflow
container_name: airflow_webserver
restart: always
depends_on:
- postgres
environment:
<<: *env_common
AIRFLOW__CORE__LOAD_EXAMPLES: ${AIRFLOW__CORE__LOAD_EXAMPLES}
AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION: ${AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION}
EXECUTOR: ${EXECUTOR}
_AIRFLOW_DB_UPGRADE: ${_AIRFLOW_DB_UPGRADE}
_AIRFLOW_WWW_USER_CREATE: ${_AIRFLOW_WWW_USER_CREATE}
_AIRFLOW_WWW_USER_USERNAME: ${_AIRFLOW_WWW_USER_USERNAME}
_AIRFLOW_WWW_USER_PASSWORD: ${_AIRFLOW_WWW_USER_PASSWORD}
_AIRFLOW_WWW_USER_ROLE: ${_AIRFLOW_WWW_USER_ROLE}
_AIRFLOW_WWW_USER_EMAIL: ${_AIRFLOW_WWW_USER_EMAIL}
logging:
options:
max-size: 10m
max-file: "3"
volumes:
- ./dags:bla-bla
- ./logs:bla-bla
ports:
- ${AIRFLOW_WEBSERVER_PORT}:${AIRFLOW_WEBSERVER_PORT}
command: webserver
healthcheck:
test: ["CMD-SHELL", "[ -f /usr/local/airflow/airflow-webserver.pid ]"]
interval: 30s
timeout: 30s
retries: 3

Mongo-Express: failed to connect to server [mongo:27017] on first connect

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

Mongo authentication inside Docker

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