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
# FAQ - Frequently Asked Questions


## General Questions


### What's the difference between `composerize-np "docker run..."` and `composerize-np run "docker run..."`?


**Short answer:** None! Both work identically.

**Details:**

1. **Direct command** (without `run`):
   ```bash
   composerize-np "docker run -p 80:80 nginx"

   ```
   - ✅ Shorter and faster to type
   - ✅ Compatible with original JavaScript version
   - ✅ Convenient for manual terminal use
   - ❌ Less explicit about what the command does

2. **With `run` subcommand**:
   ```bash
   composerize-np run "docker run -p 80:80 nginx"

   ```
   - ✅ Explicitly indicates converting docker run
   - ✅ Better for scripts and automation
   - ✅ Consistent with other subcommands (yaml-to-json, json-to-yaml)
   - ❌ Slightly longer

**Recommendation:**
- For manual work: use direct command
- For scripts/CI/CD: use `run`

### Why doesn't the command `composerize-np "docker run..."` work?


Make sure that:
1. Command is in quotes: `"docker run -p 80:80 nginx"`
2. You're using the latest version (with legacy mode support)
3. If it doesn't work, try with `run`: `composerize-np run "docker run..."`

### Why does the program crash/close on long commands in the command line?


**Short answer:** Use double double quotes `""` in PowerShell or `--from-file` for long commands!

**Quick fix for PowerShell:**
```powershell
# ❌ Doesn't work

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"
```

**Reasons for problems with direct input:**

1. **Command line length limitations:**
   - Windows CMD: ~8191 characters
   - PowerShell: ~32767 characters (but with escaping issues)
   - Your command may exceed these limits

2. **Special character escaping issues:**
   - Quotes: `"curl -f http://localhost || exit 1"`
   - Operators: `||`, `&&`, `|`, `&`, `;`
   - Special characters: `<`, `>`, `` ` ``, `'`, `=`, `,`
   - PowerShell/CMD try to interpret them BEFORE passing to the program

3. **Example problematic command:**
   ```bash
   # ❌ May crash in PowerShell

   composerize-np "docker run --health-cmd='curl http://localhost || exit 1' nginx"

   # PowerShell sees || and tries to process it as an operator

   ```

**Solution: use `--from-file`**

1. **Save command to file** (e.g., `command.txt`):
   
   Single-line format:
   ```
   docker run -d --name app -p 80:80 --health-cmd="curl http://localhost || exit 1" nginx
   ```
   
   Or Bash-style with line breaks:
   ```bash
   docker run -d \

     --name app \

     -p 80:80 \

     --health-cmd="curl http://localhost || exit 1" \

     nginx

   ```

2. **Use `--from-file`**:
   ```bash
   # Simple way (without run)

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

   
   # Or with subcommand

   composerize-np run --from-file command.txt -o

   ```

