Last active
July 25, 2022 13:11
-
-
Save rsperl/943eaaa6ee3cb3acb1d45e0eb659c451 to your computer and use it in GitHub Desktop.
Run a docker container from python #docker #container #python #snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import docker | |
| import os | |
| import sys | |
| import base64 | |
| image = "registry.com/me/image:1.2.3" | |
| CWD = os.path.abspath(os.path.dirname(__file__)) | |
| # DOCKER_HOST=unix://var/run/docker.sock | |
| # DOCKER_TLS_VERIFY | |
| # DOCKER_CERT_PATH | |
| client = docker.from_env(assert_hostname=False) | |
| env = { | |
| "VAR1": os.getenv("VAR1"), | |
| } | |
| if uservarb64 is not None: | |
| env["USERVARB64"] = uservarb64 | |
| volumes = { | |
| DIR: {"bind": "/mydirectory", "mode": "rw"}, | |
| CWD + "/tmp": {"bind": "/uservars", "mode": "rw"}, | |
| } | |
| # run container | |
| # https://docker-py.readthedocs.io/en/stable/containers.html | |
| cmd = ["bin/app.sh"] | |
| cname = "hello" | |
| c = client.containers.run( | |
| image=image, | |
| hostname=cname, | |
| name=cname, | |
| command=cmd, | |
| detach=True, | |
| environment=env, | |
| remove=True, | |
| volumes=volumes, | |
| working_dir="/playbooks", | |
| ) | |
| print(f"Logs for {c.name} ({c.id})") | |
| print("--------------------------------------------------------------------") | |
| for line in c.logs(stream=True, stdout=True, stderr=True, follow=True): | |
| print(line.decode().strip()) | |
| print("--------------------------------------------------------------------") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment