added docker python code for virtualization
This commit is contained in:
@@ -8,9 +8,16 @@ import (
|
||||
"bytes"
|
||||
"os"
|
||||
"archive/tar"
|
||||
"log"
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/client"
|
||||
|
||||
natting "github.com/docker/go-connections/nat"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
network "github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
|
||||
@@ -40,6 +47,15 @@ func RunVM()(interface{},error) {
|
||||
|
||||
// TODO Run contianer
|
||||
|
||||
imagename := "ubuntu"
|
||||
containername := "test1"
|
||||
portopening := "1003"
|
||||
inputEnv := []string{fmt.Sprintf("LISTENINGPORT=%s", portopening)}
|
||||
err = RunContainer(client, imagename, containername, portopening, inputEnv)
|
||||
if err != nil {
|
||||
return nil,err
|
||||
}
|
||||
|
||||
sshreturn := new(SSHreturn)
|
||||
|
||||
sshreturn.Port = "1003"
|
||||
@@ -51,6 +67,7 @@ func RunVM()(interface{},error) {
|
||||
}
|
||||
|
||||
// Taken from (https://medium.com/@Frikkylikeme/controlling-docker-with-golang-code-b213d9699998)
|
||||
// Builds local docker image
|
||||
func BuildImage(client *client.Client, tags []string, dockerfile string) error{
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -120,3 +137,80 @@ func BuildImage(client *client.Client, tags []string, dockerfile string) error{
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
// Taken from (https://medium.com/@Frikkylikeme/controlling-docker-with-golang-code-b213d9699998)
|
||||
// Runs Docker image
|
||||
func RunContainer(client *client.Client, imagename string, containername string, port string, inputEnv []string) error {
|
||||
// Define a PORT opening
|
||||
newport, err := natting.NewPort("tcp", port)
|
||||
if err != nil {
|
||||
fmt.Println("Unable to create docker port")
|
||||
return err
|
||||
}
|
||||
|
||||
// Configured hostConfig:
|
||||
// https://godoc.org/github.com/docker/docker/api/types/container#HostConfig
|
||||
hostConfig := &container.HostConfig{
|
||||
PortBindings: natting.PortMap{
|
||||
newport: []natting.PortBinding{
|
||||
{
|
||||
HostIP: "0.0.0.0",
|
||||
HostPort: port,
|
||||
},
|
||||
},
|
||||
},
|
||||
RestartPolicy: container.RestartPolicy{
|
||||
Name: "always",
|
||||
},
|
||||
LogConfig: container.LogConfig{
|
||||
Type: "json-file",
|
||||
Config: map[string]string{},
|
||||
},
|
||||
}
|
||||
|
||||
// Define Network config (why isn't PORT in here...?:
|
||||
// https://godoc.org/github.com/docker/docker/api/types/network#NetworkingConfig
|
||||
networkConfig := &network.NetworkingConfig{
|
||||
EndpointsConfig: map[string]*network.EndpointSettings{},
|
||||
}
|
||||
gatewayConfig := &network.EndpointSettings{
|
||||
Gateway: "gatewayname",
|
||||
}
|
||||
networkConfig.EndpointsConfig["bridge"] = gatewayConfig
|
||||
|
||||
// Define ports to be exposed (has to be same as hostconfig.portbindings.newport)
|
||||
exposedPorts := map[natting.Port]struct{}{
|
||||
newport: struct{}{},
|
||||
}
|
||||
|
||||
// Configuration
|
||||
// https://godoc.org/github.com/docker/docker/api/types/container#Config
|
||||
config := &container.Config{
|
||||
Image: imagename,
|
||||
Env: inputEnv,
|
||||
ExposedPorts: exposedPorts,
|
||||
Hostname: fmt.Sprintf("%s-hostnameexample", imagename),
|
||||
}
|
||||
|
||||
// Creating the actual container. This is "nil,nil,nil" in every example.
|
||||
cont, err := client.ContainerCreate(
|
||||
context.Background(),
|
||||
config,
|
||||
hostConfig,
|
||||
networkConfig,
|
||||
containername,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Run the actual container
|
||||
client.ContainerStart(context.Background(), cont.ID, types.ContainerStartOptions{})
|
||||
log.Printf("Container %s is created", cont.ID)
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user