Skip to content

Commit

Permalink
Add Docker container operations and related action
Browse files Browse the repository at this point in the history
The DockerContainer.go file has been expanded to include two main operations: starting and building a Docker container. Corresponding documentation and error handling have also been provided. Furthermore, the docker-build.go file introduces the creation of an action for Docker image building, including input/output properties and execution method.
  • Loading branch information
PiotrFerenc committed Apr 19, 2024
1 parent 934b96a commit 4fc5fb4
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
81 changes: 81 additions & 0 deletions cmd/worker/actions/docker/docker-build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package docker

import (
"context"
"github.com/PiotrFerenc/mash2/cmd/worker/actions"
"github.com/PiotrFerenc/mash2/internal/Container"
"github.com/PiotrFerenc/mash2/internal/types"
)

func CreateDockerBuild() actions.Action {
return &dockerImage{
dockerfile: actions.Property{
Name: "dockerfile",
Type: actions.Text,
Description: "The dockerfile to use for building the image",
DisplayName: "Dockerfile",
Validation: "required",
},
tags: actions.Property{
Name: "tags",
Type: actions.Text,
Description: "The tags to apply to the built image",
DisplayName: "Tags",
Validation: "",
},
imageId: actions.Property{
Name: "id",
Type: actions.Text,
Description: "The unique identifier for the built Docker image",
DisplayName: "Image ID",
Validation: "",
},
}
}

type dockerImage struct {
dockerfile actions.Property
tags actions.Property
imageId actions.Property
}

func (d *dockerImage) GetCategoryName() string {
return "docker"
}

func (d *dockerImage) Inputs() []actions.Property {
return []actions.Property{
d.dockerfile, d.tags,
}
}

func (d *dockerImage) Outputs() []actions.Property {
return []actions.Property{
d.imageId,
}
}

// Execute builds a Docker image using the provided Dockerfile content and tags.
//
// Parameters:
//
// process: The process object containing the Dockerfile content and tags.
//
// Returns:
//
// types.Process: The updated process object with the image ID set.
// error: An error if the image build process fails.
func (d *dockerImage) Execute(process types.Process) (types.Process, error) {
ctx := context.Background()
dockerfile, err := d.dockerfile.GetStringFrom(&process)
if err != nil {
return process, err
}
tags, err := d.tags.GetStringFrom(&process)
if err != nil {
return process, err
}
imageId, err := Container.BuildImage(dockerfile, tags, ctx)
process.SetString(d.imageId.Name, imageId)
return process, nil
}
64 changes: 64 additions & 0 deletions internal/Container/DockerContainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@ package Container

import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"os"
)

// StartContainer starts a Docker container with the specified image name, environment variables,
// volume bindings, and context.
//
// Parameters:
//
// imageName: The name of the Docker image to use for the container.
// env: The environment variables to set for the container.
// vol: The volume bindings for the container.
// ctx: The context to use for the Docker client.
//
// Returns:
//
// string: The ID of the container that was started.
// error: An error if the container failed to start.
func StartContainer(imageName string, env, vol []string, ctx context.Context) (string, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
Expand All @@ -27,3 +43,51 @@ func StartContainer(imageName string, env, vol []string, ctx context.Context) (s
}
return resp.ID, nil
}

// BuildImage builds a Docker image using the specified Dockerfile path, image name, and context.
//
// Parameters:
//
// dockerfilePath: The path to the Dockerfile used for building the image.
// imageName: The name of the image to be built.
// ctx: The context to use for the Docker client.
//
// Returns:
//
// string: The ID of the built image.
// error: An error if the image build process fails.
func BuildImage(dockerfilePath, imageName string, ctx context.Context) (string, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return "", err
}

buildContext, err := os.Open(dockerfilePath)
if err != nil {
return "", err
}
defer buildContext.Close()

options := types.ImageBuildOptions{
Dockerfile: dockerfilePath,
Tags: []string{imageName},
Remove: true,
}

response, err := cli.ImageBuild(ctx, buildContext, options)
if err != nil {
return "", err
}
defer response.Body.Close()

//body, err := io.ReadAll(response.Body)
//if err != nil {
// return "", err
//}
imgID, _, err := cli.ImageInspectWithRaw(ctx, imageName)
if err != nil {
return "", err
}

return imgID.ID, nil
}

0 comments on commit 4fc5fb4

Please sign in to comment.