๐ŸŒ
Docker
docker-py.readthedocs.io
Docker SDK for Python - Read the Docs
A Python library for the Docker Engine API. It lets you do anything the docker command does, but from within Python apps โ€“ run containers, manage containers, manage Swarms, etc. For more information about the Engine API, see its documentation.
๐ŸŒ
GitHub
github.com โ€บ docker โ€บ docker-py
GitHub - docker/docker-py: A Python library for the Docker Engine API ยท GitHub
A Python library for the Docker Engine API. Contribute to docker/docker-py development by creating an account on GitHub.
Starred by 7.2K users
Forked by 1.7K users
Languages ย  Python
Discussions

dockerpy - link containers with the docker python API - Stack Overflow
I want to use the docker python API (pip install docker-py) to create a container and link it to an existing container which I created with docker-compose. ... But using the docker API I'm stuck. I think that I have to use the docker.Client().create_container() method, which takes an - undocumented - parameter links=. (I strongly think the documentation ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Hosting a Docker web app with a Python API
If your budget can stretch to ~$5/month, you could try pythonanywhere? It's obviously not 'self-hosted' though. Otherwise, host the app in a docker container or virtual machine and open up a/the port that others can use to access the app at the host's IP address. If you're at a research institute/university however, the IT/InfoSec department might have something to say about this if they've already said no to you before though. More on reddit.com
๐ŸŒ r/selfhosted
7
4
October 3, 2024
python with bloomberg API

Bloomberg has published this library and documentation, and I noticed lots of examples in the git repo.

https://www.bloomberg.com/professional/support/api-library/

This directory contains an interface for interacting with Bloomberg API services using the Python programming language.

https://github.com/msitt/blpapi-python

