最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Firebase serviceAccountKey.json not being read when containerizing NestJS backend - production - Stack Overflow

programmeradmin1浏览0评论

I'm learning Docker and trying to containerize my NestJS backend. My backend uses Firebase as a database, and I'm having issues loading the Firebase credentials.

Locally, everything works fine because I use the serviceAccountKey.json file for authentication with Firebase. However, when running inside Docker, it seems like the file is not being read properly. 1.Loading the serviceAccountKey.json file directly in the code:

admin.initializeApp({
  credential: admin.credential.cert(require("./serviceAccountKey.json"))
});

Error in Docker: The file appears to be missing or not read properly. 2.Using environment variables and parsing the JSON in the code:

admin.initializeApp({
  credential: admin.credential.cert(JSON.parse(process.env.FIREBASE_CREDENTIALS))
});

.env file

FIREBASE_CREDENTIALS='{"type": "service_account", "project_id": "...", "private_key": "..."}'

Error in Docker: The JSON is not correctly parsed.

3.Mounting the file inside the container using volumes in docker-compose.yml: volumes:

 ./serviceAccountKey.json:/app/serviceAccountKey.json

Error: The file still can't be found inside the container.

Dockerfile

FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "start"]

docker-compose.yml

version: '3.8'
services:
  backend:
    build: .
    ports:
      - "3000:3000"
    env_file:
      - .env
    volumes:
      - .:/app
      - /app/node_modules

How can I ensure that Firebase reads the serviceAccountKey.json file correctly inside the Docker container? Is there a best practice for handling this type of credential?

I'm learning Docker and trying to containerize my NestJS backend. My backend uses Firebase as a database, and I'm having issues loading the Firebase credentials.

Locally, everything works fine because I use the serviceAccountKey.json file for authentication with Firebase. However, when running inside Docker, it seems like the file is not being read properly. 1.Loading the serviceAccountKey.json file directly in the code:

admin.initializeApp({
  credential: admin.credential.cert(require("./serviceAccountKey.json"))
});

Error in Docker: The file appears to be missing or not read properly. 2.Using environment variables and parsing the JSON in the code:

admin.initializeApp({
  credential: admin.credential.cert(JSON.parse(process.env.FIREBASE_CREDENTIALS))
});

.env file

FIREBASE_CREDENTIALS='{"type": "service_account", "project_id": "...", "private_key": "..."}'

Error in Docker: The JSON is not correctly parsed.

3.Mounting the file inside the container using volumes in docker-compose.yml: volumes:

 ./serviceAccountKey.json:/app/serviceAccountKey.json

Error: The file still can't be found inside the container.

Dockerfile

FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "start"]

docker-compose.yml

version: '3.8'
services:
  backend:
    build: .
    ports:
      - "3000:3000"
    env_file:
      - .env
    volumes:
      - .:/app
      - /app/node_modules

How can I ensure that Firebase reads the serviceAccountKey.json file correctly inside the Docker container? Is there a best practice for handling this type of credential?

Share Improve this question asked Feb 7 at 19:54 Vitor DiamantinoVitor Diamantino 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Recommendation:

  • Use the environment variable approach as it is more secure & works best in dockerized environments.
  • Avoid hardcoding or manually copying sensitive files inside Docker images.

Update your .env file:

Make sure the private key has proper newlines (\n) and is correctly formatted.

FIREBASE_CREDENTIALS='{
  "type": "service_account",
  "project_id": "your_project_id",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEv...\n-----END PRIVATE KEY-----\n",
  "client_email": "your_client_email"
}'

Modify your firebase initialization code:

import * as admin from "firebase-admin";

if (!process.env.FIREBASE_CREDENTIALS) {
  throw new Error("FIREBASE_CREDENTIALS environment variable is missing");
}

// Fix potential issues with JSON parsing
const firebaseConfig = JSON.parse(
  process.env.FIREBASE_CREDENTIALS.replace(/\\n/g, "\n") 
);

// Initialize Firebase
admin.initializeApp({
  credential: admin.credential.cert(firebaseConfig),
});

Update docker-compose.yml to pass the environment file:

version: '3.8'
services:
  backend:
    build: .
    ports:
      - "3000:3000"
    env_file:
      - .env
    volumes:
      - .:/app
      - /app/node_modules

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论