Docker Community Forums

Share and learn in the Docker community.

Fatal error: No default database configured.: file FluentKit/Databases.swift, line 158

I am trying to launch a Vapor app container with a mongoDB database, using an environment variable that specifies my db’s name and port number. I am getting an error:

Fatal error: No default database configured.: file FluentKit/Databases.swift, line 158

I have confirmed that I have a mongoDB container running:

…though I am somehow unable to connect to it, or it does not believe it’s configured.

This is my docker-compose.yml file:

version: '3.7'

volumes:
  db_data:

x-shared_environment: &shared_environment
  LOG_LEVEL: ${LOG_LEVEL:-debug}
  DATABASE_HOST: db
  DATABASE_NAME: vapor_database
  DATABASE_USERNAME: vapor_username
  DATABASE_PASSWORD: vapor_password

services:
  app:
    image: prizmserver:latest
    build:
      context: .
    environment:
      <<: *shared_environment
    volumes:
      - $PWD/.env:/app/.env
    links:
      - db:db
    ports:
      - '8080:8080'
    # user: '0' # uncomment to run as root for testing purposes even though Dockerfile defines 'vapor' user.
    command: ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]
  migrate:
    image: prizmserver:latest
    build:
      context: .
    environment:
      <<: *shared_environment
    depends_on:
      - db
    command: ["migrate", "--yes"]
    deploy:
      replicas: 0
  revert:
    image: prizmserver:latest
    build:
      context: .
    environment:
      <<: *shared_environment
    depends_on:
      - db
    command: ["migrate", "--revert", "--yes"]
    deploy:
      replicas: 0
  db:
    image: mongo
    volumes:
      - db_data:/data/db
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

My configure.swift file currently looks like this:

public func configure(_ app: Application) throws {
    
    guard let connectionString = Environment.get("MONGODB") else {
        fatalError("No MongoDB connection string is available in .env")
    }
    print("Connection string: \(connectionString)")
    try app.databases.use(.mongo(connectionString: connectionString),
                          as: .mongo)
    
    app.migrations.add(CreateArticle())
    app.migrations.add(CreateMember())
    
    app.logger.logLevel = .debug
    
    try app.autoMigrate().wait()
    print("app db: \(app.db), app databases: \(app.databases)")
    // register routes
    try routes(app)
}

And my .env file contains just this line:

MONGODB=mongodb://db:27017/prizmserver

How can I “configure a default database”, if this error message is, in fact, directly me properly?