Skip to content

Commit

Permalink
feat: Improve Docker-related error-handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua committed Aug 29, 2019
1 parent 0a5067c commit a17af2e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/lib/dockerize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import {


export default async function dockerize(options: DockerizeArguments) {
await ensureDocker();


// ----- [1] Validate Options ------------------------------------------------

ow(options.cwd, 'cwd', ow.string);
Expand Down
23 changes: 22 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]);
Expand Down

0 comments on commit a17af2e

Please sign in to comment.