Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to expose ports when running locally with Docker #492

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bin/cmds/app/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ exports.builder = yargs => {
default: 'bridge',
type: 'string',
description: 'Docker network mode. Must match name from `docker network ls`. Only works when running the app inside Docker.',
})
.option('ports', {
default: [],
type: 'array',
description: 'Docker exposed ports, i.e. `6113/tcp` or `5683/udp`. Only works when running the app inside docker.',
});
};
exports.handler = async yargs => {
Expand All @@ -46,6 +51,7 @@ exports.handler = async yargs => {
skipBuild: yargs.skipBuild,
linkModules: yargs.linkModules,
network: yargs.network,
ports: yargs.ports,
});
} catch (err) {
if (err instanceof Error && err.stack) {
Expand Down
31 changes: 31 additions & 0 deletions lib/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class App {
skipBuild = false,
linkModules = '',
network = 'bridge',
ports = [],
} = {}) {
const homey = await AthomApi.getActiveHomey();

Expand Down Expand Up @@ -176,6 +177,7 @@ class App {
skipBuild,
linkModules,
network,
ports,
});
}

Expand Down Expand Up @@ -220,6 +222,7 @@ class App {
skipBuild,
linkModules,
network,
ports,
}) {
// Prepare Docker
const docker = new Docker();
Expand Down Expand Up @@ -746,9 +749,37 @@ class App {
},
};

const exposedPorts = [];
if (Array.isArray(ports)) {
const portRegex = /^\d+$/;
const portWithProtocolRegex = /^(\d+)\/(udp|tcp)$/;
ports.forEach(port => {
let portNumber;
let portProtocol;
if (portRegex.test(port)) {
portNumber = port;
portProtocol = 'tcp';
} else if (portWithProtocolRegex.test(port)) {
const matches = portWithProtocolRegex.exec(port);
portNumber = matches[1];
portProtocol = matches[2];
} else {
throw new Error(`The requested port "${port}" is not valid`);
}

const dockerPort = `${portNumber}/${portProtocol}`;
createOpts.ExposedPorts[dockerPort] = {};
createOpts.HostConfig.PortBindings[dockerPort] = [{
HostPort: `${portNumber}`,
}];
exposedPorts.push(dockerPort);
});
}

Log.success(`Starting debugger at 0.0.0.0:${inspectPort}...`);
Log.info(' — Open `about://inspect` in Google Chrome and select the remote target.');
Log.success(`Starting \`${manifest.id}@${manifest.version}\` in a Docker container...`);
exposedPorts.forEach(port => Log.info(` — Exposing Docker port ${port} on local machine...`));
Log.info(' — Press CTRL+C to quit.');
Log('─────────────── Logging stdout & stderr ───────────────');

Expand Down