devcontainers / devcontainers/features

I would like to create a feature that adds some npm packages, yet when I try that I get npm not found

Offen
#763 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Shell
Sterne
1.5k
Forks
621
Ø Merge
6 T. 53 Min.
Gemergte PRs (30 T.)
9

Beschreibung

I am trying to create a local/private feature, I used to run those npm install as part of the postCreateCommand, but I need to create a standalone image and would like to use devcontainer to do so.

Since the image will be instatiated from docker run, it will not trigger post-create scripts, as such I was to install some npm package within the image before it gets created.

My feature is using InstallAfter common-utils and node, it then run npm as su -c ${USERNAME} and yet npm is not found, I tried to use the hard-coded path directly still the same issue.

```
{
"id": "myfeature",
"version": "0.0.1",
"name": "MyFeature",
"installAfter": [
"ghcr.io/devcontainers/features/common-utils",
"ghcr.io/devcontainers/features/node"
]
}
```

my install script

```
#!/bin/bash

USERNAME="${USERNAME:-"${_REMOTE_USER:-"automatic"}"}"

set -e

if [ "$(id -u)" -ne 0 ]; then
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
exit 1
fi

# Ensure that login shells get the correct path if the user updated the PATH using ENV.
rm -f /etc/profile.d/00-restore-env.sh
echo "export PATH=${PATH//$(sh -lc 'echo $PATH')/\$PATH}" > /etc/profile.d/00-restore-env.sh
chmod +x /etc/profile.d/00-restore-env.sh

# Determine the appropriate non-root user
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
USERNAME=""
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
for CURRENT_USER in "${POSSIBLE_USERS[@]}"; do
if id -u ${CURRENT_USER} > /dev/null 2>&1; then
USERNAME=${CURRENT_USER}
break
fi
done
if [ "${USERNAME}" = "" ]; then
USERNAME=root
fi
elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then
USERNAME=root
fi

su ${USERNAME} -c "/usr/local/share/nvm/versions/node/v20.9.0/bin/npm install -g somepublicnpmpackages" 2>&1
```

if I run my image using docker run I can find that the npm program is located at /usr/local/share/nvm/versions/node/v20.9.0/bin/ so I tried hard-coded that path but somehow that didn't work.

I would appreciate any guidance here

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne mit dem im Issue gezeigten Feature-Installationsskript und vergleiche die Umgebung des node-Features während der Feature-Installation mit der Umgebung im resultierenden Image. Reproduziere die npm-Suche unter `su` und mit `docker run`; abgeschlossen ist die Aufgabe, wenn das Feature die angeforderten npm-Pakete installieren kann, ohne dass post-create-Skripte ausgeführt werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
node.js, shell
Bereich
devops, tooling
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.