How to pass environment variables from docker-compose into the NodeJS project?
Asked Answered
V

3

29

I have a NodeJS application, which I want to docker-size.

The application consists of two parts:

  • server part, running an API which is taking data from a DB. This is running on the port 3000;

  • client part, which is doing a calls to the API end-points from the server part. This is running on the port 8080;

With this, I have a variable named "server_address" in my client part and it has the value of "localhost:3000". But here is the thing, the both projects should be docker-sized in a separate Dockerimage files and combined in one docker-compose.yml file.

So due some reasons, I have to run the docker containers via docker-compose.yml file. So is it possible to connect these things somehow and to pass the server address externally from dockerfile into the NodeJS project?

docker-composer.yml

version: "3"
services:
  client-side-app:
    image: my-client-side-docker-image
    environment:
      - BACKEND_SERVER="here we need to enter backend server"
    ports:
      - "8080:8080"
  server-side-app:
    image: my-server-side-docker-image
    ports:
      - "3000:3000"

both of the Dockerfile's looks like:

FROM node:8.11.1
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "dev"]

by having these files, I have the concern:

  • will I be able to use the variable BACKEND_SERVER somehow in the project? And if yes, how to do this? I'm not referring to the Dockerimage file, instead into the project itself?
Vanvanadate answered 4/10, 2018 at 15:45 Comment(5)
The answer is yes.Muttonchops
That'll work. But as you're in compose world, you could use the service name as the reference (if I'm understanding correctly)Hezekiah
@Hezekiah so how I will be able to call it? Not sure what are you referring to.Vanvanadate
@RobertMoskal how this can be accomplished?Vanvanadate
Sorry i was reading this on the bus yesterday - If I understand correctly, you can just use the service name server-side-app in your client-side config, as docker-compose will create a network where those containers can be looked up by their service name - equally the solution by @t-prisar works well!Hezekiah
H
48

Use process.env in node.js code, like this

process.env.BACKEND_SERVER

Mention your variable in docker-compose file.

version: "3"
services:
  client-side-app:
    image: my-client-side-docker-image
    environment:
      - BACKEND_SERVER="here we need to enter backend server"
    ports:
      - "8080:8080"
  server-side-app:
    image: my-server-side-docker-image
    ports:
      - "3000:3000"
Hak answered 4/10, 2018 at 19:6 Comment(1)
I got error URLSchemeUnknown: Not supported URL scheme http+docker, can you help?Cadenza
B
1

In addition to the previous answer, you can alternatively define variables and their values when running a container:

docker run --env variable1=value1 --env variable2=value2 <image>

Other two different ways are: (1) referring environment variables which you’ve already exported to your local environment and (2) loading the variables from a file:

(1)

# In your ~/.bash (or ~/.zshrc) file: export VAR1=value1
docker run --env VAR1 <image>

(2)

cat env.list
# Add the following in the env.list
# variable1=value1
# variable2=value2
# variable3=value3
docker run --env-file env.list <image>

Those options are useful in case you don't want to mention your variables in the docker-compose file.

Reference

Bosomy answered 18/10, 2022 at 18:39 Comment(0)
H
0

If you are using just docker without docker-compose, try using the ENV property in the Dockerfile to initialise the process.env.variable.

If you want to get the env from the CLI or during build time externally, combine it along with ARG property.

Eg Dockerfile for reference,

FROM node:20-alpine
// here 8000 is the default value if no value is passed during runtime. 
ARG PORT_ARG=8000
ENV PORT=$PORT_ARG
ENTRYPOINT ["node", "index.js" ]

Usage in CLI

// process.env.PORT will have 8081 as the value because of this step
docker build . --build-arg PORT_ARG=8081  -t web_api

docker run -it web_api
Hesitant answered 2/5, 2023 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.