Created
December 17, 2024 11:39
-
-
Save praswicaksono/f1b6daebc779e8697f59371886e3e7a3 to your computer and use it in GitHub Desktop.
Golang create apisix container
This file contains hidden or 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
package docker | |
import ( | |
"context" | |
_ "embed" | |
"fmt" | |
"io" | |
"os" | |
"github.com/docker/docker/api/types/container" | |
"github.com/docker/docker/api/types/image" | |
"github.com/docker/docker/api/types/mount" | |
"github.com/docker/docker/api/types/network" | |
"github.com/docker/go-connections/nat" | |
v1 "github.com/opencontainers/image-spec/specs-go/v1" | |
"github.com/rs/zerolog/log" | |
"github.com/spf13/viper" | |
) | |
//go:embed include/apisix/server.yaml | |
var serverConfig []byte | |
//go:embed include/apisix/router.yaml | |
var routerConfig []byte | |
func (d *Docker) InstallStandaloneApisix(ctx context.Context) error { | |
home := viper.Get("HOME") | |
err := os.MkdirAll(fmt.Sprintf("%s/.turu/apisix/", home), os.ModePerm) | |
if err != nil { | |
return err | |
} | |
err = os.WriteFile(fmt.Sprintf("%s/.turu/apisix/server.yaml", home), serverConfig, 0666) | |
if err != nil { | |
return err | |
} | |
err = os.WriteFile(fmt.Sprintf("%s/.turu/apisix/router.yaml", home), routerConfig, 0666) | |
if err != nil { | |
return err | |
} | |
reader, err := d.DockerManager.ImagePull(ctx, "apache/apisix:3.11.0-debian", image.PullOptions{}) | |
if err != nil { | |
return err | |
} | |
defer reader.Close() | |
_, err = io.Copy(os.Stdout, reader) | |
if err != nil { | |
return err | |
} | |
log.Info().Msg("Image apache/apisix:3.11-debian has successfully pulled") | |
res, err := d.DockerManager.ContainerCreate( | |
ctx, | |
&container.Config{ | |
Image: "apache/apisix:3.11.0-debian", | |
Env: []string{ | |
"APISIX_STAND_ALONE=true", | |
}, | |
}, | |
&container.HostConfig{ | |
Mounts: []mount.Mount{ | |
{ | |
Type: mount.TypeBind, | |
Source: fmt.Sprintf("%s/.turu/apisix/server.yaml", viper.GetString("HOME")), | |
Target: "/usr/local/apisix/conf/config.yaml", | |
}, | |
{ | |
Type: mount.TypeBind, | |
Source: fmt.Sprintf("%s/.turu/apisix/router.yaml", viper.GetString("HOME")), | |
Target: "/usr/local/apisix/conf/apisix.yaml", | |
}, | |
}, | |
PortBindings: nat.PortMap{ | |
nat.Port("9080/tcp"): []nat.PortBinding{ | |
{ | |
HostIP: "0.0.0.0", | |
HostPort: "80", | |
}, | |
}, | |
nat.Port("9443/tcp"): []nat.PortBinding{ | |
{ | |
HostIP: "0.0.0.0", | |
HostPort: "443", | |
}, | |
}, | |
}, | |
}, | |
&network.NetworkingConfig{ | |
EndpointsConfig: map[string]*network.EndpointSettings{ | |
"mager": {}, | |
}, | |
}, | |
&v1.Platform{}, | |
"mager-proxy-apisix", | |
) | |
if err != nil { | |
return err | |
} | |
log.Info().Msg(fmt.Sprintf("Container created with id %s", res.ID)) | |
err = d.DockerManager.ContainerStart(ctx, res.ID, container.StartOptions{}) | |
if err != nil { | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment