Develop with Docker Engine SDKs

Table of Contents

Develop with Docker Engine SDKs

Install the SDKs

Go SDK

Python SDK

View the API reference

Versioned API and SDK

SDK and API quickstart

Unofficial libraries


 

Develop with Docker Engine SDKs

Estimated reading time: 7 minutes

Docker provides an API for interacting with the Docker daemon (called the Docker Engine API), as well as SDKs for Go and Python. The SDKs allow you to build and scale Docker apps and solutions quickly and easily. If Go or Python don’t work for you, you can use the Docker Engine API directly.

The Docker Engine API is a RESTful API accessed by an HTTP client such as wget or curl, or the HTTP library which is part of most modern programming languages.

Install the SDKs

Use the following commands to install the Go or Python SDK. Both SDKs can be installed and coexist together.

Go SDK

go get github.com/docker/docker/client

The client requires a recent version of Go. Run go version and ensure that you are running a currently supported version of Go

Read the full Docker Engine Go SDK reference.

Python SDK

  • Recommended: Run pip install docker.

  • If you can’t use pip:

    1. Download the package directly.
    2. Extract it and change to the extracted directory,
    3. Run python setup.py install.

Read the full Docker Engine Python SDK reference.

View the API reference

You can view the reference for the latest version of the API or choose a specific version.

Versioned API and SDK

The version of the Docker Engine API you should use depends upon the version of your Docker daemon and Docker client. Refer to the versioned API and SDK section in the API documentation for details.

SDK and API quickstart

Use the following guidelines to choose the SDK or API version to use in your code:

  • If you’re starting a new project, use the latest version, but use API version negotiation or specify the version you are using. This helps prevent surprises.
  • If you need a new feature, update your code to use at least the minimum version that supports the feature, and prefer the latest version you can use.
  • Otherwise, continue to use the version that your code is already using.

As an example, the docker run command can be easily implemented using the Docker API directly, or using the Python or Go SDK.

package main

import (
	"context"
	"io"
	"os"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/container"
	"github.com/docker/docker/client"
	"github.com/docker/docker/pkg/stdcopy"
)

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

    reader, err := cli.ImagePull(ctx, "docker.io/library/alpine", types.ImagePullOptions{})
    if err != nil {
        panic(err)
    }
    io.Copy(os.Stdout, reader)

    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)
    }

    stdcopy.StdCopy(os.Stdout, os.Stderr, out)
}
import docker
client = docker.from_env()
print client.containers.run("alpine", ["echo", "hello", "world"])
$ curl --unix-socket /var/run/docker.sock -H "Content-Type: application/json" \
  -d '{"Image": "alpine", "Cmd": ["echo", "hello world"]}' \
  -X POST http:/v1.24/containers/create
{"Id":"1c6594faf5","Warnings":null}

$ curl --unix-socket /var/run/docker.sock -X POST http:/v1.24/containers/1c6594faf5/start

$ curl --unix-socket /var/run/docker.sock -X POST http:/v1.24/containers/1c6594faf5/wait
{"StatusCode":0}

$ curl --unix-socket /var/run/docker.sock "http:/v1.24/containers/1c6594faf5/logs?stdout=1"
hello world

For more examples, take a look at the SDK examples.

Unofficial libraries

There are a number of community supported libraries available for other languages. They have not been tested by Docker, so if you run into any issues, file them with the library maintainers.

LanguageLibrary
Clibdocker
C#Docker.DotNet
C++lasote/docker_client
Dartbwu_docker
Erlangerldocker
Gradlegradle-docker-plugin
Groovydocker-client
Haskelldocker-hs
HTML (Web Components)docker-elements
Javadocker-client
Javadocker-java
Javadocker-java-api
NodeJSdockerode
NodeJSharbor-master
PerlEixo::Docker
PHPDocker-PHP
Rubydocker-api
Rustdocker-rust
Rustshiplift
Scalatugboat
Scalareactive-docker
Swiftdocker-client-swift

developingsdk

 

 

 

 

 

 

 

 

 

 

 

 

已标记关键词 清除标记
【为什么还需要学习C++?】 你是否接触很多语言,但从来没有了解过编程语言的本质? 你是否想成为一名资深开发人员,想开发别人做不了的高性能程序? 你是否经常想要窥探大型企业级开发工程的思路,但苦于没有基础只能望洋兴叹?   那么C++就是你个人能力提升,职业之路进阶的不二之选。 【课程特色】 1.课程共19大章节,239课时内容,涵盖数据结构、函数、类、指针、标准库全部知识体系。 2.带你从知识与思想的层面从0构建C++知识框架,分析大型项目实践思路,为你打下坚实的基础。 3.李宁老师结合4大国外顶级C++著作的精华为大家推出的《征服C++11》课程。 【学完后我将达到什么水平?】 1.对C++的各个知识能够熟练配置、开发、部署; 2.吊打一切关于C++的笔试面试题; 3.面向物联网的“嵌入式”和面向大型化的“分布式”开发,掌握职业钥匙,把握行业先机。 【面向人群】 1.希望一站式快速入门的C++初学者; 2.希望快速学习 C++、掌握编程要义、修炼内功的开发者; 3.有志于挑战更高级的开发项目,成为资深开发的工程师。 【课程设计】 本课程包含3大模块 基础篇 本篇主要讲解c++的基础概念,包含数据类型、运算符等基本语法,数组、指针、字符串等基本词法,循环、函数、类等基本句法等。 进阶篇 本篇主要讲解编程中常用的一些技能,包含类的高级技术、类的继承、编译链接和命名空间等。 提升篇: 本篇可以帮助学员更加高效的进行c++开发,其中包含类型转换、文件操作、异常处理、代码重用等内容。
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页