1

I am trying to use docker compose environment variables for an API endpoint in my Svelte app, but it is undefined.

This does work for .env like this answer but i would like to pass it through a docker-compose environment

Docker compose file

services:
  frontend:
    build: ./frontend/ # For development
    container_name: frontend
    restart: always
    environment: 
      - API_URL: https://0.0.0.0/8000
    ports:
      - "5000:5000"
    volumes:
      - ~/uploader:/app/static/uploads
    depends_on:
      - backend

rollup.config

plugins: [
        replace({
            preventAssignment: true,
            __myapp:JSON.stringify({
                    env: {
                        isProd: production,
                        // ...config().parsed
                        API_URL: process.env.API_URL
                    }
                })
                // "API_URL_VAR", JSON.stringify(process.env.API_URL_VAR)
        }),

Svelte

<script lang="ts">
  console.log(__myapp.env.API_URL);
</script>

<h1 class="text-red-800 text-3xl font-bold p-3">Hello World!</h1>

1 Answers1

0

The problem is that the environment variables are evaluated and set during the build process of your image / your app(API_URL: process.env.API_URL), so passing the environment variables form docker compose will have no effect.

As a workaround, I used this which will allow you to set environment variables during build.