Description
Hello,
I'm writing a feature which requires writing configuration files to the home directory of the user within the container.
Looking at the source code of existing implemented features as a reference, such as the anaconda
feature, I encountered code such as the following, which has specific logic to determine the user (and thus the home directory location).
I wasn't able to find specific documentation in the proposal on a "contract" for feature developers; with details on a USERNAME
environment variable being provided when executing the install.sh
script. Furthermore, use of "magical" users such as automatic
, auto
, vscode
, node
, codespace
or other (via /etc/passwd
) to aid in the determination behaviour is something I wouldn't want to have to repeat in authoring my features.
Furthermore, the reliance on having one of these user exist in a given base image is tenuous and makes this logic quite fragile (and smelly).
Therefore I propose a better and surprisingly much simpler solution, inspired in part by how the vagrant
shell provisioner works, as follows:
-
The process which is executing the
install.sh
script, has access to the givendevcontainer.json
file, which defines aremoteUser
and thus can be used to provide theUSERNAME
environment variable to the script; sort of like a docker build argument, which would remove the need to have to infer the correct user. -
Since the feature
install.sh
script is typically executed asroot
, unless the base image switches the user, files installed to the home directory need to bechown
ed to make the files accessible to the user when the dev container is running. This can be solved by having an optionaldevcontainer-feature.json
property, such asinstallAs
, which is an enum type with values ofroot
orremoteUser
. WhenremoteUser
is provided,install.sh
is executed in the context of the user provided by theremoteUser
property. This is similar in concept to how theprivileged
setting of thevagrant
shell provisioner alters the context. This does place a limitation on what theinstall.sh
script can do, so authors should include an assertion for root if required for proper execution. -
To aid troubleshooting, since the onus is on the
devcontainer.json
author to ensure alignment of the user(s) contained in the base image and theremoteUser
provided, a check should be performed before installing features, to assert that the givenremoteUser
exists and a nice helpful message if it doesn't.