Develop with Docker Engine SDKs and API

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

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.

A given version of the Docker Engine SDK supports a specific version of the Docker Engine API, as well as all earlier versions. If breaking changes occur, they are documented prominently.

Daemon and client API mismatches

The Docker daemon and client do not necessarily need to be the same version at all times. However, keep the following in mind.

  • If the daemon is newer than the client, the client does not know about new features or deprecated API endpoints in the daemon.

  • If the client is newer than the daemon, the client can request API endpoints that the daemon does not know about.

A new version of the API is released when new features are added.

The Docker API is backward-compatible, so you do not need to update code that uses the API unless you need to take advantage of new features.

To see the highest version of the API your Docker daemon and client support, use docker version:

$ docker version

Client:
Version: 17.04.0-ce
API version: 1.28
Go version: go1.7.5
Git commit: 4845c56
Built: Wed Apr 5 06:06:36 2017
OS/Arch: darwin/amd64 Server:
Version: 17.04.0-ce
API version: 1.28 (minimum version 1.12)
Go version: go1.7.5
Git commit: 4845c56
Built: Tue Apr 4 00:37:25 2017
OS/Arch: linux/amd64
Experimental: true

  

You can specify the API version to use, in one of the following ways:

  • When using the SDK, use the latest version you can, but at least the version that incorporates the API version with the features you need.

  • When using curl directly, specify the version as the first part of the URL. For instance, if the endpoint is /containers/, you can use /v1.27/containers/.

  • To force the Docker CLI or the Docker Engine SDKs to use an old version version of the API than the version reported by docker version, set the environment variable DOCKER_API_VERSION to the correct version. This works on Linux, Windows, or macOS clients.

    DOCKER_API_VERSION='1.27'

    While the environment variable is set, that version of the API is used, even if the Docker daemon supports a newer version.

  • For the SDKs, you can also specify the API version programmatically, as a parameter to the client object. See the Go constructor or the Python SDK documentation for client.

Docker EE and CE API mismatch

If you use Docker EE in production, we recommend using Docker EE in development too.

If you can’t, such as when your developers use Docker for Mac or Docker for Windows and manually build and push images, then your developers need to configure their Docker clients to use the same version of the API reported by their Docker daemon. This prevents the developer from using a feature that is not yet supported on the daemon where the workload runs in production. You can do this one of two ways:

  • Configure the Docker client to connect to an external daemon running Docker EE. You can use the -H flag on the docker command or set the DOCKER_HOST environment variable. The client uses the daemon’s latest supported API version.
  • Configure the Docker client to use a specific API by setting the DOCKER_API_VERSION environment variable to the API version to use, such as 1.30.

API version matrix

Docker does not recommend running versions prior to 1.12, which means you are encouraged to use an API version of 1.24 or higher.

Docker version Maximum API version Change log
18.05 1.37 changes
18.04 1.37 changes
18.03 1.37 changes
18.02 1.36 changes
17.12 1.35 changes
17.11 1.34 changes
17.10 1.33 changes
17.09 1.32 changes
17.07 1.31 changes
17.06 1.30 changes
17.05 1.29 changes
17.04 1.28 changes
17.03.1 1.27 changes
17.03 1.26 changes
1.13.1 1.26 changes
1.13 1.25 changes
1.12 1.24 changes
1.11 1.23 changes
1.10 1.22 changes
1.9 1.21 changes
1.8 1.20 changes
1.7 1.19 changes
1.6 1.18 changes

Choose the SDK or API version to use

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 do 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.

SDK and API quickstart

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

Go:

package main

import (
"io"
"os" "github.com/docker/docker/client"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/pkg/stdcopy" "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)
} stdcopy.StdCopy(os.Stdout, os.Stderr, out)
}

  

Python

import docker
client = docker.from_env()
print client.containers.run("alpine", ["echo", "hello", "world"])

  

HTTP

$ 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 getting started guide.

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.

Language Library
C libdocker
C# Docker.DotNet
C++ lasote/docker_client
Dart bwu_docker
Erlang erldocker
Gradle gradle-docker-plugin
Groovy docker-client
Haskell docker-hs
HTML (Web Components) docker-elements
Java docker-client
Java docker-java
Java docker-java-api
NodeJS dockerode
NodeJS harbor-master
Perl Eixo::Docker
PHP Docker-PHP
Ruby docker-api
Rust docker-rust
Rust shiplift
Scala tugboat
Scala reactive-docker
Swift docker-client-swift

  

