Core API Functionality

Containers

  • Creating a container:
from docker import DockerClient
          
          client = DockerClient(base_url='unix://var/run/docker.sock')
          image = 'ubuntu:latest'
          container = client.containers.create(image, detach=True)
          print(container.id)
          

Source: https://docker-py.readthedocs.io/en/stable/containers.html#creating-a-container

  • Starting a container:
container.start()
          

Source: https://docker-py.readthedocs.io/en/stable/containers.html#starting-a-container

  • Stopping a container:
container.stop()
          

Source: https://docker-py.readthedocs.io/en/stable/containers.html#stopping-a-container

  • Removing a container:
container.remove()
          

Source: https://docker-py.readthedocs.io/en/stable/containers.html#removing-a-container

  • Inspecting a container:
container_info = container.attrs
          print(container_info)
          

Source: https://docker-py.readthedocs.io/en/stable/containers.html#inspecting-a-container

  • Managing a container:
# Execute a command inside a container
          container.exec_run("ls -l /")
          
          # Get logs from a container
          for line in container.logs(stream=True):
              print(line.decode())
          
          # Get the container's stats
          stats = container.stats(stream=True)
          

Source: https://docker-py.readthedocs.io/en/stable/containers.html#managing-a-container

Images

  • Pulling an image:
client.images.pull('ubuntu:latest')
          

Source: https://docker-py.readthedocs.io/en/stable/images.html#pulling-an-image

  • Pushing an image:
client.images.push('my_image:latest')
          

Source: https://docker-py.readthedocs.io/en/stable/images.html#pushing-an-image

  • Building an image:
client.images.build(path='.', tag='my_image:latest')
          

Source: https://docker-py.readthedocs.io/en/stable/images.html#building-an-image

  • Listing images:
images = client.images.list()
          for image in images:
              print(image.tags)
          

Source: https://docker-py.readthedocs.io/en/stable/images.html#listing-images

  • Tagging an image:
client.images.tag('my_image:latest', 'my_registry/my_image:latest')
          

Source: https://docker-py.readthedocs.io/en/stable/images.html#tagging-an-image

Networks

  • Creating a network:
network = client.networks.create('my_network')
          print(network.id)
          

Source: https://docker-py.readthedocs.io/en/stable/networks.html#creating-a-network

  • Connecting to a network:
container.connect(network)
          

Source: https://docker-py.readthedocs.io/en/stable/networks.html#connecting-to-a-network

  • Disconnecting from a network:
container.disconnect(network)
          

Source: https://docker-py.readthedocs.io/en/stable/networks.html#disconnecting-from-a-network

  • Removing a network:
network.remove()
          

Source: https://docker-py.readthedocs.io/en/stable/networks.html#removing-a-network

Volumes

  • Creating a volume:
volume = client.volumes.create(name='my_volume')
          print(volume.id)
          

Source: https://docker-py.readthedocs.io/en/stable/volumes.html#creating-a-volume

  • Mounting a volume:
client.containers.create(image='ubuntu:latest', volumes={'my_volume': {'bind': '/data'}})
          

Source: https://docker-py.readthedocs.io/en/stable/volumes.html#mounting-a-volume

  • Inspecting a volume:
volume_info = volume.attrs
          print(volume_info)
          

Source: https://docker-py.readthedocs.io/en/stable/volumes.html#inspecting-a-volume

  • Removing a volume:
volume.remove()
          

Source: https://docker-py.readthedocs.io/en/stable/volumes.html#removing-a-volume

Services

  • Deploying a service:
client.services.create(image='nginx:latest', name='my_service')
          

Source: https://docker-py.readthedocs.io/en/stable/services.html#deploying-a-service

  • Scaling a service:
service = client.services.get('my_service')
          service.scale(replicas=3)
          

Source: https://docker-py.readthedocs.io/en/stable/services.html#scaling-a-service

  • Updating a service:
service.update(image='nginx:latest')
          

Source: https://docker-py.readthedocs.io/en/stable/services.html#updating-a-service

  • Removing a service:
service.remove()
          

Source: https://docker-py.readthedocs.io/en/stable/services.html#removing-a-service

Swarm

  • Managing a Docker Swarm cluster:
client.swarm.init(advertise_addr='192.168.99.100')
          
          # Join a swarm cluster
          client.swarm.join(remote_addrs=['192.168.99.101:2377'], token='your_token')
          
          # Get the swarm's information
          swarm_info = client.swarm.attrs
          
          # Leave a swarm cluster
          client.swarm.leave(force=True)
          

Source: https://docker-py.readthedocs.io/en/stable/swarm.html#managing-a-docker-swarm-cluster

Plugins

  • Working with Docker plugins:
# Get all plugins
          plugins = client.plugins.list()
          
          # Install a plugin
          client.plugins.install('my_plugin:latest')
          
          # Remove a plugin
          client.plugins.remove('my_plugin')
          

Source: https://docker-py.readthedocs.io/en/stable/plugins.html#working-with-docker-plugins