Mongo Express authentication failed with docker compose - mongodb

I am trying to run mongodb and mongo express in two containers using docker-compose. It seems like the mongodb container works fine but the mongo express container keeps exiting with code 0.
The error I get is: Could not connect to database using connectionString: mongodb://rootuser:rootpass#mongodb:27017/"
mongo-express | (node:8) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodb:27017] on first connect [MongoError: Authentication failed.
My docker-compose.yml looks like this:
version: "3.8"
services:
mongodb:
image: mongo
container_name: mongodb
ports:
- 27017:27017
volumes:
- data:/data
environment:
- MONGO_INITDB_ROOT_USERNAME=rootuser
- MONGO_INITDB_ROOT_PASSWORD=rootpass
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=rootuser
- ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass
- ME_CONFIG_MONGODB_SERVER=mongodb
volumes:
data: {}
networks:
default:
name: mongodb_network

In MongoDB you can create user accounts in any database you wish. For example, I can create a user account called 'JohnDoe' in database 'MyDatabase1' and another account called 'JohnDoe' in database 'MyDatabase2'. These are two different accounts. You specify the authorizing database in your database connection string. Because of this feature it can become confusing. As such it has become the standard, and recommended approach to create database users in a single database called 'admin'. By adding users to a single database it is easier to manage.
In this post, you did not identify/specify which database is your authorization database for the user 'rootuser' and the connection info does not seem to contain such a concept. I suspect your user is defined in a database but when you attempt to connect it is looking at the temp database and not finding the user.

Related

Postgres and Docker Compose; password authentication fails and role 'postgres' does not exist. Cannot connect from pgAdmin4

I have a docker-compose that brings up the psql database as below, currently I'm trying to connect to it with pgAdmin4 (not in a docker container) and be able to view it. I've been having trouble authenticating with the DB and I don't understand why.
docker-compose
version: "3"
services:
# nginx and server also have an override, but not important for this q.
nginx:
ports:
- 1234:80
- 1235:443
server:
build: ./server
ports:
- 3001:3001 # app server port
- 9230:9230 # debugging port
env_file: .env
command: yarn dev
volumes:
# Mirror local code but not node_modules
- /server/node_modules/
- ./server:/server
database:
container_name: column-db
image: 'postgres:latest'
restart: always
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres # The PostgreSQL user (useful to connect to the database)
POSTGRES_PASSWORD: root # The PostgreSQL password (useful to connect to the database)
POSTGRES_DB: postgres # The PostgreSQL default database (automatically created at first launch)
volumes:
- ./db-data/:/var/lib/postgresql/data/
networks:
app-network:
driver: bridge
I do docker-compose up then check the logs, and it says that it is ready for connections. I go to pgAdmin and enter the following:
where password is root. I then get this error:
FATAL: password authentication failed for user "postgres"
I check the docker logs and I see
DETAIL: Role "postgres" does not exist.
I'm not sure what I'm doing wrong, according to the docs the super user should be created with those specifications. Am I missing something? Been banging my head against this for an hour now. Any help is appreciated!
#jjanes solved it in a comment, I had used a mapped volume and never properly set up the db. Removed the volume and we're good to go.

MongoDb : Cannot connect database on Docker container

I use the following docker-compose.yml to build a MongoDb database in a Docker container:
version: "3"
services:
mongodb:
image: mongo:latest
container_name: mongodb
volumes:
- mongo_data:/data/db
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: pass
MONGODB_USERNAME: user
MONGODB_PASSWORD: pass
MONGO_INITDB_DATABASE: social-network-db
volumes:
mongo_data:
Then I restarted the container and try to connect database via MongoDb Compass app by using these values:
Connection parameters
So, what is wrong and why I cannot connect to my MongoDb database? If you have some suggestion regarding to adocker-compose.yml parameter or creating used while building container, please inform me.

FluentMySQL migration fails when building with docker-compose but not when building from Xcode

I'm getting the following crash when using docker-compose with Vapor & FluentMySQL.
api_1 | [ INFO ] Migrating 'mysql' database (/app/.build/checkouts/fluent/Sources/Fluent/Migration/MigrationConfig.swift:69)
api_1 | Fatal error: Error raised at top level: NIO.ChannelError.connectFailed(NIO.NIOConnectionError(host: "db", port: 3309, dnsAError: nil, dnsAAAAError: nil, connectionErrors: [NIO.SingleConnectionFailure(target: [IPv4]db/172.27.0.2:3309, error: connection reset (error set): Connection refused (errno: 111))])): file /home/buildnode/jenkins/workspace/oss-swift-5.1-package-linux-ubuntu-18_04/swift/stdlib/public/core/ErrorType.swift, line 200
Migration fails when using:
migrations.add(model: Model.self, database: .mysql)
If I remove that so no Models are migrated the app builds without errors and I am able to access it at http://localhost/.
My docker-compose.yml looks like this:
version: "3.7"
services:
api:
image: vaporapiimage
ports:
- 80:8080
environment:
MYSQL_HOST: db
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_DATABASE: dbname
SLEEP_LENGTH: 7
MYSQL_PORT: 3309
depends_on:
- db
db:
image: mysql:8.0.1
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: dbname
MYSQL_USER: user
MYSQL_PASSWORD: password
ports:
- "3309:3306"
It builds and runs as expected locally from Xcode (as in without Docker).
If the api service is commented out then the db service will run on its own and create the database on first run.
Any help would be greatly appreciated.
UPDATE:
Replacing the api with Adminer results in being able to access the db through Adminer in the browser. So the db service is accessible and it seems as if the issue is with Vapor/Fluent (or rather my use of them):
admin:
image: adminer
ports:
- 8080:8080
The problem was that I had mapped the port for db to 3309 so that it didn't conflict with the host MYSQL but had also set the ENV 'MYSQL_PORT' to 3309 when that should have been kept as the default 3306 because that was for communication between containers.

Docker password authentication failed for user "postgres"

I'm writing a docker-compose file to launch some services. But the db service is a trouble maker, I always get this error:
FATAL: password authentication failed for user "postgres"
DETAIL: Password does not match for user "postgres".
Connection matched pg_hba.conf line 95: "host all all all md5"
I've read a lot of threads, and I've correctly set the POSTGRES_USER and POSTGRES_PASSWORD. I have also remove the previous volumes and container to force postgresql to re-init the password. But I can't figure out why it's still not working.
So what is the correct way to force the re-initialization of the postgresql image. So I would be able to connect to my database.
I've seen that this error: Connection matched pg_hba.conf line 95: "host all all all md5", and I've heard about the postgres conf file. But it's an official container it's supposed to work, isn't it ?
version: '3'
services:
poll:
build: poll
container_name: "poll"
ports:
- "5000:80"
networks:
- poll-tier
environment:
- REDIS_HOST=redis
depends_on:
- redis
worker:
build: worker
container_name: "worker"
networks:
- back-tier
environment:
- REDIS_HOST=redis
- POSTGRES_HOST=db
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=root
depends_on:
- redis
- db
redis:
image: "redis:alpine"
container_name: "redis"
networks:
- poll-tier
- back-tier
result:
build: result
container_name: "result"
ports:
- "5001:80"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=root
- POSTGRES_HOST=db
- RESULT_PORT=80
networks:
- result-tier
depends_on:
- db
db:
image: "postgres:alpine"
container_name: "db"
restart: always
networks:
- back-tier
- result-tier
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=root
- POSTGRES_DB=postgres
volumes:
db-data:
driver: local
networks:
poll-tier: {}
back-tier: {}
result-tier: {}
I'm expected to get the db connected, and not password authentication failed for user "postgres".
Make sure your APPs (not the database container) are actually using the POSTGRES_USER and POSTGRES_PASSWORD variables. I suspect they are looking for something like DB_USER or similar and so aren't getting the right values in.
By default, every PostgreSQL database driver and admin tool defaults to the postgres user. This may explain why the error message complains about postgres even if the environment variable isn't being used.
A good way to verify is to change all references to the database user in the docker-compose file to something like postgres2. I suspect you'll still see apps complaining that password auth failed for postgres.
In my case, it was caused by postgres.exe service on Windows 10 running in the background. When I stopped the service, uninstalled PostgreSQL 12 from Windows, and restarted, I could finally connect to Postgres Docker container.

Accessing postgres data in docker-compose network

I'm having trouble accessing a database created from a docker-compose file.
Given the following compose file, I should be able to connect to it from java using something like:
jdbc:postgresql://eprase:eprase#database:7000/eprase
However, the connection is rejected. I can't even use PGAdmin to connect it using the same details to create a new server.
I've entered the database container and ran psql commands to verify that the eprase user and database have been created according to postgres Docker documentation, everything seems fine. I can't tell if the problem is within the database container or something I need to change in the compose network.
The client & server services can largely be ignored, the server is a java based web API and the client is an Angular app.
Compose file:
version: "3"
services:
client:
image: eprase/client:latest
build: ./client/eprase-app
networks:
api:
ports:
- "5000:80"
tty: true
depends_on:
- server
server:
image: eprase/server:latest
build: ./server
networks:
api:
ports:
- "6000:8080"
depends_on:
- database
database:
image: postgres:9
volumes:
- "./database/data:/var/lib/postgresql/data"
environment:
- "POSTGRES_USER=eprase"
- "POSTGRES_PASSWORD=eprase"
- "POSTGRES_DB=eprase"
networks:
api:
ports:
- "7000:5432"
restart: unless-stopped
pgadmin:
image: dpage/pgadmin4:latest
environment:
- "PGADMIN_DEFAULT_EMAIL=admin#eprase.com"
- "PGADMIN_DEFAULT_PASSWORD=eprase"
networks:
api:
ports:
- "8000:80"
depends_on:
- database
networks:
api:
The PostgreSQL database is listening on container port 5432. The 7000:5432 line is mapping host port 7000 to container port 5432. That allows you to connect to the database on port 7000. But, your services on a common network (api) should communicate with each other via the container ports.
So, from the perspective of the containers for the client and server services, the connection string should be:
jdbc:postgresql://eprase:eprase#database:5432/eprase