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 badge1 Answer
Reset to default 0Recommendation:
- 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