11import * as dockerCompose from "docker-compose" ;
2- import Dockerode from "dockerode" ;
2+ import Dockerode , { ContainerInfo } from "dockerode" ;
33import { BoundPorts } from "./bound-ports" ;
44import { Container } from "./container" ;
55import { ContainerState } from "./container-state" ;
@@ -50,7 +50,7 @@ export class DockerComposeEnvironment {
5050 }
5151
5252 public async up ( ) : Promise < StartedDockerComposeEnvironment > {
53- log . info ( `Starting DockerCompose environment` ) ;
53+ log . info ( `Starting DockerCompose environment ${ this . projectName } ` ) ;
5454
5555 const dockerClient = await DockerClientInstance . getInstance ( ) ;
5656
@@ -61,6 +61,16 @@ export class DockerComposeEnvironment {
6161 ( container ) => container . Labels [ "com.docker.compose.project" ] === this . projectName
6262 ) ;
6363
64+ const startedContainerNames = startedContainers . reduce (
65+ ( containerNames : string [ ] , startedContainer : ContainerInfo ) => [
66+ ...containerNames ,
67+ startedContainer . Names . join ( ", " ) ,
68+ ] ,
69+ [ ]
70+ ) ;
71+
72+ log . info ( `Started the following containers: ${ startedContainerNames . join ( ", " ) } ` ) ;
73+
6474 const startedGenericContainers = (
6575 await Promise . all (
6676 startedContainers . map ( async ( startedContainer ) => {
@@ -75,7 +85,20 @@ export class DockerComposeEnvironment {
7585 const boundPorts = this . getBoundPorts ( startedContainer ) ;
7686 const containerState = new ContainerState ( inspectResult ) ;
7787
78- await this . waitForContainer ( dockerClient , container , containerName , containerState , boundPorts ) ;
88+ try {
89+ log . info ( `Waiting for container ${ containerName } to be ready` ) ;
90+ await this . waitForContainer ( dockerClient , container , containerName , containerState , boundPorts ) ;
91+ log . info ( `Container ${ containerName } is ready` ) ;
92+ } catch ( err ) {
93+ log . error ( `Container ${ containerName } failed to be ready: ${ err } ` ) ;
94+
95+ try {
96+ await down ( this . composeFilePath , this . composeFiles , this . projectName ) ;
97+ } catch {
98+ log . warn ( `Failed to stop DockerCompose environment after failed up` ) ;
99+ }
100+ throw err ;
101+ }
79102
80103 return new StartedGenericContainer (
81104 container ,
@@ -115,22 +138,19 @@ export class DockerComposeEnvironment {
115138 env : { ...defaultOptions . env , ...this . env } ,
116139 } ;
117140
118- log . info ( `Starting DockerCompose environment` ) ;
141+ log . info ( `Upping DockerCompose environment` ) ;
119142 try {
120143 await dockerCompose . upAll ( options ) ;
121- log . info ( `Started DockerCompose environment` ) ;
144+ log . info ( `Upped DockerCompose environment` ) ;
122145 } catch ( err ) {
123- log . error ( `Failed to start DockerCompose environment: ${ err } ` ) ;
146+ const errorMessage = err . err ? err . err . trim ( ) : err ;
147+ log . error ( `Failed to up DockerCompose environment: ${ errorMessage } ` ) ;
124148 try {
125149 await down ( this . composeFilePath , this . composeFiles , this . projectName ) ;
126150 } catch {
127- log . warn ( `Failed to stop DockerCompose environment after failed start` ) ;
128- }
129- if ( err . err ) {
130- throw new Error ( err . err . trim ( ) ) ;
131- } else {
132- throw new Error ( err ) ;
151+ log . warn ( `Failed to stop DockerCompose environment after failed up` ) ;
133152 }
153+ throw new Error ( errorMessage ) ;
134154 }
135155 }
136156
@@ -147,10 +167,8 @@ export class DockerComposeEnvironment {
147167 containerState : ContainerState ,
148168 boundPorts : BoundPorts
149169 ) : Promise < void > {
150- log . debug ( "Waiting for container to be ready" ) ;
151170 const waitStrategy = this . getWaitStrategy ( dockerClient , container , containerName ) ;
152171 await waitStrategy . withStartupTimeout ( this . startupTimeout ) . waitUntilReady ( container , containerState , boundPorts ) ;
153- log . info ( "Container is ready" ) ;
154172 }
155173
156174 private getWaitStrategy ( dockerClient : DockerClient , container : Container , containerName : string ) : WaitStrategy {
0 commit comments