diff --git a/src/lib/dockerize.ts b/src/lib/dockerize.ts index ce5b080..0fc3da3 100644 --- a/src/lib/dockerize.ts +++ b/src/lib/dockerize.ts @@ -24,6 +24,9 @@ import { export default async function dockerize(options: DockerizeArguments) { + await ensureDocker(); + + // ----- [1] Validate Options ------------------------------------------------ ow(options.cwd, 'cwd', ow.string); diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 0e81d70..dff533e 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -10,6 +10,27 @@ import readPkgUp, {NormalizedPackageJson, Options} from 'read-pkg-up'; import tar from 'tar'; +/** + * Ensures that Docker is installed on the system and that the Docker daemon is + * running. + */ +export async function ensureDocker() { + try { + await execa('docker', ['version']); + } catch (err) { + if (err.exitCode === 127 || (err.exitCode === 2 && err.exitCodeName === 'ENOENT')) { + throw new Error('The "docker" command could not be found. Ensure Docker is installed.'); + } + + if (err.stderr.toLowerCase().includes('cannot connect to the docker daemon')) { + throw new Error('Unable to connect to the Docker daemon. Ensure Docker is running.'); + } + + throw err; + } +} + + /** * If the provided value is an array, it is returned as-is. Otherwise, the value * is wrapped in an array and returned. @@ -177,7 +198,7 @@ export async function copyNpmrc(npmrcOption: string | undefined, destDir: string /** - * Provided an image name, returns its size. + * Provided a Docker image name, returns its size. */ export async function getImageSize(imageName: string) { const results = await execa('docker', ['inspect', imageName]);