pyinfra-dev / pyinfra

pyinfra turns Python code into shell commands and runs them on your servers. Execute ad-hoc commands and write declarative operations. Target SSH servers, local machine and Docker containers. Fast and scales from one server to thousands.
https://pyinfra.com
MIT License
3.84k stars 374 forks source link

container operation #635

Open Gaming4LifeDE opened 3 years ago

Gaming4LifeDE commented 3 years ago

Is your feature request related to a problem? Please describe

A lot of software requires docker-compose to be installed. So the workflow would be to deploy a server, install docker/podman and (docker/podman)-compose and then deploy your application using that

Describe the solution you'd like

An operation to manage docker/podman. The CLI syntax is the same so it shouldn't be much of an issue to support both. Running containers on boot is a different story though because docker has a daemon process and podman doesn't...

Fizzadar commented 3 years ago

I'm keen to add support for these; perhaps as two separate modules, plus a server operation, ie:

fkrauthan commented 4 months ago

Are there any plans to add this to the project? Currently using ansible and was thinking of switching to pyinfra. But I heavily use the docker management commands from ansible.

xvello commented 4 months ago

Podman introduced Quadlet files in 4.4 to allow systemd to orchestrate containers declared in files. If you have a recent enough version of Podman, it is the recommended way and can be achieved with files.put and systemd operations. Simple example running nginx on port 8080:

from io import StringIO
from pyinfra.operations import apt, files, systemd

apt.packages(packages=["podman"])

spec = """
[Container]
Image=docker.io/nginx:1.25
PublishPort=8080:80
User=root
Group=root
PodmanArgs=--log-level=warn

[Service]
Restart=always
TimeoutStartSec=900

[Install]
WantedBy=multi-user.target default.target
"""

container = files.put(
    name="Add test container quadlet",
    src=StringIO(spec),
    dest="/etc/containers/systemd/nginx.container",
)

if container.changed:
    systemd.daemon_reload()
    systemd.service(service="nginx.service", running=True, restarted=True)
lingmann commented 4 months ago

Podman introduced Quadlet files in 4.4 to allow systemd to orchestrate containers declared in files

@xvello thanks, this sounds like a promising alternative for remote container management with pyinfra... I'm looking into using your approach + podlet to migrate a number of docker-compose based projects over.