sidelifter

Utilities for working with Docker containers

Usage no npm install needed!

<script type="module">
  import sidelifter from 'https://cdn.skypack.dev/sidelifter';
</script>

README

sidelifter

npm Build Status dependencies Status devDependencies Status

Utilities for working with Docker containers

Example

import * as path from 'path'
import { startContainer, removeContainer } from 'sidelifter'
import { openDatabase } from 'rumor-mill'

async function run() {
  const container = await startContainer({
    image: 'mysql:5',
    env: {
      MYSQL_RANDOM_ROOT_PASSWORD: '1',
      MYSQL_DATABASE: 'database',
      MYSQL_USER: 'user',
      MYSQL_PASSWORD: 'password'
    },
    mount: {
      [path.resolve()]: '/root/app'
    }
  })

  const localPort = container.ports.get(3306)
  const db = await openDatabase(`mysql://user:password@127.0.0.1:${localPort}/database`)

  await removeContainer(container)
}

run()

Usage

Install sidelifter by running:

yarn add sidelifter

Actions

startContainer(options)

Pulls an image, creates a container, and starts the container.

Readable streams container.stdout and container.stderr are provided. A writable stream container.stdin is also provided.

Mapped ports are provided as container.ports via Map from container port to host port.

Parameters
  • options (object)
    • image (string): The name and tag of a Docker image
    • env ({ [string]: string }): Environment variables to be set within the container
    • mount ({ [string]: string }): Directories to mount within the container
    • cmd (Array): An optional command to run within the Docker container
Returns

waitForContainer(container)

Waits for the Cmd that the container is running to exit.

Parameters
Returns
  • promise (Promise)

removeContainer(container)

Stops and destroys the container.

Parameters
Returns
  • promise (Promise)

Debugging

Debug logging can be enabled by setting the environment variable NODE_DEBUG=sidelifter.

See the Node.js documentation for more information.