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-2024.3.2.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

dockerctx-2024.3.2-py2.py3-none-any.whl (5.9 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: dockerctx-2024.3.2.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for dockerctx-2024.3.2.tar.gz
Algorithm Hash digest
SHA256 f34240ab867476a99125843524c31c7661e93212f54781fb9a814a547b46ed54
MD5 22ceda101812dbf03a1b3ff13e0cbe93
BLAKE2b-256 94be67a5158a6fb31e32b35d0ccb3f7546c47610b70981fb19214aebab58ab09

See more details on using hashes here.

File details

Details for the file dockerctx-2024.3.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for dockerctx-2024.3.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bc3d04c5135d316377515f51126bafca2648c9f4490e810e3af694211205e8a8
MD5 8b64791f1de34dfa61e5c59474649463
BLAKE2b-256 559e9d3c3c6525fe19791542efb1012c471f21f8558813e34038fd53351b385d

See more details on using hashes here.

Supported by

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