{"id":16907961,"url":"https://github.com/cjrh/dockerctx","last_synced_at":"2025-06-10T14:10:33.519Z","repository":{"id":19101313,"uuid":"83931479","full_name":"cjrh/dockerctx","owner":"cjrh","description":"Context manager for managing the lifetime of a docker container","archived":false,"fork":false,"pushed_at":"2024-12-02T20:06:12.000Z","size":80,"stargazers_count":9,"open_issues_count":1,"forks_count":7,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-06-01T04:06:41.748Z","etag":null,"topics":["docker","python","unit-testing"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cjrh.png","metadata":{"files":{"readme":"README-pypi.rst","changelog":"CHANGES","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["cjrh","wallies"]}},"created_at":"2017-03-04T23:23:48.000Z","updated_at":"2025-05-19T00:37:35.000Z","dependencies_parsed_at":"2024-03-27T23:30:54.059Z","dependency_job_id":"2803cf42-3847-4d75-9c38-256f25ad8956","html_url":"https://github.com/cjrh/dockerctx","commit_stats":{"total_commits":62,"total_committers":6,"mean_commits":"10.333333333333334","dds":"0.30645161290322576","last_synced_commit":"b5a01894c43e8be55f4c9c901089bb8978fdb302"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cjrh%2Fdockerctx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cjrh%2Fdockerctx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cjrh%2Fdockerctx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cjrh%2Fdockerctx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cjrh","download_url":"https://codeload.github.com/cjrh/dockerctx/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cjrh%2Fdockerctx/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259089057,"owners_count":22803671,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","python","unit-testing"],"created_at":"2024-10-13T18:49:31.088Z","updated_at":"2025-06-10T14:10:33.495Z","avatar_url":"https://github.com/cjrh.png","language":"Python","funding_links":["https://github.com/sponsors/cjrh","https://github.com/sponsors/wallies"],"categories":[],"sub_categories":[],"readme":"dockerctx\n=========\n\n`dockerctx` is a context manager for managing the lifetime of a docker container.\n\nThe main use case is for setting up scaffolding for running tests, where you want\nsomething a little broader than *unit tests*, but less heavily integrated than,\nsay, what you might write using `Robot framework`_.\n\n.. _Robot framework: http://robotframework.org/\n\nInstall\n-------\n\n.. code-block:: bash\n\n    $ pip install dockerctx\n\nFor dev, you have to use flit_:\n\n.. code-block:: bash\n\n    $ pip install flit\n    $ flit install\n\nThe development-specific requirements will be installed automatically.\n\n.. _flit: https://flit.readthedocs.io/en/latest/\n\nDemo\n----\n\nThis is taken from one of the tests:\n\n.. code-block:: python\n\n    import time\n    import redis\n    import pytest\n    from dockerctx import new_container\n\n    # First make a pytest fixture\n\n    @pytest.fixture(scope='function')\n    def f_redis():\n\n        # This is the new thing! It's pretty clear.  The `ready_test` provides\n        # a way to customize what \"ready\" means for each container. Here,\n        # we simply pause for a bit.\n\n        with new_container(\n                image_name='redis:latest',\n                ports={'6379/tcp': 56379},\n                ready_test=lambda: time.sleep(0.5) or True) as container:\n            yield container\n\n    # Here is the test.  Since the fixture is at the \"function\" level, a fully\n    # new Redis container will be created for each test that uses this fixture.\n    # After the test completes, the container will be removed.\n\n    def test_redis_a(f_redis):\n        # The container object comes from the `docker` python package. Here we\n        # access only the \"name\" attribute, but there are many others.\n        print('Container %s' % f_redis.name)\n        r = redis.StrictRedis(host='localhost', port=56379, db=0)\n        r.set('foo', 'bar')\n        assert r.get('foo') == b'bar'\n\nNote that a brand new Redis container is created here, used within the\ncontext of the context manager (which is wrapped into a *pytest* fixture\nhere), and then the container is destroyed after the context manager\nexits.\n\n\nIn the src, there is another, much more elaborate test which\n\n#. runs a *postgres* container;\n#. waits for postgres to begin accepting connections;\n#. creates a database;\n#. creates tables (using the SQLAlchemy_ ORM);\n#. performs database operations;\n#. tears down and removes the container afterwards.\n\n.. _SQLAlchemy: http://www.sqlalchemy.org/\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcjrh%2Fdockerctx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcjrh%2Fdockerctx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcjrh%2Fdockerctx/lists"}