Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement spawning docker containers
- Loading branch information
Showing
7 changed files
with
132 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.DS_Store | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package executor | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/client" | ||
) | ||
|
||
var ( | ||
dockerClient *client.Client | ||
containers []string | ||
) | ||
|
||
const imageName = "alpine" | ||
|
||
func InitDockerClient() error { | ||
cli, err := client.NewClientWithOpts(client.FromEnv) | ||
if err != nil { | ||
return err | ||
} | ||
dockerClient = cli | ||
info, err := dockerClient.Info(context.Background()) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("Docker client initialized: %s", info.Name) | ||
|
||
return nil | ||
} | ||
|
||
func SpawnContainer(name string) (containerID string, err error) { | ||
log.Printf("Spawning container %s\n", name) | ||
|
||
resp, err := dockerClient.ContainerCreate(context.Background(), &container.Config{ | ||
Image: imageName, | ||
Cmd: []string{"tail", "-f", "/dev/null"}, | ||
}, nil, nil, nil, name) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
log.Printf("Container %s created\n", name) | ||
|
||
containerID = resp.ID | ||
if err := dockerClient.ContainerStart(context.Background(), containerID, container.StartOptions{}); err != nil { | ||
return "", err | ||
} | ||
log.Printf("Container %s started\n", name) | ||
|
||
containers = append(containers, containerID) | ||
return containerID, nil | ||
} | ||
|
||
func StopContainer(containerID string) error { | ||
if err := dockerClient.ContainerStop(context.Background(), containerID, container.StopOptions{}); err != nil { | ||
return err | ||
} | ||
log.Printf("Container %s stopped\n", containerID) | ||
return nil | ||
} | ||
|
||
func DeleteContainer(containerID string) error { | ||
if err := StopContainer(containerID); err != nil { | ||
return err | ||
} | ||
|
||
if err := dockerClient.ContainerRemove(context.Background(), containerID, container.RemoveOptions{}); err != nil { | ||
return err | ||
} | ||
log.Printf("Container %s removed\n", containerID) | ||
return nil | ||
} | ||
|
||
func Cleanup() error { | ||
log.Println("Cleaning up containers") | ||
|
||
for _, containerID := range containers { | ||
if err := DeleteContainer(containerID); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters