Skip to content

Commit 297183d

Browse files
tpluscodeAlexZeitler
authored andcommitted
feat: using standalone binary
1 parent c6c54a8 commit 297183d

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

readme.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
`docker-compose` is a small library that allows you to run [docker-compose](https://docs.docker.com/compose/) (which is still required) via Node.js. This is useful to bootstrap test environments.
99

10-
As of version 1.0, this library only supports `docker compose` (v2, the docker "compose" plugin). The `docker-compose` (v1) has been removed from recent releases of Docker Desktop and is no longer supported. Use the `0.x` versions of this library if you still need to use the old `docker-compose` (v1).
10+
As of version 1.0, this library supports `docker compose` (v2, the docker "compose" plugin) by default. The `docker-compose` (v1) has been removed from recent releases of Docker Desktop and is no longer supported. However, you can still force the use of `docker-compose` by using the [standanlone mode](#standalone-mode).
1111

1212
## Installation
1313

@@ -88,6 +88,21 @@ result.data.services.forEach((service) => {
8888
})
8989
```
9090

91+
### Standalone mode
92+
93+
While the `docker-compose` executable is no longer part of a default docker installation, it is still possible to download its binary [standalone](https://docs.docker.com/compose/install/standalone/). This is useful for example when building docker images, avoiding the need to install the whole docker stack.
94+
95+
To use a standalone binary, you can set the `executable.standalone` option to `true`. You can also set the `executablePath` option to the path of the `docker-compose` binary.
96+
97+
```js
98+
compose.upAll({
99+
executable: {
100+
standalone: true,
101+
executablePath: '/path/to/docker-compose' // optional
102+
}
103+
})
104+
```
105+
91106
## Known issues
92107

93108
* During testing we noticed that `docker compose` seems to send its exit code also commands don't seem to have finished. This doesn't occur for all commands, but for example with `stop` or `down`. We had the option to wait for stopped / removed containers using third party libraries but this would make bootstrapping `docker-compose` much more complicated for the users. So we decided to use a `setTimeout(500)` workaround. We're aware this is not perfect, but it seems to be the most appropriate solution for now. Details can be found in the [v2 PR discussion](https://github.com/PDMLab/docker-compose/pull/228#issuecomment-1422895821) (we're happy to get help here).

src/index.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ import childProcess from 'child_process'
22
import yaml from 'yaml'
33
import mapPorts from './map-ports'
44

5-
export interface IDockerComposeExecutableOptions {
6-
executablePath: string
7-
options?: string[] | (string | string[])[]
8-
}
5+
export type IDockerComposeExecutableOptions =
6+
| {
7+
executablePath: string
8+
options?: string[] | (string | string[])[]
9+
standalone?: never
10+
}
11+
| {
12+
executablePath?: string
13+
options?: never
14+
standalone: true
15+
}
916

1017
export interface IDockerComposeOptions {
1118
cwd?: string
@@ -290,14 +297,22 @@ export const execCompose = (
290297

291298
const cwd = options.cwd
292299
const env = options.env || undefined
293-
const executable = options.executable || { executablePath: 'docker' }
300+
const executable = options.executable
294301

295-
const executableOptions = executable.options || []
296-
const executableArgs = composeOptionsToArgs(executableOptions)
302+
let executablePath: string
303+
let executableArgs: string[] = []
304+
305+
if (executable?.standalone && !executable.executablePath) {
306+
executablePath = 'docker-compose'
307+
} else {
308+
executablePath = executable?.executablePath || 'docker'
309+
const executableOptions = executable?.options || []
310+
executableArgs = [...composeOptionsToArgs(executableOptions), 'compose']
311+
}
297312

298313
const childProc = childProcess.spawn(
299-
executable.executablePath,
300-
[...executableArgs, 'compose', ...composeArgs],
314+
executablePath,
315+
[...executableArgs, ...composeArgs],
301316
{
302317
cwd,
303318
env

0 commit comments

Comments
 (0)