Skip to content

Commit

Permalink
adding 'static' port forwarding support
Browse files Browse the repository at this point in the history
  • Loading branch information
MunsMan committed Mar 1, 2023
1 parent 6ee5243 commit 6909283
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/spec-node/containerNetwork.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { DevContainerConfig, DevContainerFromDockerfileConfig, DevContainerFromImageConfig } from '../spec-configuration/configuration';

function getStaticPorts (ports: number | string | (number | string)[] | undefined): string[]{
ports = ports ?? [];
ports = typeof ports === 'number' || typeof ports === 'string'? [ports] : ports;
return ports.map((port) => typeof port === 'number'? `127.0.0.1:${port}:${port}`: port);
}

function appPorts (config: DevContainerFromDockerfileConfig | DevContainerFromImageConfig): string[]{
return getStaticPorts(config.appPort);
}

function hasAppPorts(obj: unknown):obj is (DevContainerFromDockerfileConfig | DevContainerFromImageConfig) {
return (obj as DevContainerFromDockerfileConfig | DevContainerFromImageConfig).appPort !== undefined;
}
export function applyStaticPorts (config: DevContainerConfig): string[] {
let staticPorts: string[] = [];
staticPorts = staticPorts.concat(...getStaticPorts(config.forwardPorts));
if(hasAppPorts(config)){
staticPorts = staticPorts.concat(...appPorts(config));
}
return (<string[]>[]).concat(...staticPorts.map((port) => ['-p', port]));
}

5 changes: 2 additions & 3 deletions src/spec-node/singleContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { LogLevel, Log, makeLog } from '../spec-utils/log';
import { extendImage, getExtendImageBuildInfo, updateRemoteUserUID } from './containerFeatures';
import { getDevcontainerMetadata, getImageBuildInfoFromDockerfile, getImageMetadataFromContainer, ImageMetadataEntry, mergeConfiguration, MergedDevContainerConfig } from './imageMetadata';
import { ensureDockerfileHasFinalStageName } from './dockerfileUtils';
import { applyStaticPorts } from './containerNetwork';

export const hostFolderLabel = 'devcontainer.local_folder'; // used to label containers created from a workspace/folder
export const configFileLabel = 'devcontainer.config_file';
Expand Down Expand Up @@ -326,9 +327,7 @@ export async function spawnDevContainer(params: DockerResolverParameters, config
const { common } = params;
common.progress(ResolverProgress.StartingContainer);

const appPort = config.appPort;
const exposedPorts = typeof appPort === 'number' || typeof appPort === 'string' ? [appPort] : appPort || [];
const exposed = (<string[]>[]).concat(...exposedPorts.map(port => ['-p', typeof port === 'number' ? `127.0.0.1:${port}:${port}` : port]));
const exposed = applyStaticPorts(config);

const cwdMount = workspaceMount ? ['--mount', workspaceMount] : [];

Expand Down

0 comments on commit 6909283

Please sign in to comment.