**Advantages of `--from-file`:**
- ✅ No length limitations
- ✅ No escaping issues
- ✅ All special characters work (`, ", ', $, ||, &&, etc.)
- ✅ Bash-style backslash support (`\`)
- ✅ Multi-line commands
- ✅ Shell doesn't see or interpret the content

**When to use direct input:**
- ✅ Short simple commands: `docker run -p 80:80 nginx`
- ✅ Commands without special characters
- ✅ Quick testing

**When to use `--from-file`:**
- ✅ Long commands (>100 characters)
- ✅ Commands with special characters (`||`, `&&`, quotes)
- ✅ Production commands with many parameters
- ✅ Commands from documentation/scripts

### How to choose between YAML and JSON?


**YAML (default):**
```bash
# Output to console

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

# Save to file

composerize-np "docker run -p 80:80 nginx" -o docker-compose.yml
```
- ✅ Standard Docker Compose format
- ✅ More human-readable
- ✅ Fewer characters

**JSON:**
```bash
# Output to console

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

# Save to file

composerize-np "docker run -p 80:80 nginx" --output-format json -o compose.json
```
- ✅ Easier to parse programmatically
- ✅ Strict structure
- ✅ Support in most programming languages

### How to save result directly to JSON file?


Use combination of `--output-format json` and `-o`:

```bash
# Save to docker-compose.json (automatic name)

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

# Save to custom file

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

# Full example with MySQL

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

**Important:** When using `-o` without filename:
- YAML format → `docker-compose.yml`
- JSON format → `docker-compose.json`

## Format Conversion


### How to convert existing docker-compose.yml to JSON?


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

### How to convert JSON back to YAML?


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

### Can I convert automatically by file extension?


Yes! Use the `convert` command:

```bash
# YAML → JSON

composerize-np convert input.yml -o output.json

# JSON → YAML

composerize-np convert input.json -o output.yml
```

## Working with Files


### How to save result to file?


```bash
# To docker-compose.yml (default)

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

# To custom file

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

### How to output to console instead of file?


Simply don't use the `-o` flag:

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

### Can I read from stdin?


Yes, if stdin is not TTY, the program will read existing compose file for merge:

```bash
cat existing-compose.yml | composerize-np "docker run -p 80:80 nginx"
```

## Docker Compose Formats


### Which Docker Compose versions are supported?


The `-f, --format` parameter determines the Docker Compose version:

- **`latest`** (default) - without version specification (Docker Compose Specification)
- **`v3x`** - Docker Compose v3
- **`v2x`** - Docker Compose v2

```bash
# Without version (modern format)

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

# services:

#   nginx:

#     image: nginx

#     ports:

#       - 80:80


# Version 3

composerize-np "docker run -p 80:80 nginx" -f v3x
# Result:

# version: '3'

# services:

#   nginx:

#     image: nginx

#     ports:

#       - 80:80


# Version 2

composerize-np "docker run -p 80:80 nginx" -f v2x
# Result:

# version: '2'

# services:

#   nginx:

#     image: nginx

#     ports:

#       - 80:80

```

### Which format to choose?


- **`latest`** - for new projects (recommended)
  - ✅ Modern standard
  - ✅ Supported by Docker Compose v2+
  - ✅ No version specification required

- **`v3x`** - for Docker Compose v3 compatibility
  - ✅ Wide support
  - ✅ Stable format
  - ⚠️ Some features deprecated

- **`v2x`** - for old projects on Docker Compose v2
  - ⚠️ Deprecated format
  - ✅ Compatibility with legacy systems

### What is the `-i, --indent` parameter?


Specifies the number of spaces for indentation in YAML file:

```bash
# 2 spaces (default, YAML standard)

composerize-np "docker run nginx" -i 2

# 4 spaces (popular in some projects)

composerize-np "docker run nginx" -i 4
```

**Note:** In the current version, YAML always uses 2 spaces (serde_yaml standard). The `-i` parameter is kept for compatibility with the original version and future improvements.

**For JSON:** The `-i` parameter affects JSON output formatting:
```bash
# Compact JSON (no indentation)

composerize-np "docker run nginx" --output-format json -i 0

# JSON with indentation (default 2)

composerize-np "docker run nginx" --output-format json -i 2
```

## Flag Support


### Which Docker flags are supported?


**All** major Docker flags (80+) are supported:
- Ports, volumes, environment
- Memory, CPU, GPU
- Networks, healthcheck, logging
- Security, capabilities
- And much more

See full list in [README.md](README.md#supported-flags)

### What to do if a flag is not supported?


1. Check that you're using correct syntax
2. Look at the list of supported flags in README
3. Create an issue on GitHub with command example

## Performance


### How much faster is the Rust version?


- ⚡ Startup: ~10-50x faster (no Node.js overhead)
- 💾 Memory: ~10x less (~5 MB vs ~50 MB)
- 📦 Size: ~30x smaller (~3 MB vs ~100 MB with Node.js)

### Can I use it in CI/CD?

Yes! The Rust version is ideal for CI/CD:
- Fast startup
- Single executable file
- No dependencies
- Cross-platform

## Development


### How to build from source?


```bash
git clone https://github.com/yourusername/composerize-np
cd composerize-np
cargo build --release
```

### How to run tests?


```bash
cargo test
```

### How to add support for a new flag?


1. Add mapping in `src/mappings.rs`
2. Add test in `src/lib.rs` or `tests/integration_tests.rs`
3. Run `cargo test`

## Comparison with Original


### How is it different from the JavaScript version?


| Feature | JavaScript | Rust |
|---------|-----------|------|
| Basic flags |||
| All flags |||
| YAML output |||
| JSON output |||
| YAML ↔ JSON |||
| Speed | 🐌 ||
| Size | ~100 MB | ~3 MB |
| Dependencies | Node.js | None |

### Can I replace the JavaScript version?


Yes! The Rust version is fully compatible and has additional features:
- ✅ All flags supported
- ✅ Compatible CLI (direct command works)
- ✅ Additionally: JSON output and YAML↔JSON conversion
- ✅ Faster and lighter

## Troubleshooting


### Error "No image specified"


Make sure the command contains an image name:

```bash
# ❌ Wrong

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

# ✅ Correct

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

### Error "Unknown format"


Use one of the supported formats:
- `latest` (default)
- `v3x`
- `v2x`

```bash
composerize-np "docker run nginx" -f v3x
```

### Flag is ignored


Some flags (e.g., `--rm`, `-d`) are ignored as they have no equivalent in Docker Compose:
- `--rm` - containers in Compose are always removed on stop
- `-d` - containers in Compose always run in background

## Networks and Volumes


### Why do networks and volumes sections appear in the compose file?


`composerize-np` automatically creates `networks:` and `volumes:` sections for used resources to make the file fully valid and ready to use.

**Example:**
```bash
composerize-np "docker run --network ml-net -v data:/data nginx"
```

Result:
```yaml
services:
  nginx:
    networks:
      ml-net: {}
    volumes:
    - data:/data
    image: nginx
networks:
  ml-net:
    external: true
volumes:
  data: null
```

### What does `external: true` mean for networks?


This means the network must be created beforehand with:
```bash
docker network create ml-net
```

If you want Docker Compose to create the network automatically, remove `external: true` or replace with:
```yaml
networks:
  ml-net:
    driver: bridge
```

### Why don't bind mounts appear in the volumes section?


Bind mounts (paths starting with `/`, `.`, `~`) don't require declaration in the `volumes:` section as they reference existing paths in the host filesystem.

**Named volumes** (e.g., `data:/data`) require declaration so Docker knows to create the volume.

```bash
# Bind mount - will NOT appear in volumes section

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

# Named volume - will appear in volumes section

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

### How to disable automatic creation of networks/volumes sections?


In the current version, this behavior is enabled by default to create valid compose files. If you need to disable it, you can:

1. Manually remove sections from generated file
2. Use standard networks (`bridge`, `host`, `none`) - they won't appear in networks section

### Can I use multiple networks?


Yes! Just specify multiple `--network` flags:

```bash
composerize-np "docker run --network net1 --network net2 nginx"
```

Result:
```yaml
services:
  nginx:
    networks:
      net1: {}
      net2: {}
    image: nginx
networks:
  net1:
    external: true
  net2:
    external: true
```

## Additional Help


Didn't find an answer to your question?

- 📖 Read [README.md]README.md
- 🐛 Create an issue on GitHub
- 💬 Ask a question in Discussions