Question

Trouble deploying CTFd in the kubernetes containers

Hello, I’m trying to get the CTFd deployed on my kubernetes cluster, and I can get it locally but I can’t get it to my external IP Address, I keep getting this error:

Traceback (most recent call last):
  File "/opt/CTFd/ping.py", line 23, in <module>
    engine = create_engine(url)
             ^^^^^^^^^^^^^^^^^^
  File "<string>", line 2, in create_engine
  File "/opt/venv/lib/python3.11/site-packages/sqlalchemy/util/deprecations.py", line 375, in warned
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/sqlalchemy/engine/create.py", line 544, in create_engine
    dbapi = dialect_cls.dbapi(**dbapi_args)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 811, in dbapi
    import psycopg2
ModuleNotFoundError: No module named 'psycopg2'

But I’m not sure how to fix it, I have a .yaml file and a docker file that is supposed to force an install of “psycopg2” but I keep running into the error.

The docker file is this:

******# Use CTFd as base image
FROM ctfd/ctfd:3.5.0

# Switch to root user to install dependencies
USER root

# Fix potential missing files and allow changes in package releases
RUN apt-get clean && \
    apt-get update --allow-releaseinfo-change && \
    apt-get install -y libpq-dev gcc

# Switch back to the original user for security
USER ctfd

# Install psycopg2 for PostgreSQL connection
RUN pip install psycopg2

# Start CTFd
CMD ["python", "serve.py"]**

The .yaml is this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ctfd
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ctfd
  template:
    metadata:
      labels:
        app: ctfd
    spec:
      containers:
      - name: ctfd
        image: nathanw898/my-ctfd:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 8000
        env:
        -
          name: DATABASE_URL
          value: "postgresql://postgres:TestPassword@ctfd-db-postgresql.default.svc.cluster.local:5432/postgres"
---
apiVersion: v1
kind: Service
metadata:
  name: ctfd-service
spec:
  type: LoadBalancer
  selector:
    app: ctfd
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8000


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
March 12, 2025

Hey there 👋

It sounds like you’re almost there! The error suggests that the package didn’t get installed properly inside the correct venv.

Have you tried using the venv from the error message? For example:

RUN /opt/venv/bin/pip install --no-cache-dir psycopg2-binary

Let me know how it goes!

- Bobby

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.