I have a node app using dotenv
to keep track of env variables. Its using the .env
file in the root folder to get the variables in runtime. The problem is when i'm using docker to build a node image the below line copies the .env
file for the build as well
FROM node:latest
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
EXPOSE 8000
If i build & pull the image from dockerhub. The file already contains the .env
file i used in development. Ideally , i would like to specify a different .env
file for production. (perhaps manually creating a new .env file in the production server)
i tried specifying the .env
file in dockerignore . but line COPY . /usr/src/app
still seems to copy the env file as well.
I do not need to use dotenv
as such. I tried specifying it like the below
version: '2'
services:
node:
container_name: node
build: .
env_file: .env
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
command: npm start
so i really dont need to specify a .env folder in the build. But this doesnt work as well.
How do i stop the COPY
command from copying the .env
file?
The .env file feature only works when you use the docker-compose up command and does not work with docker stack deploy . Both $VARIABLE and ${VARIABLE} syntax are supported.
The . env file, is only used during a pre-processing step when working with docker-compose. yml files. Dollar-notation variables like $HI are substituted for values contained in an “. env” named file in the same directory.
You can create an. env file in the application's root directory that contains key/value pairs defining the project's required environment variables.
env files are simply too risky and cumbersome for modern application development. While . env files are still commonly used and were an improvement upon storing secrets in source code, the security risks and impact on developer productivity are only now being fully realized.
There's a file for that!
Before the docker CLI sends the context to the docker daemon, it looks for a file named .dockerignore in the root directory of the context. If this file exists, the CLI modifies the context to exclude files and directories that match patterns in it. This helps to avoid unnecessarily sending large or sensitive files and directories to the daemon and potentially adding them to images using ADD or COPY.
In your case, you'll just need to echo .env >> .dockerignore
while in the same directory as your Dockerfile.
create a .dockerignore
file in the same directory as .env
and Dockerfile
then add .env
file to .dockerignore
file. now Docker will not include .env
file while building the image.
#.dockerignore
.env
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With