devcontainers / cli

A reference implementation for the specification that can create and configure a dev container from a devcontainer.json.
https://containers.dev
MIT License
1.38k stars 192 forks source link

Evaluating Local Environment Variables With Prebuilds #819

Open jcarpinelli-bdai opened 2 months ago

jcarpinelli-bdai commented 2 months ago

I have a private feature which creates and adds a user to the underlying container. The feature passes all of its tests; it definitely correctly creates a user. I've published the feature image to the GitHub container registry.

When I use this feature with a Dev Container, it works fine. For some reason, when I pre-build and push the Dev Container's image to GHCR, the user is no longer present in the pre-built image.

When I look at the build logs, the feature is definitely being installed. For some reason, all of the "work" that feature does is not present in the pre-built image.

Do you have any suggestions for troubleshooting this issue?

jcarpinelli-bdai commented 2 months ago

For context, the image is being published with a namespace "organization/devcontainers/project", where organization and project are replaced with specific values. Is having multiple forward slashes an issue?

jcarpinelli-bdai commented 2 months ago

I think I found the issue... I had thought that ${localENV:...} variables were evaluated on each individual user's machine. It appears that localEnv is evaluated when images are pre-built. Is there any way to prebuild an image that looks for local environment variables when the image is used?

chrmarti commented 2 months ago

I think I found the issue... I had thought that ${localENV:...} variables were evaluated on each individual user's machine. It appears that localEnv is evaluated when images are pre-built. Is there any way to prebuild an image that looks for local environment variables when the image is used?

You could add an ENTRYPOINT or CMD to the Dockerfile that runs when the container is started. Make sure to use "overrideCommand": false to get it to run. You could use "containerEnv": { } to pass values to it.

jcarpinelli-bdai commented 2 months ago

Could I pre-build the image, and have a user of the image specify "containerEnv": {"USERNAME": "${localEnv:USER}"}"?