- 最新
- 投票最多
- 评论最多
【以下的回答经过翻译处理】 从 Gunicorn 或 Flask 或 nginx 没有任何有意义的日志输出来看,我认为可能存在一些入口点或启动命令配置错误。看起来,在尝试启动运行容器的计算节点 3 次之后,系统认为此次部署失败。
我尝试搭建一个环境,使用类似于您在帖子中描述的配置
这是我创建的目录和文件的列表,以及每个文件的内容:
static/index.html
static/readme.txt
hello.py
Dockerfile-flask
nginx.conf
Dockerfile-nginx
static/index.html:
<!DOCTYPE html>
<html>
<head><title>Static Content Root</title></head>
<body><p>Your static content is here.</p></body>
</html>
static/readme.txt:
Did you read me??
hello.py:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, World!"
Dockerfile-flask:
FROM python:alpine
RUN pip3 install Flask gunicorn
RUN mkdir /app
ADD hello.py /app
# nginx container will be sending traffic here via localhost:5555,
# see nginx.conf
EXPOSE 5555
WORKDIR /app
ENTRYPOINT ["gunicorn", "--bind", "localhost:5555", "--access-logfile", "-", "hello:app"]
nginx.conf:
server {
listen 80;
server_name localhost;
location / {
root /static;
index index.html;
}
location /flask/ {
# forward /flask/... requests to the gunicorn+flask container running on
# the same compute node, listening on port 5555.
proxy_pass http://localhost:5555/;
}
}
Dockerfile-nginx:
FROM nginx:alpine
COPY static /static/
COPY nginx.conf /etc/nginx/conf.d/default.conf
接下来,我在本地构建了Docker镜像。标记为"gunicorn-flask-app"的是Flask应用程序镜像。标记为"nginx-ingress"的是Nginx镜像,其中包含一些静态内容,并配置为将一部分流量发送到gunicorn/flask容器:
docker build . -t gunicorn-flask-app --platform=linux/amd64 -f Dockerfile-flask
docker build . -t nginx-ingress --platform=linux/amd64 -f Dockerfile-nginx
现在让我们创建容器服务:
aws lightsail create-container-service --service-name example --power nano --scale 1
它需要一些时间才能部署完成。我使用命令 aws lightsail get-container-services --service-name example 来查看它是否已经完成,或在Lightsail控制台上查看。
让我们将本地构建的镜像推送到Lightsail。
推送Flask镜像:
aws lightsail push-container-image --service-name example --label flask --image gunicorn-flask-app
输出(请注意,镜像引用中的序列号48对您而言可能完全不同):
...
Digest: sha256:d46b4439329e8de4c608cf4f61865beea9b988fe732309c8a1ff0a83cd16b82c
Image "gunicorn-flask-app" registered.
Refer to this image as ":example.flask.48" in deployments.
推送 nginx 镜像:
aws lightsail push-container-image --service-name example --label nginx --image nginx-ingress
输出:
...
Digest: sha256:bd008e587fcc59cc71f4421af0b0397a7a04d56e2ac2f4d0fc6e52a1adcc6ec6
Image "nginx-ingress" registered.
Refer to this image as ":example.nginx.47" in deployments.
完成所有这些后,我们现在可以创建部署:
aws lightsail create-container-service-deployment --service-name example \
--containers '{
"app": {
"image": ":example.flask.latest"
},
"ingress": {
"image": ":example.nginx.latest",
"ports": {"80": "HTTP"}
}
}' \
--public-endpoint '{"containerName": "ingress", "containerPort": 80}'
随着部署的进行,我看到运行 gunicorn+flask 应用程序的"app"容器的日志如下:
[deployment:3] Creating your deployment
... [1] [INFO] Starting gunicorn 20.1.0
... [1] [INFO] Listening at: http://127.0.0.1:5555 (1)
... [1] [INFO] Using worker: sync
... [7] [INFO] Booting worker with pid: 7
[deployment:3] Reached a steady state
...以及运行我们的 nginx 服务器的“ingress”容器日志:
[deployment:3] Creating your deployment
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
...
/docker-entrypoint.sh: Configuration complete; ready for start up
... [notice] 1#1: using the "epoll" event method
... [notice] 1#1: nginx/1.23.3
... [notice] 1#1: built by gcc 12.2.1 20220924 (Alpine 12.2.1_git20220924-r4)
... [notice] 1#1: OS: Linux 5.10.167-147.601.amzn2.x86_64
... [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1024:4096
... [notice] 1#1: start worker processes
... [notice] 1#1: start worker process 29
... [notice] 1#1: start worker process 30
[deployment:3] Reached a steady state
当我通过公共终端节点访问我的服务 https://example.qwerty1234567.us-west-2.cs.amazonlightsail.com/时,我可以看到它按预期返回静态内容。当我访问 https://example.qwerty1234567.us-west-2.cs.amazonlightsail.com/flask/时,我可以看到来自Flask应用程序的 "Hello, World!" 消息。
注意:URL中的 "qwerty1234567.us-west-2" 部分会因个人账户和部署服务的区域而异。您可以在Lightsail控制台的容器服务详细信息中找到此URL,或通过运行命令 aws lightsail get-container-services --service-name example 来获取。
先决条件
我在安装了 Docker Desktop & Homebrew 的 Mac 上执行此操作。
为了将本地构建的镜像推送到Lightsail容器服务部署中使用,您需要安装以下:
AWS CLI: brew install awscli lightsailctl: brew install aws/tap/lightsailctl
相关内容
- AWS 官方已更新 3 年前
- AWS 官方已更新 3 年前
- AWS 官方已更新 1 年前
- AWS 官方已更新 3 年前