Try using docker-py

!! Please note that the behavior is completely different depending on the version!

Normally docker is listening as a daemon, listening to unix domain sockets. Actually, the protocol that flows to this socket is REST / HTTP, and you can bind it to tcp with the -H option, and it becomes a web server. The specification is written in "Docker Remote API". And there are also various language clients.

The story of such a python client docker-py.

First of all, normal usage.

import docker

dc = docker.Client()
# docker ps
dc.containers()
# docker images
dc.images()
# docker run -ti ubuntu bash
c = dc.create_container("ubuntu", stdin_open=True, tty=True)
dc.start(c["Id"])
print "docker attach %s" % c["Id"]

However.

Be careful as there are some serious pitfalls, just like "Oh ... I'll tell you what happened right now."

The assembly part of ExposedPorts in / container / create. Is it a list or a tuple? Some people think that the list is correct and are required to tuple. Moreover, if it is not the correct combination, the correct value will not be obtained. Besides, the trap that no error occurs when using it normally.

import traceback
import docker

dc = docker.Client()

tests = (
	[22,], # OK
	(22,), # NG
	["22/tcp",], # NG
	[(22, "tcp"),], # OK
	[[22, "tcp"],] # NG
	)

for ports in tests:
	c = dc.create_container("ubuntu", stdin_open=True, tty=True, command="bash", ports=ports)
	dc.start(c["Id"])
	info = dc.inspect_container(c["Id"])
	print ports, info["Config"]["ExposedPorts"]
	try:
		assert {'22/tcp': {}} == info["Config"]["ExposedPorts"]
	except:
		traceback.print_exc()
	finally:
		dc.stop(c["Id"])

Next is PortBindings in / containers / (id) / start. Yeah ... I wonder. What is this inconsistency?

import traceback
import docker

dc = docker.Client()

tests = (
	# automatic allocation
	{22:None}, # OK
	{"22/tcp": None}, # OK
	# manual allocation
	{22:10080}, # OK
	{"22/tcp": 10080}, # OK
	{22: ":10080"}, # NG
	{22: dict(HostPort=10080, HostIp="0.0.0.0")}, # NG
	{22: ["0.0.0.0", 10080]}, # NG
	{22: ("0.0.0.0", 10080)} # OK
	)

for port_bindings in tests:
	c = dc.create_container("ubuntu", stdin_open=True, tty=True, command="bash", ports=[22,])
	dc.start(c["Id"], port_bindings=port_bindings)
	info = dc.inspect_container(c["Id"])
	print port_bindings, info["HostConfig"]["PortBindings"]
	try:
		assert '22/tcp' in info["HostConfig"]["PortBindings"]
	except:
		traceback.print_exc()
	finally:
		dc.stop(c["Id"])

Recommended Posts

Try using docker-py
Try using Tkinter
Try using cookiecutter
Try using PDFMiner
Try using geopandas
Try using Selenium
Try using scipy
Try using pandas.DataFrame
Try using django-swiftbrowser
Try using matplotlib
Try using tf.metrics
Try using PyODE
Try using virtualenv (virtualenvwrapper)
[Azure] Try using Azure Functions
Try using virtualenv now
Try using W & B
Try using Django templates.html
[Kaggle] Try using LGBM
Try using Python's feedparser.
Try using Python's Tkinter
Try using Tweepy [Python2.7]
Try using Pytorch's collate_fn
Try using PythonTex with Texpad.
[Python] Try using Tkinter's canvas
Try using Jupyter's Docker image
Try using scikit-learn (1) --K-means clustering
Try function optimization using Hyperopt
Try using matplotlib with PyCharm
Try using Azure Logic Apps
Try using Kubernetes Client -Python-
[Kaggle] Try using xg boost
Try using the Twitter API
Try using OpenCV on Windows
Try using Jupyter Notebook dynamically
Try using AWS SageMaker Studio
Try tweeting automatically using Selenium.
Try using SQLAlchemy + MySQL (Part 1)
Try using the Twitter API
Try using SQLAlchemy + MySQL (Part 2)
Try using Django's template feature
Try using the PeeringDB 2.0 API
Try using Pelican's draft feature
Try using pytest-Overview and Samples-
Try using folium with anaconda
Try using Janus gateway's Admin API
[Statistics] [R] Try using quantile regression.
Try using Spyder included in Anaconda
Try using design patterns (exporter edition)
Try using Pillow on iPython (Part 1)
Try using Pillow on iPython (Part 2)
Try using Pleasant's API (python / FastAPI)
Try using LevelDB in Python (plyvel)
Try using pynag to configure Nagios
Try using PyCharm's remote debugging feature
Try using ArUco on Raspberry Pi
Try using cheap LiDAR (Camsense X1)
[Sakura rental server] Try using flask.
Try using Pillow on iPython (Part 3)
Reinforcement learning 8 Try using Chainer UI
Try to get statistics using e-Stat
Try using Python argparse's action API