0

I have enabled API on my remote ubuntu server on 2375 port.I want to develop a go application that using Docker API from remote.In examples and tutorials i can not see any options section that specify Docker API Server settings.Probably it is taking it from environment variables.(https://docs.docker.com/v17.09/develop/sdk/examples/#run-a-container)

Below code have this code section :

cli, err := client.NewEnvClient()

I think here is taking the Docker API settings.

I am a newbie programmer and thank you for any help.

package main

import (
    "io"
    "os"

    "github.com/docker/docker/api/types"
    "github.com/docker/docker/api/types/container"
    "github.com/docker/docker/client"
    "golang.org/x/net/context"
)

func main() {
    ctx := context.Background()
    cli, err := client.NewEnvClient()
    if err != nil {
        panic(err)
    }

    _, err = cli.ImagePull(ctx, "docker.io/library/alpine", types.ImagePullOptions{})
    if err != nil {
        panic(err)
    }

    resp, err := cli.ContainerCreate(ctx, &container.Config{
        Image: "alpine",
        Cmd:   []string{"echo", "hello world"},
    }, nil, nil, "")
    if err != nil {
        panic(err)
    }

    if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
        panic(err)
    }

    statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
    select {
    case err := <-errCh:
        if err != nil {
            panic(err)
        }
    case <-statusCh:
    }

    out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
    if err != nil {
        panic(err)
    }

    io.Copy(os.Stdout, out)
}
akuscu
  • 465
  • 1
  • 3
  • 13
  • 1
    The documention you are following is for Docker 17.09 which dates back to 2017; if you take a look at the [latest version](https://docs.docker.com/develop/sdk/examples/) you might find it easier (unless you are using an older version of Docker?). The function you are using does pull the host etc from the environent; this is stated in the [docs](https://godoc.org/github.com/docker/docker/client#NewEnvClient) which also mention that the function is depreciated (use [NewClientWithOpts](https://godoc.org/github.com/docker/docker/client#NewClientWithOpts) instead). – Brits Jan 28 '20 at 08:46
  • One other question; have you secured port 2375? (opening this to the internet is a [risk](https://docs.docker.com/engine/security/https/)) – Brits Jan 28 '20 at 08:50
  • Because of it is for test purpose ,i did not secure the port.I am using docker 18.09 version. – akuscu Jan 28 '20 at 10:21
  • 1
    Saying "it's a risk" is understanding things a little: letting the Docker daemon accept connections over the network lets _anyone_ who can reach the system have unrestricted root access with no restrictions, authentication, or logging. Unless you're positive you have a correct TLS setup, **disable Docker access on port 2375 immediately**. – David Maze Jan 28 '20 at 11:22

1 Answers1

0

I used DOCKER_HOST environment to point my docker API adress and it is working

export DOCKER_HOST=tcp://<my-docker-ip>:<port>
akuscu
  • 465
  • 1
  • 3
  • 13