More on reddit.com
๐ŸŒ r/learnpython
6
4
September 10, 2018
Communicate with Docker containers via API
offer wine compare retire market cough nutty angle trees thumb This post was mass deleted and anonymized with Redact More on reddit.com
๐ŸŒ r/docker
9
5
January 22, 2023
๐ŸŒ
Docker Docs
docs.docker.com โ€บ reference โ€บ docker engine api โ€บ sdk โ€บ examples
Examples using the Docker Engine SDKs and Docker API
package main import ( "context" "io" "log" "os" "github.com/moby/moby/api/pkg/authconfig" "github.com/moby/moby/api/types/registry" "github.com/moby/moby/client" ) func main() { ctx := context.Background() apiClient, err := client.New(client.FromEnv, client.WithUserAgent("my-application/1.0.0")) if err != nil { log.Fatal(err) } defer apiClient.Close() authStr, err := authconfig.Encode(registry.AuthConfig{ Username: "username", Password: "password", }) if err != nil { log.Fatal(err) } out, err := apiClient.ImagePull(ctx, "alpine", client.ImagePullOptions{RegistryAuth: authStr}) if err != nil {
๐ŸŒ
Docker
docker-py.readthedocs.io โ€บ en โ€บ stable โ€บ api.html
Low-level API โ€” Docker SDK for Python 7.1.0 documentation
>>> import docker >>> client = ... u'Version': u'17.10.0-ce'} ... base_url (str) โ€“ URL to the Docker server. For example, unix:///var/run/docker.sock or tcp://127.0.0.1:1234....
๐ŸŒ
Docker Docs
docs.docker.com โ€บ reference โ€บ samples โ€บ python samples
Python samples | Docker Docs
Awesome Compose: A curated repository containing over 30 Docker Compose samples.
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ docker-py
docker-py ยท PyPI
A Python library for the Docker Remote API. It does everything the docker command does, but from within Python โ€“ run containers, manage them, pull/push images, etc. The latest stable version is always available on PyPi.
      ยป pip install docker-py
    
Published ย  Nov 02, 2016
Version ย  1.10.6
๐ŸŒ
Docker Docs
docs.docker.com โ€บ reference โ€บ docker engine api โ€บ sdk
SDK | Docker Docs
As an example, the docker run command can be implemented using the Docker API directly, or using the Python or Go SDK.
๐ŸŒ
Docker Docs
docs.docker.com โ€บ reference โ€บ docker engine api
Docker Engine API | Docker Docs
The Docker Go SDK allows you to ... by both the client and the Docker Engine that's in use. For the SDKs, you can also specify the API version programmatically as a parameter to the client object. See the Go constructor or the Python SDK documentation for clie...
Find elsewhere
๐ŸŒ
MetricFire
metricfire.com โ€บ blog โ€บ develop-and-deploy-a-python-api-with-kubernetes-and-docker
Comprehensive Guide to Developing and Deploying a Python API with Docker and Kubernetes (Part I) | MetricFire
May 14, 2025 - We finally exposed port 5000, since our app will use this port, and we launched the python command with our app.py as an argument. This will start the API when the container starts.
๐ŸŒ
Docker
docker-py.readthedocs.io โ€บ en โ€บ stable
Docker SDK for Python โ€” Docker SDK for Python 7.1.0 documentation
A Python library for the Docker Engine API. It lets you do anything the docker command does, but from within Python apps โ€“ run containers, manage containers, manage Swarms, etc. For more information about the Engine API, see its documentation.
๐ŸŒ
Docker
docs.docker.com โ€บ guides โ€บ python
Python | Docker Docs
This guide is a community contribution. Docker would like to thank Esteban Maya and Igor Aleksandrov for their contribution to this guide. The Python language-specific guide teaches you how to containerize a Python application using Docker.
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ docker
docker ยท PyPI
A Python library for the Docker Engine API.
      ยป pip install docker
    
Published ย  May 23, 2024
Version ย  7.1.0
๐ŸŒ
Ben Postance
bpostance.github.io โ€บ posts โ€บ docker-fask-api
How to containerize a simple Rest API using Python Flask | Ben Postance
April 19, 2021 - This post demonstrates how to setup a simple Docker container to run an API using Python Flask. The code is avaliable in this repo. These instructions are for Windows 10 OS. See these instructions for Ubunut/Linux Mint 19 installation. Create an account, download and install Docker Desktop for your operating system following the official channel. Once installed you check some basics using the following commands. See the git repo readme and official docs for more.
๐ŸŒ
DEV Community
dev.to โ€บ francescoxx โ€บ python-fullstack-rest-api-app-with-docker-1101
Python ๐Ÿ fullstack REST API app with Docker ๐Ÿณ - DEV Community
January 6, 2024 - test: just a test route create a user: create a user with a name and an email get all users: get all the users in the database get one user: get one user by id update one user: update one user by id delete one user: delete one user by id All the routes have error handling, for example if the user is not found, we will return a 404 HTTP response. ... The flask.dockerfile file is the file that will be used to containerize the Flask application. Create a file called flask.dockerfile in the backend folder and add the following content: FROM python:3.6-slim-buster WORKDIR /app COPY requirements.txt ./ RUN pip install -r requirements.txt COPY .
๐ŸŒ
Josefjebavy
blog.josefjebavy.cz โ€บ en โ€บ programming โ€บ docker-api-python
Python program to control Docker using the API
April 3, 2024 - Python library documentation for controlling docker using the API: https://docker-py.readthedocs.io/en/stable/
๐ŸŒ
GitHub
github.com โ€บ aio-libs โ€บ aiodocker
GitHub - aio-libs/aiodocker: Python Docker API client based on asyncio and aiohttp ยท GitHub
pip install -U build python -m build --sdist --wheel ... import asyncio import aiodocker async def list_things(docker): print('== Images ==') for image in (await docker.images.list()): tags = image['RepoTags'][0] if image['RepoTags'] else '' print(image['Id'], tags) print('== Containers ==') for container in (await docker.containers.list()): print(f" {container._id}") async def run_container(docker): print('== Running a hello-world container ==') container = await docker.containers.create_or_replace( config={ 'Cmd': ['/bin/ash', '-c', 'echo "hello world"'], 'Image': 'alpine:latest', }, name='testing', ) await container.start() logs = await container.log(stdout=True) print(''.join(logs)) await container.delete(force=True) async def main(): docker = aiodocker.Docker() await list_things(docker) await run_container(docker) await docker.close() if __name__ == "__main__": asyncio.run(main())
Starred by 523 users
Forked by 113 users
Languages ย  Python
Top answer
1 of 3
4

https://github.com/docker/docker-py

A Python library for the Docker Remote API. It does everything the docker command does, but from within Python โ€“ run containers, manage them, pull/push images, etc.

create_container:

Creates a container that can then be .start() ed. 
Parameters are similar to those for the docker run 
command except it doesn't support the attach options (-a).

The source code of create_container

def create_container(self, image, command=None, hostname=None, user=None,
                     detach=False, stdin_open=False, tty=False,
                     mem_limit=None, ports=None, environment=None,
                     dns=None, volumes=None, volumes_from=None,
                     network_disabled=False, name=None, entrypoint=None,
                     cpu_shares=None, working_dir=None, domainname=None,
                     memswap_limit=None, cpuset=None, host_config=None,
                     mac_address=None, labels=None, volume_driver=None,
                     stop_signal=None, networking_config=None):

But I found links at start function:

def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
          publish_all_ports=None, links=None, privileged=None,
          dns=None, dns_search=None, volumes_from=None, network_mode=None,
          restart_policy=None, cap_add=None, cap_drop=None, devices=None,
          extra_hosts=None, read_only=None, pid_mode=None, ipc_mode=None,
          security_opt=None, ulimits=None):

So I think you should:

