Skip to content

Features: "User" and Installs to User Home Directory #91

Closed
@virtualstaticvoid

Description

@virtualstaticvoid

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).

https://github.com/devcontainers/features/blob/a5fbdab3c096152dd3a318c0c23da9465815aa69/src/anaconda/install.sh#L29-L44

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:

  1. The process which is executing the install.sh script, has access to the given devcontainer.json file, which defines a remoteUser and thus can be used to provide the USERNAME environment variable to the script; sort of like a docker build argument, which would remove the need to have to infer the correct user.

  2. Since the feature install.sh script is typically executed as root, unless the base image switches the user, files installed to the home directory need to be chowned to make the files accessible to the user when the dev container is running. This can be solved by having an optional devcontainer-feature.json property, such as installAs, which is an enum type with values of root or remoteUser. When remoteUser is provided, install.sh is executed in the context of the user provided by the remoteUser property. This is similar in concept to how the privileged setting of the vagrant shell provisioner alters the context. This does place a limitation on what the install.sh script can do, so authors should include an assertion for root if required for proper execution.

    https://github.com/devcontainers/features/blob/a5fbdab3c096152dd3a318c0c23da9465815aa69/src/terraform/install.sh#L35-L38

  3. 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 the remoteUser provided, a check should be performed before installing features, to assert that the given remoteUser exists and a nice helpful message if it doesn't.

Metadata

Metadata

Assignees

Labels

finalizationProposal to be made part of the specproposalStill under discussion, collecting feedback

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions