Skip to main content

A context manager for a docker container.

Project description

https://github.com/cjrh/dockerctx/workflows/Python%20application/badge.svg https://coveralls.io/repos/github/cjrh/dockerctx/badge.svg?branch=master https://img.shields.io/pypi/pyversions/dockerctx.svg https://img.shields.io/github/tag/cjrh/dockerctx.svg https://img.shields.io/pypi/v/dockerctx.svg

dockerctx

dockerctx is a context manager for managing the lifetime of a docker container.

The main use case is for setting up scaffolding for running tests, where you want something a little broader than unit tests, but less heavily integrated than, say, what you might write using Robot framework.

Install

$ pip install dockerctx

The development-specific requirements will be installed automatically.

Demo

This is taken from one of the tests:

import time
import redis
import pytest
from dockerctx import new_container

# First make a pytest fixture

@pytest.fixture(scope='function')
def f_redis():

    # This is the new thing! It's pretty clear.  The `ready_test` provides
    # a way to customize what "ready" means for each container. Here,
    # we simply pause for a bit.

    with new_container(
            image_name='redis:latest',
            ports={'6379/tcp': 56379},
            ready_test=lambda: time.sleep(0.5) or True) as container:
        yield container

# Here is the test.  Since the fixture is at the "function" level, a fully
# new Redis container will be created for each test that uses this fixture.
# After the test completes, the container will be removed.

def test_redis_a(f_redis):
    # The container object comes from the `docker` python package. Here we
    # access only the "name" attribute, but there are many others.
    print('Container %s' % f_redis.name)
    r = redis.StrictRedis(host='localhost', port=56379, db=0)
    r.set('foo', 'bar')
    assert r.get('foo') == b'bar'

Note that a brand new Redis container is created here, used within the context of the context manager (which is wrapped into a pytest fixture here), and then the container is destroyed after the context manager exits.

In the src, there is another, much more elaborate test which

  1. runs a postgres container;

  2. waits for postgres to begin accepting connections;

  3. creates a database;

  4. creates tables (using the SQLAlchemy ORM);

  5. performs database operations;

  6. tears down and removes the container afterwards.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dockerctx-2026.1.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dockerctx-2026.1.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file dockerctx-2026.1.1.tar.gz.

File metadata

  • Download URL: dockerctx-2026.1.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dockerctx-2026.1.1.tar.gz
Algorithm Hash digest
SHA256 f6622d8470b14ca49ba12475edfdc321d3eb21259fd8b5fc5c6a1f46cc2244d5
MD5 84a417e955a0444e30adde75b393c34a
BLAKE2b-256 c749e342232ba7b5146f5df43ecce623a836e0301719954fb5b5903a3801f19c

See more details on using hashes here.

Provenance

The following attestation bundles were made for dockerctx-2026.1.1.tar.gz:

Publisher: release.yml on cjrh/dockerctx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dockerctx-2026.1.1-py3-none-any.whl.

File metadata

  • Download URL: dockerctx-2026.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dockerctx-2026.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 99c66b2d448c6472f9bdc45c1babf177885478cfd941e123d6e5ef3878ce7a75
MD5 626baeaca7ceafbf67dc4150b6b94370
BLAKE2b-256 cc4f3b69c679f10d48153dd6a326e6fd1ca44c21d6ea06b60edfa629d2b202db

See more details on using hashes here.

Provenance

The following attestation bundles were made for dockerctx-2026.1.1-py3-none-any.whl:

Publisher: release.yml on cjrh/dockerctx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page