Using Kamal do deploy a python background job

With Kamal 2.0 the ability to deploy non-ruby and non-rails apps was added. I've been using Kamal to deploy a couple of Rails apps and its been great to work with. I wanted to try this out for some other non-rails projects and thought I'd share what I found since this isn't a well documented use case.

Lets say I have a python background job that prints out the current time every second. 

import time
from datetime import datetime

if __name__ == "__main__":
    while True:
        print(datetime.now())
        time.sleep(1)

To run this I use a simple Dockerfile

FROM python:3.13

ENV PYTHONUNBUFFERED=1

ADD . /app
WORKDIR /app

CMD ["python", "main.py"]

To deploy our image, we just need to run kamal init which will generate all the necessary Kamal related files. To deploy the application you need to set a few things in the deploy.yml file. Specifically you need to set proxy: false and set a different primary_role.

Example deploy.yml

# Name of your application. Used to uniquely configure containers.
service: python-job

# Name of the container image.
image: my-user/my-app

# the default role is web but we want to run a job
primary_role: job

servers:
  job:
    hosts:
      - 192.168.0.1
    # specifically need to set proxy to false here or else
    # kamal proxy will try to set up health checks
    proxy: false

# Credentials for your image host.
registry:
  # Specify the registry server, if you're not using Docker Hub
  # server: registry.digitalocean.com / ghcr.io / ...
  username: my-user

  # Always use an access token rather than real password (pulled from .kamal/secrets).
  password:
    - KAMAL_REGISTRY_PASSWORD

# Configure builder setup.
builder:
  arch: amd64

Warning: Be sure to have a .ruby-version file defined in your project or else kamal deploy will fail.

Now if you run kamal deploy you the container should be running on your server. So far I've been using this pattern for a backup script on a server. I've put all the sample code up on Github as well.

https://github.com/abronte/kamal-python-background-job