Docker SDK practices (Windows, Ubuntu, MacOS)

Source: Internet
Author: User
Tags docker ps

Docker SDK Practice Windowsubuntumacos Windows readiness to operate in a program Docker Linux Maxos

Docker SDK practices (Windows, Ubuntu, MacOS)

Here's how to use the Docker API in Python, and here's the Docker-py

The use of Windows, Linux (Ubuntu), MacOS under the Docker SDK is described here (code uses Python, other languages can refer to official documentation)

Note that the following is for personal understanding and if you are wrong, windows

Practice system: Windows 64bit preparation work

Docker launched Docker-windows for Windows 10, but the hyer-v feature needs to be turned on, which can cause VMware workstation to work, so docker-toolbox is used.

After you install Docker-toolbox in the Official document, start Docker QuickStart terminal to see the following output:


                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~< c9/>\______ o           __/
             \         __/
              \____\_______/Docker is configured to use the

default machine  With IP 192.168.99.100
to help getting started, check out of the docs at https://docs.docker.com

Start Interactive Shell

At this point, you can perform Docker operations (such as Docker PS, Docker iamges, etc.) at the current terminal interface.

Note that these commands are limited to the current terminal, the other open the CMD terminal because the inability to connect to the Docker server and failed, related 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 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 is run elevated to connect. This error could also indicate that the Docker daemon are not running.

This is because Docker server is running in a virtual machine (ip=192.168.99.100), so if you want to use the Docker API in our own programs, you need to use the Docker remote API

First locate the IP port of the Docker server and use the Docker-machine ls command to view:

Λ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 is tcp://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?

With the above command, you can see that you want to connect to the Docker server using TLS

In the CMD terminal, set the following environment variables to successfully link:

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
# can get normal results at this time

Where docker_cert_path can view operations in a program through the docker-machine Config command docker

Now we can start to manipulate the Docker in the program, assuming that the Python Docker Library is installed (use PIP install Docker for installation if not installed)

Import Docker # define configuration ' Docker configuration, fill in according to the actual situation ' Docker_host = ' tcp://192.168.99.100:2376 ' Docker_cert_path = ' c:\\users\\ Administrator\\.docker\\machine\\certs "docker_tls_verify = 1" # test_docker.py # # There are two ways to link Docker Server # # Way one: Through Modify the TEMP environment variable if docker_host is not none:os.environ[' docker_host ' = Docker_host if Docker_cert_path isn't not NONE:OS.E nviron[' docker_cert_path '] = Docker_cert_path if docker_tls_verify is not none:os.environ[' docker_tls_verify '] = DOCK Er_tls_verify client = docker.from_env () # mode two: Use tlsconfig # configuration tlsconfig, see: 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) #测试链接是否成功, Output image list Client.images.list () # [# <ima GE: ' Amancevice/superset:lateSt ', # <image: ' Mysql/mysql-cluster:latest ' > #] 
Linux

Practice System: Ubuntu 16.04

By default, Docker uses Unix:///var/run/docker.sock to communicate with Unix sockets and can be directly linked if Python is running locally:

Import Docker

client = docker.from_env ()

#输出 image list
client.images.list ()
# [
#   <image: ' Amancevice/superset:latest ', 
#   <image: ' Mysql/mysql-cluster:latest ' >
#]
Maxos

Practice System: Mac OS X 10.11.6

Start the Docker program and use the same code as Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.