npm package: @gitzone/tsdocker
Description: develop npm modules cross platform with docker
Author: Lossless GmbH
Latest version: 1.2.42

# @gitzone/tsdocker

> 🐳 Cross-platform npm module development with Docker — test your packages in clean, reproducible Linux environments every time.

## Issue Reporting and Security

For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.

## What is tsdocker?

**tsdocker** provides containerized testing environments for npm packages, ensuring your code works consistently across different systems. It's perfect for:

- 🧪 **Testing in clean environments** — Every test run starts fresh, just like CI
- 🔄 **Reproducing CI behavior locally** — No more "works on my machine" surprises
- 🐧 **Cross-platform development** — Develop on macOS/Windows, test on Linux
- 🚀 **Quick validation** — Spin up isolated containers for testing without polluting your system

## Features

✨ **Works Everywhere Docker Does**
- Docker Toolbox
- Native Docker Desktop
- Docker-in-Docker (DinD)
- Mounted docker.sock scenarios

🔧 **Flexible Configuration**
- Custom base images
- Configurable test commands
- Environment variable injection via qenv
- Optional docker.sock mounting for nested container tests

📦 **TypeScript-First**
- Full TypeScript support with excellent IntelliSense
- Type-safe configuration
- Modern async/await patterns throughout

## Installation

```bash
npm install --save-dev @gitzone/tsdocker
# or
pnpm install --save-dev @gitzone/tsdocker
```

## Quick Start

### 1. Configure Your Project

Create an `npmextra.json` file in your project root:

```json
{
  "npmdocker": {
    "baseImage": "hosttoday/ht-docker-node:npmts",
    "command": "npmci test stable",
    "dockerSock": false
  }
}
```

### 2. Run Your Tests

```bash
npx tsdocker
```

That's it! tsdocker will:
1. ✅ Verify Docker is available
2. 🏗️ Build a test container with your specified base image
3. 📂 Mount your project directory
4. 🚀 Execute your test command
5. 🧹 Clean up automatically

## Configuration Options

| Option | Type | Description |
|--------|------|-------------|
| `baseImage` | `string` | Docker image to use as the test environment base |
| `command` | `string` | CLI command to execute inside the container |
| `dockerSock` | `boolean` | Whether to mount `/var/run/docker.sock` for Docker-in-Docker scenarios |

### Environment Variables

If you have a `qenv.yml` file in your project, tsdocker automatically loads and injects those environment variables into your test container.

Example `qenv.yml`:
```yaml
demoKey: demoValue
API_KEY: your-key-here
```

## CLI Commands

### Standard Test Run
```bash
tsdocker
```
Runs your configured test command in a fresh Docker container.

### Clean Docker Environment
```bash
tsdocker clean --all
```
⚠️ **WARNING**: This aggressively cleans your Docker environment by:
- Killing all running containers
- Removing all stopped containers
- Removing dangling images
- Removing all images
- Removing dangling volumes

Use with caution!

### VSCode in Docker
```bash
tsdocker vscode
```
Launches a containerized VS Code instance accessible via browser at `testing-vscode.git.zone:8443`.

### Speed Test
```bash
tsdocker speedtest
```
Runs a network speed test inside a Docker container.

## Advanced Usage

### Docker-in-Docker Testing

If you need to run Docker commands inside your test container (e.g., testing Docker-related tools):

```json
{
  "npmdocker": {
    "baseImage": "docker:latest",
    "command": "docker run hello-world",
    "dockerSock": true
  }
}
```

Setting `"dockerSock": true` mounts the host's Docker socket into the container.

### Custom Base Images

You can use any Docker image as your base:

```json
{
  "npmdocker": {
    "baseImage": "node:20-alpine",
    "command": "npm test"
  }
}
```

Popular choices:
- `node:20` — Official Node.js images
- `node:20-alpine` — Lightweight Alpine-based images
- `hosttoday/ht-docker-node:npmts` — Pre-configured with npmts tooling

### CI Integration

tsdocker automatically detects CI environments (via `CI=true` env var) and adjusts behavior:
- Skips mounting project directory in CI (assumes code is already in container)
- Optimizes for CI execution patterns

## Why tsdocker?

### The Problem

Local development environments drift over time. You might have:
- Stale global packages
- Modified system configurations
- Cached dependencies
- Different Node.js versions

Your tests pass locally but fail in CI — or vice versa.

### The Solution

tsdocker ensures every test run happens in a **clean, reproducible environment**, just like your CI pipeline. This means:

✅ Consistent behavior between local and CI
✅ No dependency pollution between test runs
✅ Easy cross-platform testing
✅ Reproducible bug investigations

## TypeScript Usage

tsdocker is built with TypeScript and provides full type definitions:

```typescript
import { IConfig } from '@gitzone/tsdocker/dist/tsdocker.config';

const config: IConfig = {
  baseImage: 'node:20',
  command: 'npm test',
  dockerSock: false,
  keyValueObject: {
    NODE_ENV: 'test'
  }
};
```

## Requirements

- **Docker**: Docker must be installed and accessible via CLI
- **Node.js**: Version 14 or higher recommended

## How It Works

Under the hood, tsdocker:

1. 📋 Reads your `npmextra.json` configuration
2. 🔍 Optionally loads environment variables from `qenv.yml`
3. 🐳 Generates a temporary Dockerfile
4. 🏗️ Builds a Docker image with your base image
5. 📦 Mounts your project directory (unless in CI)
6. ▶️ Runs your test command inside the container
7. 📊 Captures the exit code
8. 🧹 Cleans up containers and images
9. ✅ Exits with the same code as your tests

## Troubleshooting

### "docker not found on this machine"

Make sure Docker is installed and the `docker` command is in your PATH:

```bash
docker --version
```

### Tests fail in container but work locally

This often indicates environment-specific issues. Check:
- Are all dependencies in `package.json`? (not relying on global packages)
- Does your code have hardcoded paths?
- Are environment variables set correctly?

### Permission errors with docker.sock

If using `dockerSock: true`, ensure your user has permissions to access `/var/run/docker.sock`:

```bash
sudo usermod -aG docker $USER
# Then log out and back in
```

## Examples

### Basic npm test
```json
{
  "npmdocker": {
    "baseImage": "node:20",
    "command": "npm test"
  }
}
```

### Using npmci for multiple Node versions
```json
{
  "npmdocker": {
    "baseImage": "hosttoday/ht-docker-node:npmts",
    "command": "npmci test stable"
  }
}
```

### Testing Docker-based tools
```json
{
  "npmdocker": {
    "baseImage": "docker:latest",
    "command": "sh -c 'docker version && docker ps'",
    "dockerSock": true
  }
}
```

## Performance Tips

🚀 **Use specific base images**: `node:20-alpine` is much faster to pull than `node:latest`
🚀 **Layer caching**: Docker caches image layers — your base image only downloads once
🚀 **Prune regularly**: Run `docker system prune` periodically to reclaim disk space

## Migration from @gitzone/npmdocker

This package was previously published as `@gitzone/npmdocker`. The scope has been updated to `@gitzone/tsdocker` for better naming consistency. Functionality remains the same.

## License and Legal Information

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.

**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

### Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

### Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.