Docker Engine SDKs and API 的开发1的更多相关文章

  1. Docker Engine SDKs and API 的开发2

    Examples using the Docker Engine SDKs and Docker API After you install Docker, you can install the G ...

  2. API接口开发 配置、实现、测试

    Yii2 基于RESTful架构的 advanced版API接口开发 配置.实现.测试 环境配置: 开启服务器伪静态 本处以apache为例,查看apache的conf目录下httpd.conf,找到 ...

  3. Docker Machine 和 Docker Engine 的区别

    Docker Engine 当人们提到 Docker,一般而言,大家说的是 Docker Engine,如下图:  它是一个 client-server application. Docker Eng ...

  4. Install Docker Engine on CentOS 在CentOS 7 上安装Docker

    Install Docker Engine on CentOS OS Requirements 系统要求 To install Docker Engine,you need a maintained ...

  5. 在Ubuntu上安装Docker Engine

    在Ubuntu上安装Docker Engine 这篇文章是介绍如何在在Ubuntu上安装Docker Engine,就是Google翻译官方文档的版本,英语好的直接官方原文.原文 要在Ubuntu上开 ...

  6. 启动docker报错Failed to listen on Docker Socket for the API.

    1.启动时报错查看日志发现 # journalctl -xe Failed to listen on Docker Socket for the API. 查找socket这个配置文件,修改如下 # ...

  7. Google Map API V3开发(1)

    Google Map API V3开发(1) Google Map API V3开发(2) Google Map API V3开发(3) Google Map API V3开发(4) Google M ...

  8. Google Map API V3开发(2)

    Google Map API V3开发(1) Google Map API V3开发(2) Google Map API V3开发(3) Google Map API V3开发(4) Google M ...

  9. Google Map API V3开发(3)

    Google Map API V3开发(1) Google Map API V3开发(2) Google Map API V3开发(3) Google Map API V3开发(4) Google M ...

随机推荐

  1. [openjudge-搜索]广度优先搜索之鸣人和佐助

    题目描述 描述 佐助被大蛇丸诱骗走了,鸣人在多少时间内能追上他呢?已知一张地图(以二维矩阵的形式表示)以及佐助和鸣人的位置.地图上的每个位置都可以走到,只不过有些位置上有大蛇丸的手下,需要先打败大蛇丸 ...

  2. 正确把mysql数据库从windows迁移到linux系统上的方法

    (一)用mysqldump命令导出数据库文件: 在windows下cd到Mysql的bin目录: c:/data.txt这个目录和导出的文本名可以自己随便取,-B 后面的是表名,我要导出的表明叫use ...

  3. Python大神成长之路: 第三次学习记录 集合 函数 装饰 re

    学习记录day03   字符串可以直接切片,But字符串不可修改 字符串修改:生成了一个新的字符串 LIst修改,在原基础上修改(原内存上)     集合是一个无序的,不重复的数据组合,它的主要作用如 ...

  4. react修改app.js添加中文内容后中文部分乱码解决

    [问题]:配置完react后修改app.js内容时添加中文出现如下乱码的中文. [A解决]文档——文本编码——转换文本编码,在弹出窗口修改,确定,搞定 [B解决]首先在EditPlus内:工具——首选 ...

  5. vc709时钟信号报单端信号错误的记录

    话说,为什么我又要跑去搞fpga玩了,不是应该招个有经验的开发人员么?大概是练度不够吧…… Xilinx这个板子阿,真鸡儿贵,我这还没啥基础,慢慢试吧: 看了乱七八糟各种文档先不提,我还是决定先控制L ...

  6. 75.Java异常处理机制-自定义异常

    package testDate; //自定义异常 public class MyException extends Exception{ public MyException(){ } public ...

  7. Elasticsearch集群内的原理

        一个运行中的 Elasticsearch 实例称为一个 节点,而集群是由一个或者多个拥有相同 cluster.name 配置的节点组成, 它们共同承担数据和负载的压力.当有节点加入集群中或者从 ...

  8. Win10+Ubuntu16.04双系统安装过程中遇到的一些问题及解决办法

    前两天闲来无聊重装了系统,装的是win10 64的系统,后来心血来潮索性再装Ubuntu,搞成win10+Ubuntu双系统. win10系统的镜像文件可以通过MSDN网站获取,MSDN是一个很可靠的 ...

  9. yield的理解

    yield的理解:yield命令是异步两个阶段的分界线需要先对迭代器和生成器进行理解: 迭代器:是一种支持next()操作的对象.它包含一组元素,当执行next()时,返回其中一个元素:当所有元素都被 ...

  10. Oracle12.2中新增的分区功能

    Oracle 12.2已经发布一段时间,公网上也可以下载试用.针对12.2,partitioning(分区)也有了不少增强. 自动列表分区 多字段列表分区 只读分区 分区维护时允许过滤 在线转换非分区 ...