Docker SDK

Contents

Docker SDK#

There is a Python library that allows you to manage your Docker instance using Python docker-py.

Note: These examples should to be run in safe environment - it may inadvertently change the docker setup. Basically, use docker in docker image built from this docker file.

import docker
client = docker.from_env()

Intoduction#

As an introduction, consider the really basic operations of working with Docker - how they can be performed using the docker sdk:

  • Image build.

  • Images list.

  • Container run.

  • Containers list.

  • Container remove.

  • Image remove.

As an example, we will use an image based on the dockerfile defined below:

!mkdir -p /tmp/docker_sdk_files
%%writefile /tmp/docker_sdk_files/dockerfile
FROM ubuntu
CMD ["echo", "hello_new_image"]
Overwriting /tmp/docker_sdk_files/dockerfile

It’s an image that will print hello_new_image when it starts.

Build image

Using docker_client.images.build we can build the image.

client.images.build(path="/tmp/docker_sdk_files", tag="modified_hello_world")
(<Image: 'modified_hello_world:latest'>, <itertools._tee at 0x710a12d9e200>)

List images

To list all images use the docker_client.images.list method, it will return a list of all images in the docker daemon.

The following example shows the top element of the list - there should be an image just created.

client.images.list()[0]
<Image: 'modified_hello_world:latest'>

Run container

For more details check specific page about containers.

Let’s try to run the container based on the image we just created.

client.containers.run(
    'modified_hello_world', 
    name="modified_hello_world_container"
)
b'hello_new_image\n'

As expected, we got a message back from the container.

List containers

To list available containers, you actually need the same method as for images - client.containers.list. The following example prints the name of the first container in the list - it is the same as we specified when running that container.

client.containers.list(all = True)[0].name
'modified_hello_world_container'

Remove container

To remove a container, you can use the remove method of the container itself.

The following example runs the remove method on the container we created earlier, and shows the list of containers - it’s now empty.

client.containers.list(all=True)[0].remove()
client.containers.list(all=True)
[<Container: 4065dbc52270>,
 <Container: fd82f0fb24df>,
 <Container: 2d6dd3e39210>,
 <Container: 1a06c8f8fc92>,
 <Container: db44a0789e04>,
 <Container: d53d6874f3c8>,
 <Container: 989a496be053>]

Remove image

By using method images.remove you can remove image.

The following example applies it to the image created for this example, and shows the list of images - there are some images on my system now, but there are no more modified_hello_world images.

client.images.remove("modified_hello_world")
client.images.list()
[<Image: 'docker:latest'>,
 <Image: 'clickhouse/clickhouse-server:latest'>,
 <Image: 'airflow:latest'>,
 <Image: 'apache/airflow:3.0.0b4'>,
 <Image: 'ghcr.io/mlflow/mlflow:v2.21.0'>,
 <Image: 'dad_backup:latest'>,
 <Image: 'postgres:17.4'>,
 <Image: 'postgres:latest'>,
 <Image: 'alpine:latest'>,
 <Image: 'apache/airflow:2.10.5', 'apache/airflow:latest'>,
 <Image: 'python:3.12'>,
 <Image: 'ubuntu:24.04', 'ubuntu:latest'>,
 <Image: 'knowledge_dev:latest'>,
 <Image: 'knowledge_jupyter:latest'>,
 <Image: 'knowledge_docker:latest'>,
 <Image: 'clickhouse/clickhouse-server:24'>,
 <Image: 'apache/airflow:2.1.2'>,
 <Image: 'postgres:15.4'>,
 <Image: 'brandoncc/vim-be-good:stable'>]