@@ -9,44 +9,25 @@ namespace Azure.Functions.Cli.Helpers
9
9
{
10
10
public static class DockerHelpers
11
11
{
12
- private static async Task RunDockerCommand ( string args , bool ignoreError = false )
13
- {
14
- var docker = new Executable ( "docker" , args ) ;
15
- ColoredConsole . WriteLine ( $ "Running { docker . Command } ") ;
16
- var exitCode = await docker . RunAsync ( l => ColoredConsole . WriteLine ( l ) , e => ColoredConsole . Error . WriteLine ( ErrorColor ( e ) ) ) ;
17
- if ( exitCode != 0 && ! ignoreError )
18
- {
19
- throw new CliException ( $ "Error running { docker . Command } ") ;
20
- }
21
- }
22
-
23
12
public static Task DockerPull ( string image ) => RunDockerCommand ( $ "pull { image } ") ;
13
+
24
14
public static Task DockerPush ( string image ) => RunDockerCommand ( $ "push { image } ") ;
15
+
25
16
public static Task DockerBuild ( string tag , string dir ) => RunDockerCommand ( $ "build -t { tag } { dir } ") ;
26
17
27
- public static async Task < string > DockerRun ( string image )
28
- {
29
- var docker = new Executable ( "docker" , $ "run -d { image } ") ;
30
- var sb = new StringBuilder ( ) ;
31
- ColoredConsole . WriteLine ( $ "Running { docker . Command } ") ;
32
- var exitCode = await docker . RunAsync ( l => sb . Append ( l ) , e => ColoredConsole . Error . WriteLine ( ErrorColor ( e ) ) ) ;
33
- if ( exitCode != 0 )
34
- {
35
- throw new CliException ( $ "Error running { docker . Command } ") ;
36
- }
37
- else
38
- {
39
- return sb . ToString ( ) . Trim ( ) ;
40
- }
41
- }
18
+ public static Task CopyToContainer ( string containerId , string source , string target ) => RunDockerCommand ( $ "cp { source } { containerId } :{ target } ", containerId ) ;
42
19
43
- public static Task CopyToContainer ( string containerId , string source , string target ) => RunDockerCommand ( $ "cp { source } { containerId } : { target } " ) ;
20
+ public static Task ExecInContainer ( string containerId , string command ) => RunDockerCommand ( $ "exec -t { containerId } { command } " , containerId ) ;
44
21
45
- public static Task ExecInContainer ( string containerId , string command ) => RunDockerCommand ( $ "exec -t { containerId } { command } " ) ;
22
+ public static Task CopyFromContainer ( string containerId , string source , string target ) => RunDockerCommand ( $ "cp { containerId } : { source } { target } " , containerId ) ;
46
23
47
- public static Task CopyFromContainer ( string containerId , string source , string target ) => RunDockerCommand ( $ "cp { containerId } : { source } { target } " ) ;
24
+ public static Task KillContainer ( string containerId , bool ignoreError = false ) => RunDockerCommand ( $ "kill { containerId } " , containerId , ignoreError ) ;
48
25
49
- public static Task KillContainer ( string containerId , bool ignoreError = false ) => RunDockerCommand ( $ "kill { containerId } ", ignoreError ) ;
26
+ public static async Task < string > DockerRun ( string image )
27
+ {
28
+ ( var output , _ ) = await RunDockerCommand ( $ "run --rm -d { image } ") ;
29
+ return output . ToString ( ) . Trim ( ) ;
30
+ }
50
31
51
32
internal static async Task < bool > VerifyDockerAccess ( )
52
33
{
@@ -59,5 +40,48 @@ internal static async Task<bool> VerifyDockerAccess()
59
40
}
60
41
return true ;
61
42
}
43
+
44
+ private static async Task < ( string output , string error ) > InternalRunDockerCommand ( string args , bool ignoreError )
45
+ {
46
+ var docker = new Executable ( "docker" , args ) ;
47
+ var sbError = new StringBuilder ( ) ;
48
+ var sbOutput = new StringBuilder ( ) ;
49
+
50
+ var exitCode = await docker . RunAsync ( l => sbOutput . AppendLine ( l ) , e => sbError . AppendLine ( e ) ) ;
51
+
52
+ if ( exitCode != 0 && ! ignoreError )
53
+ {
54
+ throw new CliException ( $ "Error running { docker . Command } .\n " +
55
+ $ "output: { sbOutput . ToString ( ) } \n { sbError . ToString ( ) } ") ;
56
+ }
57
+
58
+ return ( sbOutput . ToString ( ) , sbError . ToString ( ) ) ;
59
+ }
60
+
61
+ private static async Task < ( string output , string error ) > RunDockerCommand ( string args , string containerId = null , bool ignoreError = false )
62
+ {
63
+ var printArgs = string . IsNullOrWhiteSpace ( containerId )
64
+ ? args
65
+ : args . Replace ( containerId , containerId . Substring ( 0 , 6 ) ) ;
66
+ ColoredConsole . Write ( $ "Running 'docker { printArgs } '.") ;
67
+ var task = InternalRunDockerCommand ( args , ignoreError ) ;
68
+
69
+ while ( ! task . IsCompleted )
70
+ {
71
+ await Task . Delay ( TimeSpan . FromSeconds ( 1 ) ) ;
72
+ ColoredConsole . Write ( "." ) ;
73
+ }
74
+ ColoredConsole . WriteLine ( "done" ) ;
75
+
76
+ ( var output , var error ) = await task ;
77
+
78
+ if ( StaticSettings . IsDebug )
79
+ {
80
+ ColoredConsole
81
+ . WriteLine ( $ "Output: { output } ")
82
+ . WriteLine ( $ "Error: { error } ") ;
83
+ }
84
+ return ( output , error ) ;
85
+ }
62
86
}
63
87
}
0 commit comments