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