Issue
how to use .env in Dockerfile if i dont push the .env file on my repository ? because i want to use dockerfile to work with jenkins on my repository
my dockerfile like this
FROM node:lts-alpine
COPY ./ ./
RUN npm install
EXPOSE 8090
CMD ["npm", "run" , 'dev']
i am very new about this, and here i am trying to deploy my nodejs api with docker and integration with jenkins for Continues deployment , i have follow some articles so i tried to using dockerfile and jenkisfile on my linux server
my .env is something like this , for mongod db
DB_USERNAME=for_USERNAME_DB
DB_PASSWORD=for_PASSWORD_DB
DB_NAME=for_DATABASE_NAME
my example Jenkinsfile
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'docker build -t mygithub/express-api:latest'
}
}
stage('Test') {
steps {
sh 'docker run mygithub/express-api:latest npm test'
}
}
stage('Deploy') {
when {
branch 'master'
}
steps {
sh 'docker push mygithub/express-api:latest'
}
}
}
post {
failure {
echo 'build is broken. notify team!'
}
}
}
Solution
You can create .env or env_file_name at run time by using a script in Jenkins or Configuration tool with your environment values as below.
DB_USERNAME=for_USERNAME_DB
DB_PASSWORD=for_PASSWORD_DB
DB_NAME=for_DATABASE_NAME
In Jenkins use --env-file
option while running the container to pass the environment variable inside the container for application. In this way you are not showing your environment variable in Dockerfile.
stage('Test')
{
steps
{
sh 'docker run --env-file=env_file_name mygithub/express-api:latest npm test'
}
}
Answered By - Mahattam
Answer Checked By - Clifford M. (JavaFixing Volunteer)