feat: 10 funciones infra Docker y 2 tipos — operaciones de contenedores e imagenes
Funciones Docker: list/start/stop/remove containers, list/pull/remove images, inspect, logs, run. Tipos: ContainerInfo e ImageInfo. Pre-existentes en el dominio infra, ahora registrados.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package infra
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DockerRunOpts opciones para ejecutar un contenedor Docker.
|
||||
type DockerRunOpts struct {
|
||||
Name string // Nombre del contenedor (opcional)
|
||||
Ports []string // Mapeo de puertos, ej: ["8080:80", "443:443"]
|
||||
Env map[string]string // Variables de entorno
|
||||
Volumes []string // Bind mounts, ej: ["/host/path:/container/path"]
|
||||
Detach bool // Ejecutar en background
|
||||
Remove bool // Eliminar al terminar (--rm)
|
||||
Network string // Red Docker (opcional)
|
||||
}
|
||||
|
||||
// DockerRunContainer ejecuta un contenedor Docker nuevo con la imagen y opciones dadas.
|
||||
// Devuelve el ID del contenedor creado.
|
||||
func DockerRunContainer(image string, opts DockerRunOpts) (string, error) {
|
||||
args := []string{"run"}
|
||||
|
||||
if opts.Detach {
|
||||
args = append(args, "-d")
|
||||
}
|
||||
if opts.Remove {
|
||||
args = append(args, "--rm")
|
||||
}
|
||||
if opts.Name != "" {
|
||||
args = append(args, "--name", opts.Name)
|
||||
}
|
||||
if opts.Network != "" {
|
||||
args = append(args, "--network", opts.Network)
|
||||
}
|
||||
for _, p := range opts.Ports {
|
||||
args = append(args, "-p", p)
|
||||
}
|
||||
for k, v := range opts.Env {
|
||||
args = append(args, "-e", k+"="+v)
|
||||
}
|
||||
for _, vol := range opts.Volumes {
|
||||
args = append(args, "-v", vol)
|
||||
}
|
||||
|
||||
args = append(args, image)
|
||||
|
||||
out, err := exec.Command("docker", args...).CombinedOutput()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("docker run %s: %s", image, strings.TrimSpace(string(out)))
|
||||
}
|
||||
|
||||
return strings.TrimSpace(string(out)), nil
|
||||
}
|
||||
Reference in New Issue
Block a user