Through Hackernews I found out about the recent release of Rancher Desktop and I was curious if this would be a good alternative to Docker desktop for the develop of web applications on my local machine. I don’t really have a problem with Docker desktop, just good to try something new every now and then and it is open-source. Running some containers really gets some steam out of my Mac so hopefully it has some improvement there. Also I like the idea of using Kubernetes, bringing the dev environment way closer to the production environment.

After installation the k3s “cluster” is available through kubectl:

$ k get nodes
NAME                   STATUS   ROLES                          AGE     VERSION
lima-rancher-desktop   Ready    builder,control-plane,master   4m48s   v1.21.5+k3s2

To build images locally we’ll use kim; the build command is identical to docker build:

$ kim build -t hello-fastapi:0.1 .

When done we can find our image in the available images:

$ kim images
IMAGE               TAG                 IMAGE ID            SIZE
hello-fastapi       0.1                 6f2734b232193       363MB
moby/buildkit       v0.8.3              cf14c5e88c0eb       56.5MB
rancher/kim         v0.1.0-beta.6       fef914df5da89       13.8MB

From the Kim registry, this image is directly available to our local Kubernetes cluster. So in this case we can deploy it to local with the following deployment file:

apiVersion: v1
kind: Service
metadata:
  name: hello-fastapi
spec:
  selector:
    app: hello-fastapi
  ports:
    - protocol: "TCP"
      port: 3000
      targetPort: 80
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-fastapi
spec:
  selector:
    matchLabels:
      app: hello-fastapi
  replicas: 2
  template:
    metadata:
      labels:
        app: hello-fastapi
    spec:
      containers:
        - name: hello-fastapi
          image: hello-fastapi:latest
          imagePullPolicy: Never
          ports:
            - containerPort: 80

Simply run k apply -f local-deploy.yaml and the application is available on http://localhost:3000.

Keep reload capabilities for development

Running FastAPI with Uvicorn enables to auto-reload with the --reload flag. We can map our local project as a volume to the container to keep auto-reload functionality for local development. Simply add the following lines to your local-deploy.yaml:

...
            command: [ "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80", "--reload" ]
          volumeMounts:
            - name: app-dev
              mountPath: /app
      volumes:
        - name: app-dev
          hostPath:
            path: /Users/<USERNAME>/path-to-app

Changing the files on your machine will trigger the reload and the update is directly available.