Can't use terraform inside container environment

I’m working in a backend service written in python which has to use terraform using the subprocess (of asyncio).
I don’t know how many hours I’ve poured into this trying to figure it out but to no avail.
Current status:
I’ve setup terraform in the dockerfile like this:

FROM python:3.12.3-slim

WORKDIR /app

COPY requirements.txt /app

RUN pip install --no-cache-dir -r requirements.txt

RUN apt-get update && \
    apt-get install -y curl gnupg software-properties-common unzip build-essential && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

RUN curl -fsSL https://releases.hashicorp.com/terraform/1.5.3/terraform_1.5.3_linux_amd64.zip -o terraform.zip && \
    unzip terraform.zip && \
    mv terraform /usr/local/bin/terraform && \
    rm terraform.zip

ENV PATH="/usr/local/bin:${PATH}"

COPY . /app

EXPOSE 8080

CMD ["uvicorn", "src.pikachu.main:app", "--host", "0.0.0.0", "--port", "8080", "--workers", "4", "--reload"]

And my docker-compose spins up three images: the main application build using this dockerfile, redis, PostgreSQL

The confusing thing is that terraform is accessible through the shell of the container, but not in the running application.

This is how the running application tries to do it:

process = await asyncio.create_subprocess_exec(
        *command,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
        env=kwargs,
        cwd=WORKING_DIR
    )
  • command being something like “terraform init”