Eine Python-Bibliothek für die Docker Engine API. Sie können damit alles tun, was der docker
-Befehl tut, außer innerhalb von Python-Apps – Container ausführen, Container verwalten, Schwärme verwalten usw.
Die neueste stabile Version ist auf PyPI verfügbar. Mit pip installieren:
pip install docker
Ältere Versionen (< 6.0) erforderten die Installation
docker[tls]
für die SSL/TLS-Unterstützung. Dies ist nicht mehr notwendig und ein No-Op, wird aber aus Gründen der Abwärtskompatibilität unterstützt.
Stellen Sie über den Standard-Socket oder die Konfiguration in Ihrer Umgebung eine Verbindung zu Docker her:
import docker
client = docker . from_env ()
Sie können Container ausführen:
> >> client . containers . run ( "ubuntu:latest" , "echo hello world" )
'hello world n '
Sie können Container im Hintergrund ausführen:
> >> client . containers . run ( "bfirsh/reticulate-splines" , detach = True )
< Container '45e6d2de7c54' >
Sie können Container verwalten:
> >> client . containers . list ()
[ < Container '45e6d2de7c54' > , < Container 'db18e4f20eaa' > , ...]
> >> container = client . containers . get ( '45e6d2de7c54' )
> >> container . attrs [ 'Config' ][ 'Image' ]
"bfirsh/reticulate-splines"
> >> container . logs ()
"Reticulating spline 1... n "
> >> container . stop ()
Sie können Protokolle streamen:
> >> for line in container . logs ( stream = True ):
... print ( line . strip ())
Reticulating spline 2. ..
Reticulating spline 3. ..
...
Sie können Bilder verwalten:
> >> client . images . pull ( 'nginx' )
< Image 'nginx' >
> >> client . images . list ()
[ < Image 'ubuntu' > , < Image 'nginx' > , ...]
Lesen Sie die vollständige Dokumentation, um alles zu erfahren, was Sie tun können.