|
1 |
| -"use strict"; |
2 |
| -const Docker = require("dockerode"); |
| 1 | +'use strict' |
| 2 | +const Docker = require('dockerode') |
3 | 3 |
|
4 | 4 | const Containers = {
|
5 | 5 | mongo: {
|
6 |
| - name: "fastify-mongo", |
7 |
| - Image: "mongo:7", |
| 6 | + name: 'fastify-mongo', |
| 7 | + Image: 'mongo:7', |
8 | 8 | Tty: false,
|
9 | 9 | HostConfig: {
|
10 | 10 | PortBindings: {
|
11 |
| - "27017/tcp": [{ HostIp: "0.0.0.0", HostPort: "27017" }], |
| 11 | + '27017/tcp': [{ HostIp: '0.0.0.0', HostPort: '27017' }], |
12 | 12 | },
|
13 | 13 | AutoRemove: true,
|
14 | 14 | },
|
15 | 15 | },
|
16 |
| -}; |
| 16 | +} |
17 | 17 |
|
18 | 18 | function dockerConsole() {
|
19 |
| - const docker = new Docker(); |
| 19 | + const docker = new Docker() |
20 | 20 | async function pullImage(container) {
|
21 | 21 | return new Promise((resolve, reject) => {
|
22 | 22 | docker.pull(container.Image, (err, stream) => {
|
23 | 23 | if (err) {
|
24 |
| - reject(err); |
25 |
| - return; |
| 24 | + reject(err) |
| 25 | + return |
26 | 26 | }
|
27 |
| - docker.modem.followProgress(stream, onFinished, onProgress); |
28 |
| - }); |
| 27 | + docker.modem.followProgress(stream, onFinished, onProgress) |
| 28 | + }) |
29 | 29 | function onFinished(err, output) {
|
30 | 30 | if (err) {
|
31 |
| - reject(err); |
32 |
| - return; |
| 31 | + reject(err) |
| 32 | + return |
33 | 33 | }
|
34 |
| - resolve(output); |
| 34 | + resolve(output) |
35 | 35 | }
|
36 | 36 | function onProgress(event) {
|
37 |
| - console.log(event); |
| 37 | + console.log(event) |
38 | 38 | }
|
39 |
| - }); |
| 39 | + }) |
40 | 40 | }
|
41 | 41 | return {
|
42 | 42 | async getRunningContainer(container) {
|
43 |
| - const containers = await docker.listContainers(); |
| 43 | + const containers = await docker.listContainers() |
44 | 44 | return containers.find((running) => {
|
45 |
| - return running.Names.some((name) => name.includes(container.name)); |
46 |
| - }); |
| 45 | + return running.Names.some((name) => name.includes(container.name)) |
| 46 | + }) |
47 | 47 | },
|
48 | 48 | async startContainer(container) {
|
49 |
| - const run = await this.getRunningContainer(container); |
| 49 | + const run = await this.getRunningContainer(container) |
50 | 50 | if (!run) {
|
51 |
| - await pullImage(container); |
52 |
| - const containerObj = await docker.createContainer(container); |
53 |
| - await containerObj.start(); |
| 51 | + await pullImage(container) |
| 52 | + const containerObj = await docker.createContainer(container) |
| 53 | + await containerObj.start() |
54 | 54 | }
|
55 | 55 | },
|
56 | 56 | async stopContainer(container) {
|
57 |
| - const run = await this.getRunningContainer(container); |
| 57 | + const run = await this.getRunningContainer(container) |
58 | 58 | if (run) {
|
59 |
| - const containerObj = await docker.getContainer(run.Id); |
60 |
| - await containerObj.stop(); |
| 59 | + const containerObj = await docker.getContainer(run.Id) |
| 60 | + await containerObj.stop() |
61 | 61 | }
|
62 | 62 | },
|
63 | 63 | pullImage,
|
64 |
| - }; |
| 64 | + } |
65 | 65 | }
|
66 | 66 |
|
67 |
| -module.exports = dockerConsole; |
68 |
| -module.exports.Containers = Containers; |
| 67 | +module.exports = dockerConsole |
| 68 | +module.exports.Containers = Containers |
0 commit comments