Manage Docker with Python¶
Repo: yt-docker-managed-by-python
Video: to follow...
There are two main libraries for this:
- Docs: Python on Whales
- Docs: Docker Py
For Python on Whales, a very useful script is:
from python_on_whales import DockerClient
docker = DockerClient(compose_files=["../docker-compose.yml"])
docker.compose.build()
docker.compose.up(detach=True)
# docker.compose.down()
Another example is :
becomesfrom python_on_whales import docker
docker.run(
"postgres:9.6",
name="some-postgres",
envs={"POSTGRES_PASSWORD": "mysecretpassword"},
detach=True,
)
print(docker.ps())
# [python_on_whales.Container(id='f5fb939c409d', name='some-postgres')]
For Docker-Py we have:
import docker
client = docker.from_env()
image = client.images.pull("redis")
output = client.containers.run("ubuntu", "echo hello world")
output = output.decode("utf-8")
print(str(output))
containers = client.containers.list()
for container in containers:
print(container.name)
PORT = random.randint(8000, 9000)
container = client.containers.run("nginx:latest", detach=True, ports={"80/tcp": PORT})
Docker enables the use of ARG and --build-arg to add arguments prior to FROM, FROM usually being the first allowed command
and this means we can use this and other shell variables to script a lot of processes and create our own testing matrix.For the Docker Py library we have:
import docker
client = docker.from_env()
image = client.images.pull("redis")
output = client.containers.run("ubuntu", "echo hello world")
output = output.decode("utf-8")
print(str(output))
containers = client.containers.list()
for container in containers:
print(container.name)
PORT = random.randint(8000, 9000)
container = client.containers.run("nginx:latest", detach=True, ports={"80/tcp": PORT})
Sources of YT video on Python and YAML
-
MathByte - shorter and more concise video with its repo installed in this project in
yaml
folder. -
DevOpsMadeEasy which has a YT video (1hr 45m) here.
The repo and video explore managing Docker programatically with Python, mostly through the use of editing docker compose files and then running them programatically.