SlideShare a Scribd company logo
Docker Remote API &
docker-py
Eric Ahn
2016/07
$ docker-machine create -d virtualbox default
$ docker-machine env default
$ docker-machine ls default
$ curl https://192.168.99.100:2376/images/json 
—cert $DOCKER_CERT_PATH/cert.pem 
—key $DOCKER_CERT_PATH/key.pem 
—cacert $DOCKER_CERT_PATH/ca.pem
curl: (58) SSL: Can't load the certificate "/Users/EricAhn/.docker/machine/
machines/default/cert.pem" and its private key: OSStatus -25299
password?
$ cd ~/.docker/machine/machines/default
$ openssl pkcs12 -export 
> -inkey key.pem 
> -in cert.pem 
> -CAfile ca.pem 
> -name client-side 
> -out cert.p12 
> -password pass:mypass
$ curl https://192.168.99.100:2376/images/json 
—cert $DOCKER_CERT_PATH/cert.p12 
—pass mypass 
—key $DOCKER_CERT_PATH/key.pem 
—cacert $DOCKER_CERT_PATH/ca.pem
[{"Id":"sha256:0d409d33b27e47423b049f7f863faa08655a8c901749c2b25b93ca6
7d01a470d","ParentId":"","RepoTags":["nginx:latest"],"RepoDigests":null,"Created":
1464804059,"Size":182786784,"VirtualSize":182786784,"Labels":{}}]
Generate cert.p12
with password
List images :
“nginx:latest”
Result
$ curl -X POST https://192.168.99.100:2376/images/create?fromImage=nginx:latest 
—cert $DOCKER_CERT_PATH/cert.p12 
—pass mypass 
—key $DOCKER_CERT_PATH/key.pem 
—cacert $DOCKER_CERT_PATH/ca.pem
{"status":"Pulling from library/nginx","id":"latest"}
{"status":"Digest:
sha256:0fe6413f3e30fcc5920bc8fa769280975b10b1c26721de956e1428b9e2f29d04"}
{"status":"Status: Image is up to date for nginx:latest"}
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 0d409d33b27e 7 weeks ago 182.8 MB
Download “nginx:latest” image
Result
Check using command line
$ curl -v -X POST -H "Content-Type: application/json" -d '
{ "Image": "nginx"
}' https://192.168.99.100:2376/containers/create 
--cert $DOCKER_CERT_PATH/cert.p12 
--pass mypass 
--key $DOCKER_CERT_PATH/key.pem 
--cacert $DOCKER_CERT_PATH/ca.pem
{"Id":"8c6e0e97330c08461cdfc179a524ad56c2b0c3a2ba95fc5909a999d
75eb44b37","Warnings":null}
Create container
Result a message
$ curl -X POST -H "Content-Type: application/json" 
> -d '{ "PortBindings": {"80/tcp": [{ "HostPort": "80"}]} }' 
> https://192.168.99.100:2376/containers/8c6e0e97330c/start 
> --cert $DOCKER_CERT_PATH/cert.p12 
> --pass mypass 
> --key $DOCKER_CERT_PATH/key.pem 
> --cacert $DOCKER_CERT_PATH/ca.pem
{"message":"starting container with HostConfig was deprecated
since v1.10 and removed in v1.12"}
Set a specific HostConfig
Result : Failed with
HostConfig
$ curl -v https://192.168.99.100:2376/containers/json?all=1 
--cert $DOCKER_CERT_PATH/cert.p12 
--pass mypass 
--key $DOCKER_CERT_PATH/key.pem 
--cacert $DOCKER_CERT_PATH/ca.pem
[{"Id":"4ee9a6f6b24715266ae8037fd069e2439d67c0198ee8f96b531682d61363f527","Na
mes":["/
hello"],"Image":"nginx:latest","ImageID":"sha256:0d409d33b27e47423b049f7f863faa08655
a8c901749c2b25b93ca67d01a470d","Command":"nginx -g 'daemon off;'","Created":
1469639718,"Ports":[{"PrivatePort":443,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":
80,"PublicPort":80,"Type":"tcp"}],"Labels":{},"State":"running","Status":"Up 14
minutes","HostConfig":{"NetworkMode":"default"},"NetworkSettings":{"Networks":{"bridge":
{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"f22986e826e252df0d0f06011158
9f0a732993c6636ba47e549911ccaf8f8ed8","EndpointID":"41513d5005aa2777561b6c0c
d3f3f6ae44fa7d6d47d261734391e206cdade7b6","Gateway":"172.17.0.1","IPAddress":"17
2.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":
0,"MacAddress":"02:42:ac:11:00:02"}}},"Mounts":[]}]
List all containers
Result ( json )
$ curl -X POST -H "Content-Type: application/json" 
https://192.168.99.100:2376/containers/8c6e0e97330c/start 
--cert $DOCKER_CERT_PATH/cert.p12 
--pass mypass 
--key $DOCKER_CERT_PATH/key.pem 
--cacert $DOCKER_CERT_PATH/ca.pem
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8c6e0e97330c nginx "nginx -g 'daemon off" 2 minutes ago Up 1 seconds 80/tcp, 443/tcp goofy_ardinghelli
Start a container
Running status
$ cd ~/_Python
$ sudo easy_install pip
$ sudo pip install virtualenv
$ virtualenv foobar
$ source foobar/bin/activate
(foobar) $ pip install docker-py
Collecting docker-py
Downloading docker_py-1.9.0-py2.py3-none-any.whl (42kB)
Collecting requests>=2.5.2 (from docker-py)
Downloading requests-2.10.0-py2.py3-none-any.whl (506kB)
100% | | 512kB 938kB/s
Collecting six>=1.4.0 (from docker-py)
Downloading six-1.10.0-py2.py3-none-any.whl
Collecting websocket-client>=0.32.0 (from docker-py)
Downloading websocket_client-0.37.0.tar.gz (194kB)
100% | | 194kB 1.4MB/s
Collecting backports.ssl-match-hostname>=3.5; python_version < "3.5" (from docker-py)
Downloading backports.ssl_match_hostname-3.5.0.1.tar.gz
Building wheels for collected packages: websocket-client, backports.ssl-match-hostname
Running setup.py bdist_wheel for websocket-client ... done
Stored in directory: /Users/EricAhn/Library/Caches/pip/wheels/e4/20/12/48d6732b88733a0a8f98ee1a5b2f59b64b27011d5c34c4f98c
…..
Install python and virtualenv
(foobar) $ vi docker-run.py
import docker
tls_config = docker.tls.TLSConfig(
client_cert=('/Users/EricAhn/.docker/machine/machines/default/cert.pem',
'/Users/EricAhn/.docker/machine/machines/default/key.pem'),
verify='/Users/EricAhn/.docker/machine/machines/default/ca.pem'
)
client = docker.Client(base_url='tcp://192.168.99.100:2376', tls=tls_config)
client.containers()
container_id = client.create_container(
image='nginx:latest',
ports=[80],
name='hello'
)
client.start(
container_id,
port_bindings={80: ('0.0.0.0', 80)}
)
$ python docker-run.py
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ee9a6f6b247 nginx:latest "nginx -g 'daemon off" 2 seconds ago Up 1 seconds 0.0.0.0:80->80/tcp, 443/tcp hello
Python code
Run code and check

More Related Content

PDF
Docker command
PPTX
Real World Lessons on the Pain Points of Node.js Applications
PDF
Automating Mendix application deployments with Nix
PPTX
Lessons from running potentially malicious code inside containers
PPTX
Running Docker in Development & Production (#ndcoslo 2015)
PDF
Docker deploy
PDF
Docker and friends at Linux Days 2014 in Prague
PPTX
Docker practice
Docker command
Real World Lessons on the Pain Points of Node.js Applications
Automating Mendix application deployments with Nix
Lessons from running potentially malicious code inside containers
Running Docker in Development & Production (#ndcoslo 2015)
Docker deploy
Docker and friends at Linux Days 2014 in Prague
Docker practice

What's hot (20)

PDF
Securing Prometheus exporters using HashiCorp Vault
PDF
How to create your own hack environment
PPTX
Lessons from running potentially malicious code inside Docker containers
PPTX
2012 coscup - Build your PHP application on Heroku
PDF
Docker, c'est bonheur !
PDF
Adrian Mouat - Docker Tips and Tricks
PDF
CoreOS + Kubernetes @ All Things Open 2015
PDF
Fabric workshop(1) - (MOSG)
PDF
Preparation study of_docker - (MOSG)
PDF
Testing your infrastructure with litmus
PDF
Docker in practice
PDF
CoreOS: Control Your Fleet
PDF
Hyperledger composer
PDF
Ondřej Šika: Docker, Traefik a CI - Mějte nasazené všeny větve na kterých pra...
PPTX
Nomad + Flatcar: a harmonious marriage of lightweights
PDF
Vagrant for real (codemotion rome 2016)
PPTX
Start tracking your ruby infrastructure
PDF
Using Nix and Docker as automated deployment solutions
PDF
Tensorflow in Docker
PDF
Deploying Plone and Volto, the Hard Way
Securing Prometheus exporters using HashiCorp Vault
How to create your own hack environment
Lessons from running potentially malicious code inside Docker containers
2012 coscup - Build your PHP application on Heroku
Docker, c'est bonheur !
Adrian Mouat - Docker Tips and Tricks
CoreOS + Kubernetes @ All Things Open 2015
Fabric workshop(1) - (MOSG)
Preparation study of_docker - (MOSG)
Testing your infrastructure with litmus
Docker in practice
CoreOS: Control Your Fleet
Hyperledger composer
Ondřej Šika: Docker, Traefik a CI - Mějte nasazené všeny větve na kterých pra...
Nomad + Flatcar: a harmonious marriage of lightweights
Vagrant for real (codemotion rome 2016)
Start tracking your ruby infrastructure
Using Nix and Docker as automated deployment solutions
Tensorflow in Docker
Deploying Plone and Volto, the Hard Way
Ad

Viewers also liked (20)

PDF
Py conkr 20150829_docker-python
PDF
Présentation Ikoula au Meet-up Docker à l'école 42
PDF
Py conkr 20150829_docker-python
PDF
Http capturing
PDF
High perforance-browse-networking-2015-bwahn
PDF
Cdn gslb-20151209
PPTX
Introduction to Tensorflow
PDF
Keep it simple web development stack
PDF
Docker en production et la sécurité … _
PDF
Spring rest-doc-2015-11
PDF
연구자 및 교육자를 위한 계산 및 분석 플랫폼 설계 - PyCon KR 2015
PPTX
TensorFlow
PDF
Introduction to TensorFlow
PDF
Kaz Sato, Evangelist, Google at MLconf ATL 2016
PDF
[코세나, kosena] 빅데이터 구축 및 제안 가이드
PDF
Swift server-side-let swift2016
PDF
Advanced Spark and TensorFlow Meetup May 26, 2016
PPTX
Tensorflow internal
PDF
TensorFlow
PDF
Google TensorFlow Tutorial
Py conkr 20150829_docker-python
Présentation Ikoula au Meet-up Docker à l'école 42
Py conkr 20150829_docker-python
Http capturing
High perforance-browse-networking-2015-bwahn
Cdn gslb-20151209
Introduction to Tensorflow
Keep it simple web development stack
Docker en production et la sécurité … _
Spring rest-doc-2015-11
연구자 및 교육자를 위한 계산 및 분석 플랫폼 설계 - PyCon KR 2015
TensorFlow
Introduction to TensorFlow
Kaz Sato, Evangelist, Google at MLconf ATL 2016
[코세나, kosena] 빅데이터 구축 및 제안 가이드
Swift server-side-let swift2016
Advanced Spark and TensorFlow Meetup May 26, 2016
Tensorflow internal
TensorFlow
Google TensorFlow Tutorial
Ad

Similar to Docker remote-api (20)

PDF
Docker Registry + Basic Auth
PDF
Easy Cloud Native Transformation with Nomad
PDF
Docker, the Future of DevOps
PDF
파이썬 개발환경 구성하기의 끝판왕 - Docker Compose
PDF
AWS와 Docker Swarm을 이용한 쉽고 빠른 컨테이너 오케스트레이션 - AWS Summit Seoul 2017
PPT
Presentation iv implementasi 802x eap tls peap mscha pv2
PDF
Travis and fastlane
PPTX
Get started with docker &amp; dev ops
PDF
Things I've learned working with Docker Support
PDF
Delivering Go.CD with Terraform and Docker
PDF
PHP Backdoor: The rise of the vuln
KEY
Railsconf2011 deployment tips_for_slideshare
DOCX
Vagrant file samples for various Hadoop distributions
PPTX
Get started with docker &amp; dev ops
PDF
smartdc by Ruby
PDF
Lights, Camera, Docker: Streaming Video at DramaFever
PDF
Into The Box 2018 Going live with commandbox and docker
PDF
Going live with BommandBox and docker Into The Box 2018
PDF
PDF
Da APK al Golden Ticket
Docker Registry + Basic Auth
Easy Cloud Native Transformation with Nomad
Docker, the Future of DevOps
파이썬 개발환경 구성하기의 끝판왕 - Docker Compose
AWS와 Docker Swarm을 이용한 쉽고 빠른 컨테이너 오케스트레이션 - AWS Summit Seoul 2017
Presentation iv implementasi 802x eap tls peap mscha pv2
Travis and fastlane
Get started with docker &amp; dev ops
Things I've learned working with Docker Support
Delivering Go.CD with Terraform and Docker
PHP Backdoor: The rise of the vuln
Railsconf2011 deployment tips_for_slideshare
Vagrant file samples for various Hadoop distributions
Get started with docker &amp; dev ops
smartdc by Ruby
Lights, Camera, Docker: Streaming Video at DramaFever
Into The Box 2018 Going live with commandbox and docker
Going live with BommandBox and docker Into The Box 2018
Da APK al Golden Ticket

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Spectroscopy.pptx food analysis technology
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Approach and Philosophy of On baking technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Understanding_Digital_Forensics_Presentation.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Programs and apps: productivity, graphics, security and other tools
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Spectroscopy.pptx food analysis technology
MIND Revenue Release Quarter 2 2025 Press Release
Building Integrated photovoltaic BIPV_UPV.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectral efficient network and resource selection model in 5G networks
Review of recent advances in non-invasive hemoglobin estimation
Network Security Unit 5.pdf for BCA BBA.
NewMind AI Weekly Chronicles - August'25 Week I
Approach and Philosophy of On baking technology
Diabetes mellitus diagnosis method based random forest with bat algorithm

Docker remote-api

  • 1. Docker Remote API & docker-py Eric Ahn 2016/07
  • 2. $ docker-machine create -d virtualbox default $ docker-machine env default $ docker-machine ls default $ curl https://192.168.99.100:2376/images/json —cert $DOCKER_CERT_PATH/cert.pem —key $DOCKER_CERT_PATH/key.pem —cacert $DOCKER_CERT_PATH/ca.pem curl: (58) SSL: Can't load the certificate "/Users/EricAhn/.docker/machine/ machines/default/cert.pem" and its private key: OSStatus -25299 password?
  • 3. $ cd ~/.docker/machine/machines/default $ openssl pkcs12 -export > -inkey key.pem > -in cert.pem > -CAfile ca.pem > -name client-side > -out cert.p12 > -password pass:mypass $ curl https://192.168.99.100:2376/images/json —cert $DOCKER_CERT_PATH/cert.p12 —pass mypass —key $DOCKER_CERT_PATH/key.pem —cacert $DOCKER_CERT_PATH/ca.pem [{"Id":"sha256:0d409d33b27e47423b049f7f863faa08655a8c901749c2b25b93ca6 7d01a470d","ParentId":"","RepoTags":["nginx:latest"],"RepoDigests":null,"Created": 1464804059,"Size":182786784,"VirtualSize":182786784,"Labels":{}}] Generate cert.p12 with password List images : “nginx:latest” Result
  • 4. $ curl -X POST https://192.168.99.100:2376/images/create?fromImage=nginx:latest —cert $DOCKER_CERT_PATH/cert.p12 —pass mypass —key $DOCKER_CERT_PATH/key.pem —cacert $DOCKER_CERT_PATH/ca.pem {"status":"Pulling from library/nginx","id":"latest"} {"status":"Digest: sha256:0fe6413f3e30fcc5920bc8fa769280975b10b1c26721de956e1428b9e2f29d04"} {"status":"Status: Image is up to date for nginx:latest"} $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 0d409d33b27e 7 weeks ago 182.8 MB Download “nginx:latest” image Result Check using command line
  • 5. $ curl -v -X POST -H "Content-Type: application/json" -d ' { "Image": "nginx" }' https://192.168.99.100:2376/containers/create --cert $DOCKER_CERT_PATH/cert.p12 --pass mypass --key $DOCKER_CERT_PATH/key.pem --cacert $DOCKER_CERT_PATH/ca.pem {"Id":"8c6e0e97330c08461cdfc179a524ad56c2b0c3a2ba95fc5909a999d 75eb44b37","Warnings":null} Create container Result a message
  • 6. $ curl -X POST -H "Content-Type: application/json" > -d '{ "PortBindings": {"80/tcp": [{ "HostPort": "80"}]} }' > https://192.168.99.100:2376/containers/8c6e0e97330c/start > --cert $DOCKER_CERT_PATH/cert.p12 > --pass mypass > --key $DOCKER_CERT_PATH/key.pem > --cacert $DOCKER_CERT_PATH/ca.pem {"message":"starting container with HostConfig was deprecated since v1.10 and removed in v1.12"} Set a specific HostConfig Result : Failed with HostConfig
  • 7. $ curl -v https://192.168.99.100:2376/containers/json?all=1 --cert $DOCKER_CERT_PATH/cert.p12 --pass mypass --key $DOCKER_CERT_PATH/key.pem --cacert $DOCKER_CERT_PATH/ca.pem [{"Id":"4ee9a6f6b24715266ae8037fd069e2439d67c0198ee8f96b531682d61363f527","Na mes":["/ hello"],"Image":"nginx:latest","ImageID":"sha256:0d409d33b27e47423b049f7f863faa08655 a8c901749c2b25b93ca67d01a470d","Command":"nginx -g 'daemon off;'","Created": 1469639718,"Ports":[{"PrivatePort":443,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort": 80,"PublicPort":80,"Type":"tcp"}],"Labels":{},"State":"running","Status":"Up 14 minutes","HostConfig":{"NetworkMode":"default"},"NetworkSettings":{"Networks":{"bridge": {"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"f22986e826e252df0d0f06011158 9f0a732993c6636ba47e549911ccaf8f8ed8","EndpointID":"41513d5005aa2777561b6c0c d3f3f6ae44fa7d6d47d261734391e206cdade7b6","Gateway":"172.17.0.1","IPAddress":"17 2.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen": 0,"MacAddress":"02:42:ac:11:00:02"}}},"Mounts":[]}] List all containers Result ( json )
  • 8. $ curl -X POST -H "Content-Type: application/json" https://192.168.99.100:2376/containers/8c6e0e97330c/start --cert $DOCKER_CERT_PATH/cert.p12 --pass mypass --key $DOCKER_CERT_PATH/key.pem --cacert $DOCKER_CERT_PATH/ca.pem $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8c6e0e97330c nginx "nginx -g 'daemon off" 2 minutes ago Up 1 seconds 80/tcp, 443/tcp goofy_ardinghelli Start a container Running status
  • 9. $ cd ~/_Python $ sudo easy_install pip $ sudo pip install virtualenv $ virtualenv foobar $ source foobar/bin/activate (foobar) $ pip install docker-py Collecting docker-py Downloading docker_py-1.9.0-py2.py3-none-any.whl (42kB) Collecting requests>=2.5.2 (from docker-py) Downloading requests-2.10.0-py2.py3-none-any.whl (506kB) 100% | | 512kB 938kB/s Collecting six>=1.4.0 (from docker-py) Downloading six-1.10.0-py2.py3-none-any.whl Collecting websocket-client>=0.32.0 (from docker-py) Downloading websocket_client-0.37.0.tar.gz (194kB) 100% | | 194kB 1.4MB/s Collecting backports.ssl-match-hostname>=3.5; python_version < "3.5" (from docker-py) Downloading backports.ssl_match_hostname-3.5.0.1.tar.gz Building wheels for collected packages: websocket-client, backports.ssl-match-hostname Running setup.py bdist_wheel for websocket-client ... done Stored in directory: /Users/EricAhn/Library/Caches/pip/wheels/e4/20/12/48d6732b88733a0a8f98ee1a5b2f59b64b27011d5c34c4f98c ….. Install python and virtualenv
  • 10. (foobar) $ vi docker-run.py import docker tls_config = docker.tls.TLSConfig( client_cert=('/Users/EricAhn/.docker/machine/machines/default/cert.pem', '/Users/EricAhn/.docker/machine/machines/default/key.pem'), verify='/Users/EricAhn/.docker/machine/machines/default/ca.pem' ) client = docker.Client(base_url='tcp://192.168.99.100:2376', tls=tls_config) client.containers() container_id = client.create_container( image='nginx:latest', ports=[80], name='hello' ) client.start( container_id, port_bindings={80: ('0.0.0.0', 80)} ) $ python docker-run.py $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4ee9a6f6b247 nginx:latest "nginx -g 'daemon off" 2 seconds ago Up 1 seconds 0.0.0.0:80->80/tcp, 443/tcp hello Python code Run code and check