0

I have an Ionic React app and I'm trying to inject some environment variables from a docker-compose file. I saw online that I can access the environment variables via process.env.VAR_NAME but that doesn't work for me. I tried to print the whole process.env object but no trace of my environment variables.

Dockerfile:

FROM node:12.21-alpine

RUN mkdir -p /home/app

COPY ./build /home/app

WORKDIR /home

RUN npm install -g serve

CMD ["serve", "-s", "app", "-l", "5000"]

docker-compose.yml:

version: "3.8"
services:
  myapp:
    image: myapp:local
    restart: always
    build:
      context: ./../
      dockerfile: ./docker/Dockerfile
    ports:
      - 5002:5000
    environment: 
      - MY_VAR=testValue

In my index.tsx file I logged the process.env variable and I get the following output:

console.log(process.env)
// {NODE_ENV: "production", PUBLIC_URL: "", WDS_SOCKET_HOST: undefined, WDS_SOCKET_PATH: undefined, WDS_SOCKET_PORT: undefined, …}

console.log(process.env.MY_VAR)
// undefined
Vivere
  • 1,919
  • 4
  • 16
  • 35
  • That looks right – Software Engineer Apr 25 '21 at 16:06
  • It looks but it doesn't work. From what I saw online, `process.env` is `node-specifi`c. My app is not really a `node` app, so I guess that's why it isn't working? Or maybe because I'm running it with [serve](https://www.npmjs.com/package/serve)? But I figured, `serve` uses `node` under the hood... – Vivere Apr 25 '21 at 16:09
  • What do you mean, it's not really a node app? AFAIK, serve is fine -- I use it myself with compose and environment variables. – Software Engineer Apr 25 '21 at 16:11
  • Also, try `from node:current-buster-slim` – Software Engineer Apr 25 '21 at 16:12
  • Well, it's not a node server, it's an Ionic w/ React client. I suppose there is a difference? I'm not really sure. I'll try `from node:current-buster-slim` – Vivere Apr 25 '21 at 16:14
  • Not working either, I'm simply clueless. – Vivere Apr 25 '21 at 16:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231585/discussion-between-vivere-and-software-engineer). – Vivere Apr 25 '21 at 18:12
  • 1
    https://stackoverflow.com/questions/66570279/404-error-when-trying-to-fetch-json-file-from-public-folder-in-deployed-create-r/66577247#66577247 – Jason Pan Apr 27 '21 at 08:45
  • Any luck on this? I'm running my Ionic React app in a docker container as well, and can't figure out why my env variables aren't being read. Based on your comments, it looks like it might be because I've containerized using Nginx to serve my app. Anyway, good luck! – Bennybear Apr 30 '21 at 21:03

0 Answers0