Created
December 27, 2017 20:07
-
-
Save helmutkemper/c43e7c7ff8074b6b885f3a6e62fa725d to your computer and use it in GitHub Desktop.
golang docker sdk install ghost blog
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 main | |
import ( | |
"io" | |
"os" | |
"github.com/docker/docker/client" | |
"github.com/docker/docker/api/types" | |
"github.com/docker/docker/api/types/container" | |
"golang.org/x/net/context" | |
"github.com/docker/go-connections/nat" | |
) | |
func main() { | |
ctx := context.Background() | |
cli, err := client.NewEnvClient() | |
if err != nil { | |
panic(err) | |
} | |
_, err = cli.ImagePull(ctx, "ghost:latest", types.ImagePullOptions{}) | |
if err != nil { | |
panic(err) | |
} | |
config := &container.Config{ | |
Image: "ghost", | |
ExposedPorts: nat.PortSet{ | |
"8080/tcp": struct{}{}, | |
}, | |
} | |
hostConfig := &container.HostConfig{ | |
PortBindings: nat.PortMap{ | |
"2368/tcp": []nat.PortBinding{ | |
{ | |
HostPort: "8080", | |
}, | |
}, | |
}, | |
} | |
resp, err := cli.ContainerCreate(ctx, config, hostConfig, nil, "") | |
if err != nil { | |
panic(err) | |
} | |
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { | |
panic(err) | |
} | |
out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true, Details: true, Follow: true, ShowStderr: true, Timestamps: true}) | |
if err != nil { | |
panic(err) | |
} | |
io.Copy(os.Stdout, out) | |
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning) | |
select { | |
case err := <-errCh: | |
if err != nil { | |
panic(err) | |
} | |
case <-statusCh: | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if you habe problem in line 28, you need to remove folder you_libraries/github.com/docker/docker/vendor/github.com/docker/go-connections/nat