This day focuses on Containerization with Go. You will learn how to build and manage Docker containers using Go and explore libraries like docker/docker.
- Task 1: Write a Dockerfile to containerize a Go application.
- Task 2: Use the
docker/dockerlibrary to interact with Docker from a Go program. - Task 3: Build a CLI tool to manage Docker containers (e.g., start, stop, list).
// Example: Listing Docker Containers with Go
package main
import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func main() {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
if err != nil {
panic(err)
}
for _, container := range containers {
fmt.Printf("Container ID: %s, Image: %s, Status: %s\n", container.ID[:10], container.Image, container.Status)
}
}- Write a Dockerfile to containerize a Go application and build the image.
- Use the
docker/dockerlibrary to create a Go program that starts and stops containers. - Build a CLI tool to list all running containers and their statuses.