from docker import Client
>>> cli = Client(base_url='tcp://127.0.0.1:2375')
>>> container = cli.create_container(
...     image='busybox:latest',
...     command='/bin/sleep 30')
>>> response = cli.start(container=container.get('Id'),links=[('EXISTING_CONTAINER', 'LINK_NAME')])

The working example (DO)

I am using CoreOS on DO:

  1. run docker container and mount inside the /var/run/docker.sock from host
  2. install tools
  3. run test container EXISTING_CONTAINER
  4. run python example

The set of commands:

docker run -it -v /var/run/docker.sock:/var/run/docker.sock ubuntu:12.04 bash
apt-get update;apt-get install python-pip -y;pip install docker-py
docker run -d --name EXISTING_CONTAINER busybox   sh -c "while true; do sleep 1;done"

Python example

from docker import Client
cli = Client(base_url='unix://var/run/docker.sock', version='auto')
container = cli.create_container(
image='busybox:latest',
command='/bin/sleep 30')
response = cli.start(container=container.get('Id'),links=(('EXISTING_CONTAINER', 'LINK_NAME'))

The result on host:

wp-coreos-512mb-ams2-01 ~ # docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
2f58e661579d        busybox             "sh -c 'while true; d"   23 seconds ago      Up 22 seconds                           EXISTING_CONTAINER
6f08dd3f5017        busybox:latest      "/bin/sleep 30"          9 minutes ago       Up 5 seconds                            condescending_brown
2 of 3
2

Yes, the networking documentation for docker-py is seriously lacking - the maintainers agree (https://github.com/docker/docker-py/issues/982 for a global alias example).

Note that Valeriy's answer above will create a legacy link, which might (will in my case) lead to issues if you use a non-default network such as the ones created by docker-compose.

In any case, adding parameters to Client.start is depreciated.

The new way to do this can be found in the unitttest: https://github.com/docker/docker-py/blob/master/tests/integration/network_test.py#L190-213

@requires_api_version('1.22')
def test_create_with_links(self):
    net_name, net_id = self.create_network()

    container = self.create_and_start(
        host_config=self.client.create_host_config(network_mode=net_name),
        networking_config=self.client.create_networking_config({
            net_name: self.client.create_endpoint_config(
                links=[('docker-py-test-upstream', 'bar')],
            ),
        }),
    )

    container_data = self.client.inspect_container(container)
    self.assertEqual(
        container_data['NetworkSettings']['Networks'][net_name]['Links'],
        ['docker-py-test-upstream:bar'])

    self.create_and_start(
        name='docker-py-test-upstream',
        host_config=self.client.create_host_config(network_mode=net_name),
    )

    self.execute(container, ['nslookup', 'bar'])

Valeriy's Example would then look as follows:

Python Example

from docker import Client
cli = Client(base_url='unix://var/run/docker.sock', version='auto')

# Note: 'bridge' is the default network
net_config = cli.create_networking_config(
        {'bridge': self.docker_client.create_endpoint_config(
            links=[('EXISTING_CONTAINER', 'LINK_NAME')]
        )}
    )

container = cli.create_container(
  image='busybox:latest',
  command='/bin/sleep 30',
  network_configuration=net_config 
)
response = cli.start(container=container.get('Id'))

I have not tested this specific code, but this is the way I have been able to connect a new container to an existing container, whereas the existing one had been created by compose into a network "project_default"

You might also want to check this link for more information and background.

๐ŸŒ
FastAPI
fastapi.tiangolo.com โ€บ deployment โ€บ docker
FastAPI in Containers - Docker - FastAPI
You will see the alternative automatic documentation (provided by ReDoc): If your FastAPI is a single file, for example, main.py without an ./app directory, your file structure could look like this: . โ”œโ”€โ”€ Dockerfile โ”œโ”€โ”€ main.py โ””โ”€โ”€ requirements.txt ยท Then you would just have to change the corresponding paths to copy the file inside the Dockerfile: FROM python:3.14 WORKDIR /code COPY ./requirements.txt /code/requirements.txt RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt # (1)!
๐ŸŒ
Docker Documentation
docker-docs.uclv.cu โ€บ engine โ€บ api โ€บ sdk โ€บ examples
Examples using the Docker Engine SDKs and Docker API | Docker Documentation
June 5, 2020 - $ docker run -d alpine touch /helloworld 0888269a9d584f0fa8fc96b3c0d8d57969ceea3a64acf47cd34eebb4744dbc52 $ curl --unix-socket /var/run/docker.sock\ -X POST "http:/v1.24/commit?container=0888269a9d&repo=helloworld" {"Id":"sha256:6c86a5cd4b87f2771648ce619e319f3e508394b5bfc2cdbd2d60f59d52acda6c"} developing, api, sdk, developers, rest, curl, python, go