docker sdk practice (windows, ubuntu, macOS)
Here is how to use it in python
docker api
, here is docker-py
Here we will introduce the usage practices of windows
, linux(Ubuntu)
, and macOS
below docker SDK
(code usage python
, other languages can refer to official documents )
Note
- The following content is only for personal understanding, please feel free to correct me if I am wrong
windows
Practice system: windows 10 64bit
Preparation
docker
For windows 10
the launch of docker-windows , but Hyer-V
the function needs to be enabled, which will cause it to fail to work properly, so docker-toolboxVMware Workstation
is used here .
After installing according to the official documentation docker-toolbox
, docker QuickStart Terminal
you can see the following output when you start it:
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com
Start interactive shell
At this point, you can perform operations on the current terminal interface docker
(such as docker ps, docker iamges, etc.)
Note that these commands are limited to the current terminal. If you open another cmd
terminal, docker server
it will fail because you cannot connect to it. The relevant prompts are as follows:
> docker version
Client:
Version: 17.10.0-ce
API version: 1.33
Go version: go1.8.3
Git commit: f4ffd25
Built: Tue Oct 17 19:00:02 2017
OS/Arch: windows/amd64
error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.33/version: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run elevated to connect. This error may also indicate that the docker daemon is not running.
This is because docker server
it is running in a virtual machine (ip=192.168.99.100), so if you want to use it in our own program docker api
, you need to usedocker remote api
First find the ip and port of the docker server, and use docker-machine ls
the command to view it:
λ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default - virtualbox Running tcp://192.168.99.100:2376 v17.10.0-ce
You can see that the URL istcp://192.168.99.100:2376
λ docker -H tcp://192.168.99.100:2376 ps
Get http://192.168.99.100:2376/v1.33/containers/json: malformed HTTP response "\x15\x03\x01\x00\x02\x02".
* Are you trying to connect to a TLS-enabled daemon without TLS?
Through the above command, you can see that if you want to connect to the docker server, you need to useTLS
In the cmd terminal, set the following environment variables to link successfully:
set DOCKER_HOST=tcp://192.168.99.100:2376
set DOCKER_CERT_PATH=C:/Users/Administrator/.docker/machine/certs
set DOCKER_TLS_VERIFY=1
docker images
# 此时能得到正常的结果
Which DOCKER_CERT_PATH
can docker-machine config
be viewed with the command
Operate Docker in the program
Now we can start to operate in the program docker
, assuming that the python docker library has been installed (if not installed, use pip install docker
to install)
import docker
# 定义配置
'''
Docker 配置,根据实际情况填写
'''
DOCKER_HOST = "tcp://192.168.99.100:2376"
DOCKER_CERT_PATH = "C:\\Users\\Administrator\\.docker\\machine\\certs"
DOCKER_TLS_VERIFY = "1"
# test_docker.py
#
# 这里使用两种方式链接 docker server
#
# 方式一:通过修改临时环境变量
if DOCKER_HOST is not None:
os.environ['DOCKER_HOST'] = DOCKER_HOST
if DOCKER_CERT_PATH is not None:
os.environ['DOCKER_CERT_PATH'] = DOCKER_CERT_PATH
if DOCKER_TLS_VERIFY is not None:
os.environ['DOCKER_TLS_VERIFY'] = DOCKER_TLS_VERIFY
client = docker.from_env()
# 方式二:使用 TLSConfig
# 配置 TLSConfig,详见:http://docker-py.readthedocs.io/en/stable/tls.html#docker.tls.TLSConfig
tls_config = docker.tls.TLSConfig(
ca_cert=DOCKER_CERT_PATH+"/ca.pem",
client_cert=(
DOCKER_CERT_PATH+'/cert.pem',
DOCKER_CERT_PATH+'/key.pem'
),
verify=True
)
client = docker.DockerClient(
base_url=DOCKER_HOST,
tls=tls_config
)
#测试链接是否成功,输出 image 列表
client.images.list()
# [
# <Image: 'amancevice/superset:latest'>,
# <Image: 'mysql/mysql-cluster:latest'>
# ]
Linux
Practice system: Ubuntu 16.04
By default, docker
use unix:///var/run/docker.sock
for Unix socket communication, if python
running locally, you can link directly:
import docker
client = docker.from_env()
#输出 image 列表
client.images.list()
# [
# <Image: 'amancevice/superset:latest'>,
# <Image: 'mysql/mysql-cluster:latest'>
# ]
maxOS
Practice system: Mac OS X 10.11.6
After starting docker
the program, use Linux
the same code as