Error: '$PORT' is Not a Valid Port Number During Deployment

The issue is from the CMD you are using on your Dockerfile. If I explain it badly, CMD doesn’t run the command you’re giving in a shell, and thus, doesn’t interpret the $PORT variable.

For that, you need /bin/bash (or any other shell that match your preference) to execute the command gunicorn run:app –bind 0.0.0.0:$PORT. There are several solutions for that:

Solution 1

The one I recommend the most.

Create a new file in your repository that will be responsible for launching your application. For the sake of the demonstration, I will call it scripts/docker-cmd

The content of this file should be:

#!/bin/bash

gunicorn run:app –bind 0.0.0.0:$PORT

Be mindful that this file must be executable. Otherwise, it will fail!

Then, change the CMD operation in your Dockerfile to this:

CMD ["/app/scripts/docker-cmd"]

Solution 2

Change the CMD operation to this:

CMD ["/bin/bash", "-c", "gunicorn run:app –bind 0.0.0.0:$PORT"]

Solution 3

Easier, but it could break in the future if you’re not careful.

Replace the CMD operation by the following block:

ENTRYPOINT ["/bin/bash", "-c"]

CMD ["gunicorn run:app –bind 0.0.0.0:$PORT"]

Hope that helps,

1 Like