Dockerfile fast api running port dynamically

Viewed 23

I have a Dockerfile that looks like this

# Pull base image
FROM python:3.9.2-slim-buster

RUN apt-get update \
  && apt-get -y install gcc \
  && apt-get -y install g++ \
  && apt-get -y install unixodbc unixodbc-dev \
  && apt-get clean


# Set environment varibles
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

EXPOSE 8000

WORKDIR /code/

# Install dependencies
COPY requirements.txt /code/
RUN pip3 install --requirement /code/requirements.txt


COPY ./start.sh /start.sh


COPY . /code/



CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port $PORT"]

The port seems to be coming up w/ escaping ?

When I deploy the following docker in heroku it reads following

Starting process with command `uvicorn main:app --host 0.0.0.0 --port\ \21634`

There are slashes infront of port. Why?

1 Answers

The spaces are because you have put it into one of the elements of the list given to ENTRYPOINT. If you compare it to your other entries, there's a new element for each term in the command, while you have two terms inside the one where you have $PORT - this means that it's being processed as a single parameter, and in that case Docker (or an alternative container runtime) needs to escape the spaces to build the command you're asking for.

Replace it with separate elements instead:

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "$PORT"]
Related