Improved code organization and maintainability: - Created image_support_files/ directory for Docker helper scripts - Moved bash-with-nvm and docker-entrypoint.sh to support directory - Added comprehensive README.md documenting each script's purpose - Updated Dockerfile to COPY from organized directory structure Benefits: - Cleaner repository structure - Scripts are now version-controlled files (not echo chains) - Easier to read, modify, and maintain - Self-documented with in-directory README No functional changes - all nvm functionality remains identical.
2.7 KiB
2.7 KiB
Image Support Files
This directory contains helper scripts that are copied into the Docker image during build to enable NVM functionality.
Files
bash-with-nvm
Purpose: Wrapper script for Dockerfile RUN commands
Usage: Set as SHELL directive in Dockerfile
SHELL ["/usr/local/bin/bash-with-nvm"]
What it does:
- Sources nvm before executing each Dockerfile RUN command
- Enables direct usage of
nvmcommands without manual sourcing - Example:
RUN nvm install 18works withoutbash -c "source ..."
Location in image: /usr/local/bin/bash-with-nvm
How it works:
- Exports NVM_DIR environment variable
- Sources
$NVM_DIR/nvm.shto load nvm functions - Executes the command passed to it via
eval "$@"
docker-entrypoint.sh
Purpose: ENTRYPOINT script for runtime nvm support
Usage: Set as ENTRYPOINT in Dockerfile
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
What it does:
- Makes nvm available in
docker run ... bash -ccommands - Enables nvm in CI/CD workflow scripts
- Passes through non-bash commands unchanged
Location in image: /usr/local/bin/docker-entrypoint.sh
How it works:
- Loads nvm in the entrypoint environment
- Detects if command is
bash -c ... - If yes: Injects
source /etc/bash.bashrcbefore the user's command - If no: Executes command as-is with
exec "$@"
Examples:
docker run image bash -c "nvm install 18"→ nvm is availabledocker run image node --version→ Passes through to node binary- CI/CD:
run: nvm install 19→ Works automatically
Why These Scripts Are Needed
The Problem:
- nvm is a bash function, not a binary executable
- It must be sourced into each shell session
- Docker RUN commands and
bash -cdon't automatically source it
The Solution:
- bash-with-nvm: Makes Dockerfile RUN commands work
- docker-entrypoint.sh: Makes runtime
bash -ccommands work - /etc/bash.bashrc (configured in Dockerfile): Makes interactive shells work
Together, these provide seamless nvm access in all contexts.
Maintenance
Modifying the scripts:
- Edit the file in this directory
- Rebuild the Docker image
- Test with both
docker buildanddocker runscenarios
Testing checklist:
RUN nvm install 18works in Dockerfiledocker run image bash -c "nvm install 18"worksdocker run image node --versionstill works (fallback)- CI/CD workflows can use nvm commands
Debugging:
- View script in running container:
docker run image cat /usr/local/bin/bash-with-nvm - Test entrypoint manually:
docker run --entrypoint /bin/bash image -c "nvm --version"