be used for Docker engine API Of Python library . It allows you to perform docker Anything the command does , But it can be in Python In the application —— Run container 、 Manage containers 、 management Swarm etc. .
docker-py git Project address .
pip install docker
If you intend to pass TLS Connect to docker host ,docker[tls] Please add your requirements instead , Or use pip install :
pip install docker[tls]
import docker
client = docker.from_env()
The premise is... On the remote machine docker Turn on remote service , Configuration reference link below :
【docker】 Turn on remote docker Method of service
import docker
client = docker.APIClient(base_url='tcp://ip:2375') # This ip It means that the remote access function is configured in the above operation docker Machine ip
container = client.containers.run(
image="alpine",
command="sh -c 'echo \"hello\" > /insidecontainer/test'",
volumes=["somevolume:/insidecontainer"],
detach=True
)
Common parameters :
The other parameters , Reference resources git project api Source code :
exec_output = container.exec_run("cat /test")
container.kill()
container.remove()
The obtained file is tar package
with tempfile.NamedTemporaryFile() as destination:
strm, stat = self.client.get_archive(ctnr, '/vol1/data.txt')
for d in strm:
destination.write(d)
destination.seek(0)
self.client.put_archive(ctnr, '/vol1', test_tar)