|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +const cp = require('child_process'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +const dockerImageName = 'elasticsearch-nodkz'; |
| 7 | +const version = process.argv[2] || 5; |
| 8 | + |
| 9 | +function isDockerImageExists(imageNameWithTag) { |
| 10 | + const imageId = cp |
| 11 | + .execSync(`docker images -q ${imageNameWithTag}`, { cwd: '.' }) |
| 12 | + .toString(); |
| 13 | + return imageId && imageId.length > 0; |
| 14 | +} |
| 15 | + |
| 16 | +function buildDockerContainer(v) { |
| 17 | + const imageNameWithTag = `${dockerImageName}:${v}`; |
| 18 | + const dockerContextFolder = path.resolve(__dirname, `./es${v}`); |
| 19 | + console.log( |
| 20 | + `Building docker container ${imageNameWithTag} from ${dockerContextFolder}/Dockerfile ...` |
| 21 | + ); |
| 22 | + cp.execSync( |
| 23 | + `docker build \ |
| 24 | + -t ${imageNameWithTag} \ |
| 25 | + ${dockerContextFolder}`, |
| 26 | + { |
| 27 | + cwd: dockerContextFolder, |
| 28 | + stdio: [0, 1, 2], |
| 29 | + } |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +function runDockerContainer(v) { |
| 34 | + const imageNameWithTag = `${dockerImageName}:${v}`; |
| 35 | + if (!isDockerImageExists(imageNameWithTag)) { |
| 36 | + buildDockerContainer(v); |
| 37 | + } |
| 38 | + |
| 39 | + console.log(`Starting docker container ${imageNameWithTag} ...`); |
| 40 | + cp.execSync(`docker run -i --rm -p 9200:9200 ${imageNameWithTag}`, { |
| 41 | + stdio: [0, 1, 2], |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +function removeDockerContainer(v) { |
| 46 | + const imageNameWithTag = `${dockerImageName}:${v}`; |
| 47 | + console.log(`Removing docker image ${imageNameWithTag} ...`); |
| 48 | + cp.execSync(`docker rmi ${imageNameWithTag}`, { stdio: [0, 1, 2] }); |
| 49 | +} |
| 50 | + |
| 51 | +function onExit() { |
| 52 | + removeDockerContainer(version); |
| 53 | + process.exit(0); |
| 54 | +} |
| 55 | +process.on('SIGINT', onExit); // catch ctrl-c |
| 56 | +process.on('SIGTERM', onExit); // catch kill |
| 57 | +runDockerContainer(version); |
0 commit comments