简体   繁体   中英

How to update configuration file (.env file) while running docker container

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

You can add a directory to the docker using -v<\/a>

docker run -v /Path/To/The/Env/file:/env-file-directory -p ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM