简体   繁体   English

如何运行连接到 nginx 的 docker 容器

[英]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.所以我有一个 DigitalOcean 服务器和一个有效的域名,我想为我的后端获得 HTTPS 支持。 I want to use Let's Encrypt and the common way to use this from what I've seen is in conjunction with Nginx.我想使用 Let's Encrypt,从我所看到的情况来看,使用它的常用方法是与 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. 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这是我的后端的 Dockerfile

FROM tiangolo/uvicorn-gunicorn:python3.8

LABEL maintainer="Sebastian Ramirez <tiangolo@gmail.com>"

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这是我的 nginx 配置

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我可以 curl 到 example.com 并获得 200 和 localhost:80

EDIT: I can not curl to my domain name now but I can curl to localhost:8000编辑:我现在不能 curl 到我的域名,但我可以 curl 到 localhost:8000

You have a few different options, but it's probably easiest to first set up uvicorn to run on another port (such as 8080 )您有几个不同的选项,但首先将 uvicorn 设置为在另一个端口上运行可能是最简单的(例如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)然后使用-p到 map(“发布”)容器的 web 端口为主机上的一个(您可以设置任何端口之间的映射,但使其相同可以简化未来的理解)

docker run ... -p 8080:8080

Then in your nginx configuration, set it up to proxy_pass to your mapped port然后在您的 nginx 配置中,将其设置为 proxy_pass 到您的映射端口

    proxy_pass http://127.0.0.1:8080;

Restart nginx after you do this to pick up the new config执行此操作后重新启动 nginx 以获取新配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM