@@ -136,6 +136,8 @@ export interface ILocalBundling {
136
136
137
137
/**
138
138
* A Docker image used for asset bundling
139
+ *
140
+ * @deprecated use DockerImage
139
141
*/
140
142
export class BundlingDockerImage {
141
143
/**
@@ -152,6 +154,8 @@ export class BundlingDockerImage {
152
154
*
153
155
* @param path The path to the directory containing the Docker file
154
156
* @param options Docker build options
157
+ *
158
+ * @deprecated use DockerImage.fromBuild()
155
159
*/
156
160
public static fromAsset ( path : string , options : DockerBuildOptions = { } ) {
157
161
const buildArgs = options . buildArgs || { } ;
@@ -184,7 +188,7 @@ export class BundlingDockerImage {
184
188
}
185
189
186
190
/** @param image The Docker image */
187
- private constructor ( public readonly image : string , private readonly _imageHash ?: string ) { }
191
+ protected constructor ( public readonly image : string , private readonly _imageHash ?: string ) { }
188
192
189
193
/**
190
194
* Provides a stable representation of this image for JSON serialization.
@@ -232,27 +236,50 @@ export class BundlingDockerImage {
232
236
}
233
237
234
238
/**
235
- * Copies a file or directory out of the Docker image to the local filesystem
239
+ * Copies a file or directory out of the Docker image to the local filesystem.
240
+ *
241
+ * If `outputPath` is omitted the destination path is a temporary directory.
242
+ *
243
+ * @param imagePath the path in the Docker image
244
+ * @param outputPath the destination path for the copy operation
245
+ * @returns the destination path
236
246
*/
237
- public cp ( imagePath : string , outputPath : string ) {
238
- const { stdout } = dockerExec ( [ 'create' , this . image ] ) ;
247
+ public cp ( imagePath : string , outputPath ? : string ) : string {
248
+ const { stdout } = dockerExec ( [ 'create' , this . image ] , { } ) ; // Empty options to avoid stdout redirect here
239
249
const match = stdout . toString ( ) . match ( / ( [ 0 - 9 a - f ] { 16 , } ) / ) ;
240
250
if ( ! match ) {
241
251
throw new Error ( 'Failed to extract container ID from Docker create output' ) ;
242
252
}
243
253
244
254
const containerId = match [ 1 ] ;
245
255
const containerPath = `${ containerId } :${ imagePath } ` ;
256
+ const destPath = outputPath ?? FileSystem . mkdtemp ( 'cdk-docker-cp-' ) ;
246
257
try {
247
- dockerExec ( [ 'cp' , containerPath , outputPath ] ) ;
258
+ dockerExec ( [ 'cp' , containerPath , destPath ] ) ;
259
+ return destPath ;
248
260
} catch ( err ) {
249
- throw new Error ( `Failed to copy files from ${ containerPath } to ${ outputPath } : ${ err } ` ) ;
261
+ throw new Error ( `Failed to copy files from ${ containerPath } to ${ destPath } : ${ err } ` ) ;
250
262
} finally {
251
263
dockerExec ( [ 'rm' , '-v' , containerId ] ) ;
252
264
}
253
265
}
254
266
}
255
267
268
+ /**
269
+ * A Docker image
270
+ */
271
+ export class DockerImage extends BundlingDockerImage {
272
+ /**
273
+ * Builds a Docker image
274
+ *
275
+ * @param path The path to the directory containing the Docker file
276
+ * @param options Docker build options
277
+ */
278
+ public static fromBuild ( path : string , options : DockerBuildOptions = { } ) {
279
+ return BundlingDockerImage . fromAsset ( path , options ) ;
280
+ }
281
+ }
282
+
256
283
/**
257
284
* A Docker volume
258
285
*/
0 commit comments