⚠️ Warning: PodKeeper is currently in pre-1.0.0 release. Expect potential changes and experimental features that may not be fully stable yet.
PodKeeper is a node.js open-source library for starting and stopping Docker containers in a way that ensures they won’t linger if the host program crashes or exits unexpectedly without properly stopping them.
PodKeeper is written in TypeScript and comes bundled with TypeScript types.
-
Install podkeeper:
npm i --save-dev podkeeper
-
Pull container you wish to launch beforehand:
docker pull postgres:latest
-
Start / stop container programmatically:
import { Postgres } from 'podkeeper'; const postgres = await Postgres.start(); // do something with container... await postgres.stop();
PodKeeper comes bundled with the following pre-configured services:
-
MySQL:
import { MySQL } from 'podkeeper';
-
Postgres
import { Postgres } from 'podkeeper';
-
Minio
import { Minio } from 'podkeeper';
If a popular service is missing, please do not hesitate to submit a pull request.
Alternatively, you can launch a generic container service with the GenericService class:
import { GenericService } from 'podkeeper';
cosnt service = await GenericService.start({
imageName: string,
ports: number[],
healthcheck?: {
test: string[],
intervalMs: number,
retries: number,
startPeriodMs: number,
timeoutMs: number,
},
command?: string[],
env?: { [key: string]: string | number | boolean | undefined };
});Each Docker container has a primary process, known as the "entrypoint," which keeps the container running. For example, when you start a PostgreSQL container, it runs the postgres binary. When this binary exits, the container stops as well.
PodKeeper wraps the entrypoint in a special binary called deadmanswitch, which not only starts the entrypoint but also launches a WebSocket server. The client that initiated the container must connect to this WebSocket server; otherwise, the container will self-terminate after 10 seconds.
This setup creates a connection between the launched container and its owner. Whenever this WebSocket disconnects, the deadmanswitch program automatically stops the container.
Both PodKeeper and TestContainers provide solutions for starting, stopping, and cleaning up Docker containers:
- TestContainers uses a dedicated Docker container called "Ryuk" to manage cleanup.
- PodKeeper relies on a dead man's switch mechanism for cleanup.
While TestContainers is a mature, industry-proven tool, PodKeeper is an experimental alternative that explores a different approach.
There are also some notable differences in API design philosophy:
- Process Behavior: PodKeeper services prevent the Node.js process from exiting, while TestContainers services do not.
- Container Pulling: PodKeeper does not implicitly pull containers, requiring them to be available beforehand, whereas TestContainers lazily pulls containers as needed when launching a service.
- Healthchecks: The services that PodKeeper ships out-of-the-box are pre-configured to use proper healthchecks.
Requires pnpm 11. pnpm installs the Node.js version pinned in devEngines in package.json.
pnpm install
pnpm build # TypeScript
pnpm build:deadmanswitch # Go binaries; needs Go and UPX
pnpm test # needs Docker and `docker pull postgres:latest`-
Bump the version:
# For a stable minor release pnpm version minor # For an alpha pre-release pnpm version preminor --preid=alpha
-
Push the commit and tag:
git push --follow-tags
-
Create a GitHub Release for the new tag and publish it.
CI will handle publishing to npm. Pre-releases are published under the
@nexttag.