composerize-np 0.2.0

Convert docker run commands to docker-compose files (YAML/JSON) and convert between formats
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# composerize-np


Rust version of [composerize](https://github.com/magicmark/composerize) - a powerful tool for:
- 🐳 Converting `docker run` commands to `docker-compose.yaml` or JSON files
- 🔄 Converting between YAML ↔ JSON formats
- ⚡ Fast operation, single executable, no dependencies

## Quick Start


```bash
# Simplest way - just pass the docker command

composerize-np "docker run -p 80:80 nginx"

# Save to YAML file (docker-compose.yml)

composerize-np "docker run -p 80:80 nginx" -o

# ⚠️ For long/complex commands - use a file!

# This solves escaping and length limitation issues

composerize-np --from-file my-docker-command.txt -o

# Save to JSON file (docker-compose.json)

composerize-np "docker run -p 80:80 nginx" --output-format json -o

# Convert YAML to JSON

composerize-np yaml-to-json docker-compose.yml -o docker-compose.json
```

> **⚠️ IMPORTANT for PowerShell:** If the command contains `||`, `&&`, `|` or other special characters, use **double double quotes** `""` around the problematic value:
> ```powershell
> # ❌ DOESN'T WORK in PowerShell (fails on ||)
> composerize-np "docker run --health-cmd='curl || exit 1' nginx"
> 
> # ✅ WORKS: Use double double quotes ""
> composerize-np "docker run --health-cmd=""curl || exit 1"" nginx"
> 
> # ✅ OR use --from-file (RECOMMENDED for very long commands)
> # 1. Save command to command.txt file
> # 2. Run:
> composerize-np --from-file command.txt -o
> ```

## Installation


### As a CLI tool


```bash
# Install from crates.io (after publication)

cargo install composerize-np

# Or build from source

cd composerize-np
cargo build --release
```

The executable will be in `target/release/composerize-np` (or `composerize-np.exe` on Windows).

### As a library


Add to your `Cargo.toml`:

```toml
[dependencies]
composerize-np = "0.1"
```

**Quick example:**

```rust
use composerize_np::composerize;

fn main() {
    let docker_command = "docker run -d -p 80:80 nginx";
    
    match composerize(docker_command, "", "latest", 2) {
        Ok(yaml) => println!("{}", yaml),
        Err(e) => eprintln!("Error: {}", e),
    }
}
```

See [EXAMPLES.md](EXAMPLES.md) for more examples and API documentation.

## Usage


### 1. Converting docker run to compose


There are two ways to use it:

#### Option A: Simple way (direct command)

**Fastest and most convenient for manual use**

Just pass the docker command directly:

```bash
# YAML format (default)

composerize-np "docker run -p 80:80 nginx"

# JSON format (output to console)

composerize-np "docker run -p 80:80 nginx" --output-format json

# Save YAML to file (docker-compose.yml by default)

composerize-np "docker run -p 80:80 nginx" -o

# Save YAML to custom file

composerize-np "docker run -p 80:80 nginx" -o my-compose.yml

# Save JSON to file (docker-compose.json by default)

composerize-np "docker run -p 80:80 nginx" --output-format json -o

# Save JSON to custom file

composerize-np "docker run -p 80:80 nginx" --output-format json -o compose.json

# With formatting parameters

composerize-np "docker run -p 80:80 nginx" -f v3x -i 4 -o
```

**When to use:**
- ✅ Quick conversion in terminal
- ✅ Compatibility with original composerize version
- ✅ Simple one-line commands

#### Option B: With `run` subcommand

**Recommended for scripts and automation**

Use explicit `run` subcommand:

```bash
# YAML format

composerize-np run "docker run -p 80:80 nginx"

# JSON format

composerize-np run "docker run -p 80:80 nginx" --output-format json

# With parameters

composerize-np run "docker run -p 80:80 nginx" -f v3x -i 4 -o compose.yaml
```

**When to use:**
- ✅ In scripts and CI/CD
- ✅ When explicit command is needed for readability
- ✅ When using other subcommands (yaml-to-json, json-to-yaml)

**Both options work identically!** Choose whichever is more convenient for you.

| Criteria | Direct command | With `run` |
|----------|----------------|------------|
| Command length | Shorter | Longer |
| Readability in scripts | Medium | High |
| Compatibility with original | ✅ Yes | ❌ No |
| Intent clarity | Medium | High |
| Recommended for | Manual use | Scripts, CI/CD |

### 2. Converting YAML to JSON


```bash
# Output to console

composerize-np yaml-to-json docker-compose.yml

# Save to file

composerize-np yaml-to-json docker-compose.yml -o docker-compose.json

# Without pretty-print

composerize-np yaml-to-json docker-compose.yml -o output.json --pretty false
```

### 3. Converting JSON to YAML


```bash
# Output to console

composerize-np json-to-yaml docker-compose.json

# Save to file

composerize-np json-to-yaml docker-compose.json -o docker-compose.yml
```

### 4. Automatic conversion


The `convert` command automatically determines format by file extension:

```bash
# YAML → JSON

composerize-np convert docker-compose.yml -o docker-compose.json

# JSON → YAML

composerize-np convert docker-compose.json -o docker-compose.yml
```

### Formatting parameters


```bash
# -f, --format: Docker Compose version (latest, v3x, v2x)

composerize-np "docker run nginx" -f v3x    # Adds version: '3'
composerize-np "docker run nginx" -f v2x    # Adds version: '2'
composerize-np "docker run nginx" -f latest # No version (default)

# -i, --indent: Number of spaces for indentation (default 2)

composerize-np "docker run nginx" -i 4      # 4 spaces instead of 2

# Combination of parameters

composerize-np "docker run -p 80:80 nginx" -f v3x -i 4 -o compose.yml
```

### Help


```bash
# General help

composerize-np --help

# Command help

composerize-np run --help
composerize-np yaml-to-json --help
composerize-np json-to-yaml --help
composerize-np convert --help
```

## Examples


### Simple Nginx web server


```bash
# Option 1: Direct command (shorter)

composerize-np "docker run -d -p 80:80 nginx"

# Option 2: With subcommand (more explicit)

composerize-np run "docker run -d -p 80:80 nginx"
```

Result:
```yaml
services:
  nginx:
    image: nginx
    ports:
      - 80:80
```

### MySQL with environment variables (JSON)


```bash
composerize-np run "docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=secret mysql:8" --output-format json
```

Result:
```json
{
  "services": {
    "mysql": {
      "image": "mysql:8",
      "ports": ["3306:3306"],
      "environment": ["MYSQL_ROOT_PASSWORD=secret"]
    }
  }
}
```

### Converting existing compose file


```bash
# Have docker-compose.yml, need JSON

composerize-np yaml-to-json docker-compose.yml -o docker-compose.json

# Edit JSON...


# Convert back to YAML

composerize-np json-to-yaml docker-compose.json -o docker-compose.yml
```

## Supported Flags


Full support for all major Docker flags:

### Network and ports

- `-p, --publish` - ports
- `--expose` - expose ports
- `--network, --net` - network
- `--network-alias` - network aliases
- `--ip` - IPv4 address
- `--ip6` - IPv6 address
- `--link` - container links
- `--add-host` - additional hosts
- `--dns` - DNS servers
- `--dns-search` - DNS search domains
- `--dns-opt` - DNS options
- `--mac-address` - MAC address

### Volumes and filesystem

- `-v, --volume` - volumes
- `--mount` - mount
- `--volumes-from` - volumes from other containers
- `--tmpfs` - tmpfs mount
- `--read-only` - read-only
- `--workdir, -w` - working directory

### Environment and configuration

- `-e, --env` - environment variables
- `--env-file` - environment file
- `--name` - container name
- `--hostname, -h` - hostname
- `--domainname` - domain name
- `--user, -u` - user
- `--label, -l` - labels
- `--entrypoint` - entrypoint
- `--platform` - platform

### Resources

- `--memory, -m` - memory limit
- `--memory-swap` - swap limit
- `--memory-reservation` - memory reservation
- `--memory-swappiness` - swappiness
- `--cpus` - CPU count
- `--cpu-shares, -c` - CPU shares
- `--cpu-period` - CPU period
- `--cpu-quota` - CPU quota
- `--cpu-rt-period` - CPU RT period
- `--cpu-rt-runtime` - CPU RT runtime
- `--pids-limit` - process limit
- `--ulimit` - ulimits
- `--device` - devices
- `--gpus` - GPU

### Security and privileges

- `--privileged` - privileged mode
- `--cap-add` - add capabilities
- `--cap-drop` - drop capabilities
- `--security-opt` - security options
- `--userns` - user namespace
- `--group-add` - additional groups

### Behavior and policies

- `--restart` - restart policy
- `-d, --detach` - run in background
- `-t, --tty` - allocate pseudo-TTY
- `-i, --interactive` - interactive mode
- `--init` - use init
- `--rm` - remove after stop
- `--pull` - pull policy
- `--stop-signal` - stop signal
- `--stop-timeout` - stop timeout

### Logging

- `--log-driver` - logging driver
- `--log-opt` - logging options

### Healthcheck

- `--health-cmd` - health check command
- `--health-interval` - check interval
- `--health-retries` - retry count
- `--health-timeout` - check timeout
- `--health-start-period` - start period
- `--no-healthcheck` - disable healthcheck

### Other

- `--cgroup-parent` - parent cgroup
- `--cgroupns` - cgroup namespace
- `--ipc` - IPC mode
- `--pid` - PID mode
- `--uts` - UTS namespace
- `--isolation` - isolation
- `--runtime` - runtime
- `--shm-size` - /dev/shm size
- `--sysctl` - sysctl parameters
- `--storage-opt` - storage options
- `--blkio-weight` - Block IO weight
- `--device-read-bps` - device read limit
- `--device-write-bps` - device write limit
- `--device-read-iops` - read IOPS limit
- `--device-write-iops` - write IOPS limit
- `--oom-kill-disable` - disable OOM killer
- `--oom-score-adj` - OOM score adjustment

## Automatic Networks and Volumes Sections


`composerize-np` automatically creates `networks:` and `volumes:` sections at the root of the compose file for used resources:

### Networks

- Automatically adds `networks:` section for custom networks
- Marks them as `external: true` (assumes network is created beforehand)
- Ignores standard networks: `default`, `bridge`, `host`, `none`

```bash
composerize-np "docker run --network ml-net nginx"
```

Result:
```yaml
services:
  nginx:
    networks:
      ml-net: {}
    image: nginx
networks:
  ml-net:
    external: true
```

### Volumes

- Automatically adds `volumes:` section for named volumes
- Does **NOT** add bind mounts (paths starting with `/`, `.`, `~`)
- Named volumes are declared as `null` (created by Docker automatically)

```bash
composerize-np "docker run -v data:/data -v cache:/cache -v /host:/host nginx"
```

Result:
```yaml
services:
  nginx:
    volumes:
    - data:/data
    - cache:/cache
    - /host:/host
    image: nginx
volumes:
  data: null
  cache: null
```

### Full example with GPU, networks and volumes


```bash
composerize-np "docker run -d \
  --name ml-gpu \
  --network ml-net \
  --gpus all \
  -v ml-models:/models \
  -v ml-cache:/cache \
  --tmpfs /tmp:rw,size=256m \
  --health-cmd='curl -f http://localhost:5000/health || exit 1' \
  --health-interval=20s \
  -p 5000:5000 \
  tensorflow/tensorflow:latest-gpu"
```

Result - fully valid compose file:
```yaml
services:
  tensorflow:
    container_name: ml-gpu
    networks:
      ml-net: {}
    deploy:
      resources:
        reservations:
          devices:
          - driver: nvidia
            count: all
            capabilities:
            - gpu
    volumes:
    - ml-models:/models
    - ml-cache:/cache
    tmpfs:
    - /tmp:rw,size=256m
    healthcheck:
      test:
      - CMD-SHELL
      - curl -f http://localhost:5000/health || exit 1
      interval: 20s
    ports:
    - 5000:5000
    image: tensorflow/tensorflow:latest-gpu
networks:
  ml-net:
    external: true
volumes:
  ml-models: null
  ml-cache: null
```

This file fully complies with Docker Compose specification and is ready to use!

## Testing


The project has full test coverage:
- 41 unit tests (including networks/volumes tests)
- 18 integration tests

```bash
# Run all tests

cargo test

# Run with output

cargo test -- --nocapture
```

See [TESTING.md](TESTING.md) for detailed information.

## Development


```bash
# Clone repository

git clone https://github.com/yourusername/composerize-np
cd composerize-np

# Build

cargo build

# Run tests

cargo test

# Build release version

cargo build --release

# Install locally

cargo install --path .
```

## Documentation


### For CLI Users

- 📋 [CHEATSHEET.md]CHEATSHEET.md - Quick cheat sheet with command examples (including PowerShell tips)
-[FAQ.md]FAQ.md - Frequently Asked Questions

### For Library Users

- 📚 [LIBRARY_USAGE.md]LIBRARY_USAGE.md - Complete guide for using as a library
- 💡 [EXAMPLES.md]EXAMPLES.md - Detailed examples with explanations
- 🔧 [examples/]examples/ - Runnable code examples (`cargo run --example basic_usage`)
- 📖 [API Documentation]https://docs.rs/composerize-np - Full API reference (after publication)

## FAQ


Have questions? See [FAQ.md](FAQ.md) with answers to common questions:
- What's the difference between direct command and `run`?
- How to choose between YAML and JSON?
- How to convert between formats?
- Which flags are supported?
- And much more!

## License


MIT