DSGym
DSGym

Reputation: 2867

Changing port of Gunicorn Server with FastAPI in Docker context

I use gunicorn to start my Webserver in Docker like this:

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--reload", "-k", "uvicorn.workers.UvicornWorker", "app.main:app"]

I have a CLI Tool with should be able to overrule the port set in the Dockerfile:

@ runtime.command()
def run(branch: Optional[str] = "master", port: Optional[int] = 8000):

    subprocess.call(f"docker run -it -p 7000:7000 test", shell=True)

When I run this command, the Service is still running on port 8000, not on port 7000.

[2021-11-25 15:40:20 +0000] [1] [INFO] Starting gunicorn 20.1.0
[2021-11-25 15:40:20 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[2021-11-25 15:40:20 +0000] [1] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2021-11-25 15:40:20 +0000] [11] [INFO] Booting worker with pid: 11
[2021-11-25 15:40:21 +0000] [11] [INFO] Started server process [11]
[2021-11-25 15:40:21 +0000] [11] [INFO] Waiting for application startup.

Is there a way to override the port which was set in my Dockerfile?

Upvotes: 0

Views: 1560

Answers (1)

Paolo
Paolo

Reputation: 25989

If you want to change the behavior of the docker container, and you want to do so without creating a new image (which could be achieved with a docker commit workflow), then you must overwrite the container entrypoint and start it as you wish:

$ docker run --rm -it --entrypoint=gunicorn your_image_name --bind 0.0.0.0:7000 --reload -k uvicorn.workers.UvicornWorker app.main:app

substitute your_image_name with the name of your image.

Upvotes: 2

Related Questions