Guillermo Bastian
Guillermo Bastian

Reputation: 83

Docker is only listening to port 80

I'm learning docker and I'm testing running containers. It works fine only when I run a container listening on port 80.

Example:

Works OK:

docker run -d --name fastapicontainer_4 -p **8090**:80 fastapitest
docker run -d --name fastapicontainer_4 -p **8050**:80 fastapitest

Don´t work OK::

docker run -d --name fastapicontainer_4 -p **8050**:**8080** fastapitest

When I change the port where the program listens in the container and put a port different than 80, the page didn't work. Someone knows if it's possible to use a different port from 80? and how can I do it? I'm using fastapi.

Thanks, Guillermo

Upvotes: 3

Views: 14874

Answers (3)

Saurabhcdt
Saurabhcdt

Reputation: 1158

You need to specify the custome port you want to use to run fastapi. e.g.

uvicorn.run(app, host="0.0.0.0", port=8050)

Now if you run mapping 8050(or ay other) port on host with 8050 on container, it should work:

docker run -d --name fastapicontainer_4 -p 8050:8080 fastapitest

Upvotes: 0

Diego Velez
Diego Velez

Reputation: 1893

What you are doing:

docker run -d --name fastapicontainer_4 -p 8050:8080 fastapitest

Explanation: What this is doing is forwarding the Host port 8050 to the container port 8080. In case your fastapi service is not listening on the port 8080, the connection will fail.

Host 8050 -> Container 8080

Correct way of doing it:

docker run -d --name fastapicontainer_4 -p 8080:80 fastapitest

Explanation: This is forwarding the host port 8080 to the container port 80

Host 8080 -> Container 80

Note: Docker doesn't validate the connection when you share a port, it just opens the gate so you can do whatever you want with that open port, so even if your service is not listening on that port, docker doesn't care.

Upvotes: 0

larsks
larsks

Reputation: 311238

The syntax of the -p argument is <host port>:<container port>. You can make the host port be anything you want, and Docker will arrange for it to redirect to the container port, but you cannot set the container port to an arbitrary value. There needs to be a service in the container listening on that port.

So if you have a web server in the container running on port 80, then the <container port> part of the -p option must always be 80, unless you change the web server configuration to listen on a different port.

Upvotes: 6

Related Questions