Reputation: 49
Iam new to Dev-Ops and find myself a bit confused. I have a node-project which starts a server. One endpoint of that server should write to a database. On my local machine I test this behavior with a local DB. I created a .env.prod-file and a .env.dev-file to determine whether I use the local DB or my server DB (running on my server). I also read in blog posts that I should never ever push my .env-files to a remote repository.
Than I created a Dockerfile for building and shipping my application.
On my remote Gitlab-Repository I created a .gitlab-ci.yml which should build the docker-image from the Dockerfile every time there is a new commit to master.
But now I am faced with the issue that when I want to build the docker-image I need the .env.prod file but I should never push this file so GitLab has no access to it.
Is this described way an appropriate solution or do I do something entirely wrong(am I missing some core-concept in Dev-ops)?
This is my Dockerfile
FROM node:12.7-alpine AS build
WORKDIR /usr/graphql-server
COPY package.json /usr/graphql-server/
RUN npm install
COPY ./ /usr/graphql-server
RUN npm run build
COPY ./ /usr/graphql-server
EXPOSE 4000
CMD ["npm", "run", "start:prod"]
and the npm run start:prod
does this:
tsc && cross-env NODE_ENV=prod node dist/index.js
That is how I determine which configuration to use (index.ts):
function getConfig(): void {
let path;
if (process.env.NODE_ENV === 'prod') {
path = `${__dirname}/../src/.env.prod`;
} else {
path = `${__dirname}/../src/.env.dev`;
}
dotenv.config({ path });
}
Upvotes: 0
Views: 3423
Reputation: 49
Finally thanks to @AndreasJägle I came up with a completely different solution for then what I first thought of. I dont need a .env-file and dont use the described getConfig() method for this solution anymore. As well there is no need to store environment-variables in Gitlab.
First I now start my node process by passing environment variables at the end of the node command like node dist/index.js variable1
.
This environment variable can be read at runtime: const baseUrl = process.argv[2]
.
The baseUrl-variable can than be used to send requests to this certain url.
Second because I'am building my project using docker I have to inject the values for the environment-variables into my docker-image. For this I modified my dockerfile like this.
FROM node:12.7-alpine AS build
ENV BASE_URL=localhost
...
CMD ["sh", "-c", "node dist/index.js $BASE_URL"]
(note that localhost is the default value for the BASE_URL environment variable and will be overridden)
Finally I use the previously build image in a docker-compose file on my server and set the environment variable like this.
version: "3"
services:
test-service:
image: my-image:latest
container_name: 'my-container'
environment:
- BASE_URL=mydomain.com
Upvotes: 1