Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

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).
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).

## Installation

Expand Down Expand Up @@ -88,6 +88,21 @@ result.data.services.forEach((service) => {
})
```

### Standalone mode

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.

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.

```js
compose.upAll({
executable: {
standalone: true,
executablePath: '/path/to/docker-compose' // optional
}
})
```

## Known issues

* 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).
Expand Down
33 changes: 24 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
import yaml from 'yaml'
import mapPorts from './map-ports'

export interface IDockerComposeExecutableOptions {
executablePath: string
options?: string[] | (string | string[])[]
}
export type IDockerComposeExecutableOptions =
| {
executablePath: string
options?: string[] | (string | string[])[]
standalone?: never
}
| {
executablePath?: string
options?: never
standalone: true
}

export interface IDockerComposeOptions {
cwd?: string
Expand Down Expand Up @@ -268,8 +275,8 @@
* Executes docker compose command with common options
*/
export const execCompose = (
command,

Check warning on line 278 in src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Argument 'command' should be typed
args,

Check warning on line 279 in src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Argument 'args' should be typed
options: IDockerComposeOptions = {}
): Promise<IDockerComposeResult> =>
new Promise((resolve, reject): void => {
Expand All @@ -290,14 +297,22 @@

const cwd = options.cwd
const env = options.env || undefined
const executable = options.executable || { executablePath: 'docker' }
const executable = options.executable

const executableOptions = executable.options || []
const executableArgs = composeOptionsToArgs(executableOptions)
let executablePath: string
let executableArgs: string[] = []

if (executable?.standalone && !executable.executablePath) {
executablePath = 'docker-compose'
} else {
executablePath = executable?.executablePath || 'docker'
const executableOptions = executable?.options || []
executableArgs = [...composeOptionsToArgs(executableOptions), 'compose']
}

const childProc = childProcess.spawn(
executable.executablePath,
[...executableArgs, 'compose', ...composeArgs],
executablePath,
[...executableArgs, ...composeArgs],
{
cwd,
env
Expand Down
Loading