Question

Passing environment variables during build with dockerfile + sveltekit + vite

I’m trying to deploy my app on digitalocean and i’m have couple env variables im trying to use. I have added them in the settings of my app to the App-Level Environment Variables. However during build i see:


[2024-08-22 16:00:24] │ ✓ 203 modules transformed.
[2024-08-22 16:00:24] │ x Build failed in 1.68s
[2024-08-22 16:00:24] │ error during build:
[2024-08-22 16:00:24] │ src/lib/meilisearch/index.ts (1:30): "MEILISEARCH_HOST" is not exported by "virtual:$env/static/private", imported by "src/lib/meilisearch/index.ts".
[2024-08-22 16:00:24] │ file: /app/src/lib/meilisearch/index.ts:1:30
[2024-08-22 16:00:24] │ 
[2024-08-22 16:00:24] │ 1: import { MEILISEARCH_API_KEY, MEILISEARCH_HOST } from "$env/static/private";
[2024-08-22 16:00:24] │                                  ^
[2024-08-22 16:00:24] │ 2: import { Meilisearch } from "meilisearch";
[2024-08-22 16:00:24] │ 
[2024-08-22 16:00:24] │     at getRollupError (file:///app/node_modules/rollup/dist/es/shared/parseAst.js:392:41)
[2024-08-22 16:00:24] │     at error (file:///app/node_modules/rollup/dist/es/shared/parseAst.js:388:42)
[2024-08-22 16:00:24] │     at Module.error (file:///app/node_modules/rollup/dist/es/shared/node-entry.js:13958:16)
[2024-08-22 16:00:24] │     at Module.traceVariable (file:///app/node_modules/rollup/dist/es/shared/node-entry.js:14406:29)
[2024-08-22 16:00:24] │     at ModuleScope.findVariable (file:///app/node_modules/rollup/dist/es/shared/node-entry.js:12110:39)
[2024-08-22 16:00:24] │     at Identifier.bind (file:///app/node_modules/rollup/dist/es/shared/node-entry.js:6941:40)
[2024-08-22 16:00:24] │     at Property.bind (file:///app/node_modules/rollup/dist/es/shared/node-entry.js:4808:23)
[2024-08-22 16:00:24] │     at ObjectExpression.bind (file:///app/node_modules/rollup/dist/es/shared/node-entry.js:4804:28)
[2024-08-22 16:00:24] │     at NewExpression.bind (file:///app/node_modules/rollup/dist/es/shared/node-entry.js:4804:28)
[2024-08-22 16:00:24] │     at VariableDeclarator.bind (file:///app/node_modules/rollup/dist/es/shared/node-entry.js:4808:23)
[2024-08-22 16:00:24] │ error building image: error building stage: failed to execute command: waiting for process to exit: exit status 1
[2024-08-22 16:00:24] │ 
[2024-08-22 16:00:24] │ command exited with code 1
[2024-08-22 16:00:24] │ 
[2024-08-22 16:00:24] │  ✘ build failed


i tried modyfing my dockerfile by adding:

ENV MEILISEARCH_API_KEY=$MEILISEARCH_API_KEY ENV MEILISEARCH_HOST=$MEILISEARCH_HOST …

etc.

however that doesn’t work, i also tried to define those as ARG to have them accessible during build but no luck there either.

Can someone help me?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
August 23, 2024

Hey!

I think that when using SvelteKit with Vite, environment variables used during the build need to be prefixed with VITE_ if they are to be accessible in the client-side code.

Here’s what you can do:

  1. Instead of using MEILISEARCH_API_KEY and MEILISEARCH_HOST, rename them to VITE_MEILISEARCH_API_KEY and VITE_MEILISEARCH_HOST in your DigitalOcean App Platform settings.

  2. In your code, import these variables using import.meta.env.VITE_MEILISEARCH_API_KEY and import.meta.env.VITE_MEILISEARCH_HOST.

Also as a side note, to pass environment variables to a Dockerfile-based resource, define them as build-time or run-time environment variables in App Platform. This passes variables down to the docker build process (with a --build-arg parameter) and docker run process (with an -e parameter) when App Platform builds and deploys your container. You can then access the values of the environment variables using the ARG keyword in your Dockerfile as you normally would.

Let me know how it goes.

- Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.