Reputation: 19
I have created a nodejs app and built docker image as per this link Dockerizing a Node.js web app
But, I am also using configuration file (.env file) where I can maintain all the environment variables and access them with process.env.<Variable_name>
.
My server.js
file looks like this.
'use strict';
const express = require('express');
require('dotenv').config()
// Constants
const PORT = process.env.PORT | 8080;
const HOST = process.env.HOST;
// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello world\n');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
And My .env file is this.
HOST=10.20.30.40
PORT=8080
I can change my IP address and port to anything with out changing any code in server.js
. As similar as this, I want update the .env when I am building it as a docker image.
This is my Dockerfile
FROM node:carbon
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
COPY .env .
EXPOSE 8080
CMD [ "npm", "start" ]
I know I can update the .env file while building image by giving --build-args. But every time if need to make change in .env, I have to rebuild the image and deploy it. So, I want to update the .env file while running the image or container.
In the below command is there any way to give some arguments so it will update the .env file in the docker.
docker run -p 49160:8080 -d <your username>/node-web-app
Upvotes: 1
Views: 1932
Reputation: 23565
You can add a directory to the docker using -v
docker run -v /Path/To/The/Env/file:/env-file-directory -p ...
Then target the file in the linked directory inside of your docker using the name /env-file-directory
Upvotes: 1