coollabsio / coolify

An open-source & self-hostable Heroku / Netlify / Vercel alternative.
https://coolify.io
Apache License 2.0
34.23k stars 1.86k forks source link

[Bug]: env variables not passed to docker container on nixpack build #3477

Open benjaminpreiss opened 1 month ago

benjaminpreiss commented 1 month ago

Error Message and Logs

[2024-Sep-18 11:22:40.393891] Oops something is not okay, are you okay? 😢

[2024-Sep-18 11:22:40.400550] #0 building with "default" instance using docker driver

#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 1.22kB done
#1 DONE 0.0s

#2 [internal] load metadata for ghcr.io/railwayapp/nixpacks:ubuntu-1725321821
#2 DONE 0.4s

#3 [internal] load .dockerignore
#3 transferring context: 2B done
#3 DONE 0.0s

#4 [stage-0 1/13] FROM ghcr.io/railwayapp/nixpacks:ubuntu-1725321821@sha256:0cec4cb236c32e6bc58b896fff852f629a7abad4e4f059840d96f0869b4be2fd
#4 DONE 0.0s

#5 [internal] load build context
#5 transferring context: 2.44MB 0.0s done
#5 DONE 0.1s

#6 [stage-0 2/13] WORKDIR /app/
#6 CACHED

#7 [stage-0 3/13] COPY .nixpacks/nixpkgs-e05605ec414618eab4a7a6aea8b38f6fbbcc8f08.nix .nixpacks/nixpkgs-e05605ec414618eab4a7a6aea8b38f6fbbcc8f08.nix
#7 CACHED

#8 [stage-0 4/13] RUN nix-env -if .nixpacks/nixpkgs-e05605ec414618eab4a7a6aea8b38f6fbbcc8f08.nix && nix-collect-garbage -d
#8 CACHED

#9 [stage-0 5/13] RUN sudo apt-get update && sudo apt-get install -y --no-install-recommends curl wget
#9 CACHED

#10 [stage-0 6/13] COPY . /app/.
#10 DONE 0.3s

#11 [stage-0 7/13] RUN echo "$NPM_TOKEN"
#11 0.147
#11 DONE 0.2s

#12 [stage-0 8/13] COPY . /app/.
#12 DONE 0.2s

Steps to Reproduce

1 Setup nixpacks.toml in your repo root:

# nixpacks.toml

[phases.setup]
nixPkgs = ['pnpm', 'infisical']
cmds = ['...', 'echo "$NPM_TOKEN"']

[phases.install]
cmds = ['pnpm i --ignore-scripts']

[phases.build]
cmds = ['pnpm build']

[start]
cmd = 'pnpm start'

2 Set NPM_TOKEN environment variable in coolify 3 Make sure the above steps are available 4 Push to git & deploy 5 Verify that the $NPM_TOKEN variable is not defined in the logs

Example Repository URL

No response

Coolify Version

v4.0.0-beta.323

Are you using Coolify Cloud?

Yes (Coolify Cloud)

Operating System and Version (self-hosted)

macOS 14.4.1 (23E224)

Additional Information

No response

tolstenko commented 1 month ago

I can confirm that on a docker build. I ran printenv, and the variables are not there at build time, even if I mark them as a build-time variable. But if I call printenv on the command-tab on the running container, it works. But I need the keys at compile time.

djsisson commented 1 month ago

can you link a repo or make an example, ive checked this so many times and never found an issue

nhCoder commented 1 month ago

True, not working with docker-compose deployment also

just deploy this (docker-compose) ad env variables

Enviromental variables inside docker like this are not passed from whenadded in Enviromental variables:


environment:
      - _APP_ENV
      - _APP_WORKER_PER_CORE
      - _APP_LOCALE
      - _APP_CONSOLE_WHITELIST_ROOT
      - _APP_CONSOLE_WHITELIST_EMAILS
      - _APP_CONSOLE_SESSION_ALERTS
      - _APP_CONSOLE_WHITELIST_IPS
      - _APP_CONSOLE_HOSTNAMES
      - _APP_SYSTEM_EMAIL_NAME
      - _APP_SYSTEM_EMAIL_ADDRESS
      - _APP_EMAIL_SECURITY
      - _APP_SYSTEM_RESPONSE_FORMAT
      - _APP_OPTIONS_ABUSE
      - _APP_OPTIONS_ROUTER_PROTECTION
djsisson commented 1 month ago

@nhCoder you need to assign them like this

environment:
    - '_APP_ENV=${_APP_ENV}'

the links you pasted dont work so i cant check

tolstenko commented 1 month ago

The only thing I did differently than the standard was manually set a specific docker file. Given I am using NESTJS SSR, I need variables at compile time, so I expect the env vars to be there if I check "Build Variables?" at the Environment Variables tab. But as you can see down below, I call printenv in the docker build and they are not there. But if I go to the terminal and call printenv again, they are there, so for now, I changed the CMD on the docker to call build before start, and it works, but it slowers the startup of the new version. image image image

nhCoder commented 1 month ago

@djsisson I have updated the links please check.. and Why we need to do like this '_APP_ENV=${_APP_ENV}' standard deployment with docker doesn't require that. Also Dokploy supports like i without assiging env var inside compose.

djsisson commented 1 month ago

@tolstenko did you add ARG API_HOST in your dockerfile etc

@nhCoder coolify parses the compose files first

tolstenko commented 1 month ago

@djsisson ehhrr... it is embarrassing... you are right. I'm sorry. It works with that.

For some reason, I thought Coolify would magically be in charge of that. Well, to help you not receive this type of issue anymore and help people like me who assume this same idea, it would be nice to have an information icon, explaining what we should do to make it work. Coolify is so nice that I just wrongly assumed that.

image

I guess you could transform this issue into either an improvement to have this (i) to remember people to setup dockerfile properly, or even better, create an automation to modify the dockerfile and inject ARGs on the build container (this might be harder on multi-stage builds). I can do the (i) in a PR. What do you think?

nhCoder commented 1 month ago

Then, what is the use of providing environmental variables separately

djsisson commented 1 month ago

Then, what is the use of providing environmental variables separately @nhCoder there is already a coolify appwrite template https://github.com/coollabsio/coolify/blob/main/templates/compose/appwrite.yaml

the env vars in the env vars tab, will get passed into whichever service requires them. if you need more info look at any of the docker compose templates

djsisson commented 1 month ago

@tolstenko i think it also depends on what image you are using to build, for instance a normal nextjs dockerfile using node 20 alpine you do not have to specify ARG at all it auto picks up all teh build args that are passed in

image image