@@ -9,44 +9,25 @@ namespace Azure.Functions.Cli.Helpers
99{
1010 public static class DockerHelpers
1111 {
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-
2312 public static Task DockerPull ( string image ) => RunDockerCommand ( $ "pull { image } ") ;
13+
2414 public static Task DockerPush ( string image ) => RunDockerCommand ( $ "push { image } ") ;
15+
2516 public static Task DockerBuild ( string tag , string dir ) => RunDockerCommand ( $ "build -t { tag } { dir } ") ;
2617
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 ) ;
4219
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 ) ;
4421
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 ) ;
4623
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 ) ;
4825
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+ }
5031
5132 internal static async Task < bool > VerifyDockerAccess ( )
5233 {
@@ -59,5 +40,48 @@ internal static async Task<bool> VerifyDockerAccess()
5940 }
6041 return true ;
6142 }
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+ }
6286 }
6387}
0 commit comments