@@ -22,13 +22,18 @@ type dockerInfo struct {
2222 Stream string `json:"stream"`
2323}
2424
25- // Build builds a docker image from the image directory
26- func Build (name string , dir string ) {
27- cli , err := client .NewEnvClient ()
25+ var cli * client.Client
26+
27+ func init () {
28+ var err error
29+ cli , err = client .NewEnvClient ()
2830 if err != nil {
2931 panic (err )
3032 }
33+ }
3134
35+ // Build builds a docker image from the image directory
36+ func Build (name string , dir string ) {
3237 tar := new (archivex.TarFile )
3338 tar .Create (dir )
3439 tar .AddAll (dir , false )
@@ -66,11 +71,6 @@ func Build(name string, dir string) {
6671// Pull image from hub.docker.com
6772func Pull (name string , verbose bool ) {
6873 ctx := context .Background ()
69- cli , err := client .NewEnvClient ()
70- if err != nil {
71- panic (err )
72- }
73-
7474 if r , pullErr := cli .ImagePull (ctx , name , types.ImagePullOptions {}); pullErr != nil {
7575 panic (pullErr )
7676 } else {
@@ -83,10 +83,6 @@ func Pull(name string, verbose bool) {
8383// Deploy spins up a new container
8484func Deploy (name string , dir string , port string ) {
8585 ctx := context .Background ()
86- cli , err := client .NewEnvClient ()
87- if err != nil {
88- panic (err )
89- }
9086 imageName := name
9187 containerConfig := & container.Config {
9288 Image : imageName ,
@@ -117,30 +113,18 @@ func Deploy(name string, dir string, port string) {
117113
118114// Stop interrupts a running container
119115func Stop (containerID string ) (err error ) {
120- cli , err := client .NewEnvClient ()
121- if err != nil {
122- return err
123- }
124116 timeout := time .Duration (1 ) * time .Second
125117 err = cli .ContainerStop (context .Background (), containerID , & timeout )
126118 return err
127119}
128120
129121// Remove interrupts and remove a running container
130122func Remove (containerID string ) (err error ) {
131- cli , err := client .NewEnvClient ()
132- if err != nil {
133- return err
134- }
135123 return cli .ContainerRemove (context .Background (), containerID , types.ContainerRemoveOptions {Force : true })
136124}
137125
138126// Remove remove docker image by imageID
139127func ImageRemove (imageID string ) (err error ) {
140- cli , err := client .NewEnvClient ()
141- if err != nil {
142- return err
143- }
144128 _ , err = cli .ImageRemove (context .Background (), imageID , types.ImageRemoveOptions {Force : true })
145129 return err
146130}
0 commit comments