A.K.M. Adib
A.K.M. Adib

Reputation: 577

How to run a docker container that's connected to nginx

So I have a DigitalOcean server and a valid domain name and I want to get HTTPS support for my backend. I want to use Let's Encrypt and the common way to use this from what I've seen is in conjunction with Nginx. I'm confused as to how I'm supposed to run it though. If I run my docker container than port 80 is already in use so that means the nginx service cannot be started, but if I start the nginx, I can't curl my backend.

This is my Dockerfile for my backend

FROM tiangolo/uvicorn-gunicorn:python3.8

LABEL maintainer="Sebastian Ramirez <[email protected]>"

WORKDIR /app/backend

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
EXPOSE 8000
COPY . .
CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8000"]

This is my nginx config

server{
        listen 80;
       server_name example.xyz www.example.xyz;
       location / {
           include proxy_params;
           proxy_pass http://127.0.0.1:8000;
       }
}


I can curl to example.com and get a 200 and localhost:80

EDIT: I can not curl to my domain name now but I can curl to localhost:8000

Upvotes: 1

Views: 481

Answers (1)

ti7
ti7

Reputation: 18930

You have a few different options, but it's probably easiest to first set up uvicorn to run on another port (such as 8080)

Then use -p to map ("publish") the container's web port to one on the host's (you can set the mapping between any port(s), but making it the same can simplify future understanding)

docker run ... -p 8080:8080

Then in your nginx configuration, set it up to proxy_pass to your mapped port

    proxy_pass http://127.0.0.1:8080;

Restart nginx after you do this to pick up the new config

Upvotes: 1

Related Questions