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

Automatically publish exposed ports when running a container #83

Merged
merged 4 commits into from
May 19, 2017
Merged
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
31 changes: 22 additions & 9 deletions commands/start-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,28 @@ const teleCmdId: string = 'vscode-docker.container.start';
function doStartContainer(interactive: boolean) {
quickPickImage(false).then(function (selectedItem: ImageItem) {
if (selectedItem) {
let option = interactive ? '-it' : '';
let terminal = vscode.window.createTerminal(selectedItem.label);
terminal.sendText(`docker run ${option} --rm ${selectedItem.label}`);
terminal.show();
if (reporter) {
reporter.sendTelemetryEvent('command', {
command: interactive ? teleCmdId + '.interactive' : teleCmdId
});
}
docker.getExposedPorts(selectedItem.label).then((ports: string[]) => {
let options = '--rm'

if (interactive) {
options += ' -it';
}

if (ports.length) {
const portMappings = ports.map((port) => `-p ${port}:${port}`);
options += ` ${portMappings.join(' ')}`;
}

const terminal = vscode.window.createTerminal(selectedItem.label);
terminal.sendText(`docker run ${options} ${selectedItem.label}`);
terminal.show();

if (reporter) {
reporter.sendTelemetryEvent('command', {
command: interactive ? teleCmdId + '.interactive' : teleCmdId
});
}
});
}
});
}
Expand Down
9 changes: 9 additions & 0 deletions commands/utils/docker-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ class DockerClient {
return Promise.resolve(DockerEngineType.Linux);
}

public getExposedPorts(imageId: string) : Thenable<string[]> {
return new Promise((resolve, reject) => {
this.getImage(imageId).inspect((error, { Config: { ExposedPorts = {} }}) => {
const ports = Object.keys(ExposedPorts).map((port) => port.split("/")[0]);
resolve(ports);
});
});
}

public getImage(id:string): Docker.Image {
return this.endPoint.getImage(id);
}
Expand Down
17 changes: 7 additions & 10 deletions configureWorkspace/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@ function genDockerFile(serviceName: string, imageName: string, platform: string,
switch (platform.toLowerCase()) {
case 'node.js':

return `
FROM node:6-slim
LABEL Name=${serviceName} Version=${version}
COPY package.json /tmp/package.json
RUN cd /tmp && npm install --production
RUN mkdir -p /usr/src/app && mv /tmp/node_modules /usr/src
WORKDIR /usr/src/app
COPY . /usr/src/app
return `FROM node:6-alpine
LABEL Name=${serviceName} Version=${version}
WORKDIR /usr/src/app
COPY package.json package.json
RUN npm install --only=prod
COPY . .
EXPOSE ${port}
CMD ${cmd}
`;
CMD ${cmd}`;

case 'go':

Expand Down
1 change: 1 addition & 0 deletions typings/dockerode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ declare module Docker {
}

class Image {
inspect(cb: (err: Error, data: any) => void): void;
name: string;
remove(options: any, cb: (err: Error, exec: Exec)=>void): void;
tag(options: any, cb: (err: Error, exec: Exec)=>void): void;
Expand Down