Files
ht-docker-node/image_support_files/README.md

91 lines
2.7 KiB
Markdown
Raw Normal View History

# 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
```dockerfile
SHELL ["/usr/local/bin/bash-with-nvm"]
```
**What it does**:
- Sources nvm before executing each Dockerfile RUN command
- Enables direct usage of `nvm` commands without manual sourcing
- Example: `RUN nvm install 18` works without `bash -c "source ..."`
**Location in image**: `/usr/local/bin/bash-with-nvm`
**How it works**:
1. Exports NVM_DIR environment variable
2. Sources `$NVM_DIR/nvm.sh` to load nvm functions
3. Executes the command passed to it via `eval "$@"`
---
### `docker-entrypoint.sh`
**Purpose**: ENTRYPOINT script for runtime nvm support
**Usage**: Set as ENTRYPOINT in Dockerfile
```dockerfile
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
```
**What it does**:
- Makes nvm available in `docker run ... bash -c` commands
- 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**:
1. Loads nvm in the entrypoint environment
2. Detects if command is `bash -c ...`
3. If yes: Injects `source /etc/bash.bashrc` before the user's command
4. If no: Executes command as-is with `exec "$@"`
**Examples**:
- `docker run image bash -c "nvm install 18"` → nvm is available
- `docker 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 -c` don't automatically source it
**The Solution**:
1. **bash-with-nvm**: Makes Dockerfile RUN commands work
2. **docker-entrypoint.sh**: Makes runtime `bash -c` commands work
3. **/etc/bash.bashrc** (configured in Dockerfile): Makes interactive shells work
Together, these provide seamless nvm access in all contexts.
---
## Maintenance
**Modifying the scripts**:
1. Edit the file in this directory
2. Rebuild the Docker image
3. Test with both `docker build` and `docker run` scenarios
**Testing checklist**:
- [ ] `RUN nvm install 18` works in Dockerfile
- [ ] `docker run image bash -c "nvm install 18"` works
- [ ] `docker run image node --version` still 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"`