@cloud-cli/dx 2.1.3 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,9 +14,19 @@ npm i @cloud-cli/dx
14
14
  cy dx.list
15
15
  cy dx.prune
16
16
  cy dx.pull --image node:latest
17
- cy dx.add --name node-app --image node:latest --volumes 'volume:/container/path' --port '8080' --domain 'foo.example.com'
18
- cy dx.start --name node-app --env 'FOO=bar'
17
+ cy dx.add --name node-app --image node:latest --volumes 'volume:/container/path' --port ':80' --domain 'foo.example.com'
18
+ cy dx.start --name node-app
19
19
  cy dx.logs --name node-app
20
20
  cy dx.stop --name node-app
21
21
  cy dx.remove --name node-app
22
22
  ```
23
+
24
+ ## Options
25
+
26
+ | Option | Description |
27
+ |-|-|
28
+ | name | Required. Container name |
29
+ | image | Required: Container image |
30
+ | volumes | Comma-separated list of volume bindings |
31
+ | port | Empty = random port. `:port` = random host port. `number` = fixed port on host and container |
32
+ | domain | Domain to bind this container to. Requires `@cloud-cli/px` and `@cloud-cli/dns` |
@@ -1,5 +1,5 @@
1
1
  import { exec } from '@cloud-cli/exec';
2
- import { getConfig } from '@cloud-cli/cli';
2
+ import { getConfig, logInfo, logError } from '@cloud-cli/cli';
3
3
  import { findContainer, listContainers } from './store.js';
4
4
  import { addExecFlag, getEnvVars, getListFromString, readTargetName } from './utils.js';
5
5
  import getPort from 'get-port';
@@ -62,6 +62,18 @@ export async function restartContainer(options, { run }) {
62
62
  await run('dx.start', { name });
63
63
  return true;
64
64
  }
65
+ async function getPorts(container) {
66
+ if (!container.port) {
67
+ const port = await getPort();
68
+ return [port, port];
69
+ }
70
+ if (container.port.startsWith(':')) {
71
+ const port = await getPort();
72
+ return [port, Number(container.port.slice(1))];
73
+ }
74
+ const port = Number(container.port);
75
+ return [port, port];
76
+ }
65
77
  export async function startContainer(options, { run }) {
66
78
  readTargetName(options);
67
79
  const { name } = options;
@@ -73,11 +85,11 @@ export async function startContainer(options, { run }) {
73
85
  throw new Error('Container not found: ' + name);
74
86
  }
75
87
  const vars = (await run('env.show', { name }));
76
- const port = String(container.port || (await getPort()));
77
- vars.push({ key: 'PORT', value: port });
88
+ const ports = await getPorts(container);
89
+ vars.push({ key: 'PORT', value: String(ports[1]) });
78
90
  const { env, envKeys } = await getEnvVars(vars);
79
91
  const volumes = addExecFlag(container.volumes.split(','), 'v');
80
- const ports = addExecFlag([`${port}:${port}`], 'p');
92
+ const portBindings = addExecFlag([ports.join(':')], 'p');
81
93
  if (container.domain) {
82
94
  const [domain] = container.domain.split('/');
83
95
  await run('px.update', { domain, target: 'http://localhost:' + env.PORT });
@@ -100,15 +112,18 @@ export async function startContainer(options, { run }) {
100
112
  name,
101
113
  ...extraArgs,
102
114
  ...volumes,
103
- ...ports,
115
+ ...portBindings,
104
116
  ...addExecFlag(envKeys, 'e'),
105
117
  container.image,
106
118
  ];
107
119
  const output = await exec('docker', execArgs.filter(Boolean), { env });
108
120
  if (!output.ok) {
121
+ const e = 'Failed to start container ' + name;
109
122
  console.log([output.stdout, output.stderr].join('\n\n'));
110
- throw new Error('Failed to start ' + name);
123
+ logError(e);
124
+ throw new Error(e);
111
125
  }
126
+ logInfo('Started container ' + name);
112
127
  return true;
113
128
  }
114
129
  export async function stopContainer(options, { run }) {
@@ -119,9 +134,10 @@ export async function stopContainer(options, { run }) {
119
134
  }
120
135
  await exec('docker', ['stop', '-t', '5', name]);
121
136
  await exec('docker', ['rm', name]);
122
- const containers = [findContainer(name)].filter(Boolean);
137
+ const containers = [findContainer(name)].filter(c => c && c.domain);
123
138
  for (const container of containers) {
124
139
  await run('dns.remove', { domain: container.domain });
125
140
  }
141
+ logInfo('Stopped container ' + name);
126
142
  return true;
127
143
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud-cli/dx",
3
- "version": "2.1.3",
3
+ "version": "2.2.0",
4
4
  "description": "Docker container management",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -35,6 +35,9 @@
35
35
  "get-port": "^7.1.0"
36
36
  },
37
37
  "peerDependencies": {
38
- "@cloud-cli/cli": "^2.0.0"
38
+ "@cloud-cli/cli": "^2.0.0",
39
+ "@cloud-cli/env": "^1.0.0",
40
+ "@cloud-cli/dns": "^2.0.0",
41
+ "@cloud-cli/px": "^4.0.0"
39
42
  }
40
43
  }