- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用docker-compose
制作多容器docker应用程序。
这是我要完成的工作:我有一个python3应用,它从API调用(带有gunicorn服务器的fastAPI
)输入一个数字列表,并将数字传递给返回的函数(实际上是ML模型)一个数字,该数字随后将作为结果发送回该API调用。那部分工作正常。当我引入了一个postgres容器来存储接收到的输入到postgres表中的输入时,问题就开始了,但我还没有添加应该从本地pgadmin4应用程序访问该postgres数据库数据的部分。
这是我到目前为止所做的:我正在使用“docker-compose.yml”文件来设置这两个容器,这里是:
version: '3.8'
services:
postgres:
image: postgres:12.4
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres_password
- POSTGRES_DATABASE=postgres
docker_fastapi:
# use the Dockerfile in the current directory.
build: .
ports:
# 3000 is what I send API calls to
- "3000:3000"
# this is postgres's port
- "5432:5432"
environment:
# these are the environment variables that I am using inside psycop2 to make connection.
- POSTGRES_HOST=postgres
- POSTGRES_PORT=5432
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres_password
- POSTGRES_DATABASE=postgres
他
psycopg2
中使用这些环境变量的方式:
import os
from psycopg2 import connect
# making database connection using environement variables.
connection = connect(host=os.environ['POSTGRES_HOST'], port=os.environ['POSTGRES_PORT'],
user=os.environ['POSTGRES_USER'], password=os.environ['POSTGRES_PASSWORD'],
database=os.environ['POSTGRES_DATABASE']
)
这是Dockerfile:
FROM tiangolo/uvicorn-gunicorn:python3.8-slim
# slim = debian-based. Not using alpine because it has poor python3 support.
LABEL maintainer="Sebastian Ramirez <tiangolo@gmail.com>"
RUN apt-get update
RUN apt-get install -y libpq-dev gcc
# copy and install from requirements.txt file
COPY requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir -r /app/requirements.txt
# remove all the dependency files to reduce the final image size
RUN apt-get autoremove -y gcc
# copying all the code files to the container's file system
COPY ./api /app/api
WORKDIR /app/api
EXPOSE 3000
ENTRYPOINT ["uvicorn"]
CMD ["api.main:app", "--host", "0.0.0.0", "--port", "3000"]
这是它为我发送的API调用生成的错误:
root@naveen-hp:/home/naveen/Videos/ML-Model-serving-with-fastapi-and-Docker# # docker-compose up
Starting ml-model-serving-with-fastapi-and-docker_docker_fastapi_1 ... done
Starting ml-model-serving-with-fastapi-and-docker_postgres_1 ... done
Attaching to ml-model-serving-with-fastapi-and-docker_postgres_1, ml-model-serving-with-fastapi-and-docker_docker_fastapi_1
postgres_1 |
postgres_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres_1 |
postgres_1 | 2020-10-22 13:17:14.080 UTC [1] LOG: starting PostgreSQL 12.4 (Debian 12.4-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres_1 | 2020-10-22 13:17:14.080 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2020-10-22 13:17:14.080 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2020-10-22 13:17:14.092 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2020-10-22 13:17:14.120 UTC [24] LOG: database system was shut down at 2020-10-22 12:48:50 UTC
postgres_1 | 2020-10-22 13:17:14.130 UTC [1] LOG: database system is ready to accept connections
docker_fastapi_1 | INFO: Started server process [1]
docker_fastapi_1 | INFO: Waiting for application startup.
docker_fastapi_1 | INFO: Application startup complete.
docker_fastapi_1 | INFO: Uvicorn running on http://0.0.0.0:3000 (Press CTRL+C to quit)
docker_fastapi_1 | INFO: 172.18.0.1:56094 - "POST /predict HTTP/1.1" 500 Internal Server Error
docker_fastapi_1 | ERROR: Exception in ASGI application
docker_fastapi_1 | Traceback (most recent call last):
docker_fastapi_1 | File "/usr/local/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 391, in run_asgi
docker_fastapi_1 | result = await app(self.scope, self.receive, self.send)
docker_fastapi_1 | File "/usr/local/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
docker_fastapi_1 | return await self.app(scope, receive, send)
docker_fastapi_1 | File "/usr/local/lib/python3.8/site-packages/fastapi/applications.py", line 179, in __call__
docker_fastapi_1 | await super().__call__(scope, receive, send)
docker_fastapi_1 | File "/usr/local/lib/python3.8/site-packages/starlette/applications.py", line 111, in __call__
docker_fastapi_1 | await self.middleware_stack(scope, receive, send)
docker_fastapi_1 | File "/usr/local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
docker_fastapi_1 | raise exc from None
docker_fastapi_1 | File "/usr/local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
docker_fastapi_1 | await self.app(scope, receive, _send)
docker_fastapi_1 | File "/usr/local/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
docker_fastapi_1 | raise exc from None
docker_fastapi_1 | File "/usr/local/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
docker_fastapi_1 | await self.app(scope, receive, sender)
docker_fastapi_1 | File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 566, in __call__
docker_fastapi_1 | await route.handle(scope, receive, send)
docker_fastapi_1 | File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 227, in handle
docker_fastapi_1 | await self.app(scope, receive, send)
docker_fastapi_1 | File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 41, in app
docker_fastapi_1 | response = await func(request)
docker_fastapi_1 | File "/usr/local/lib/python3.8/site-packages/fastapi/routing.py", line 182, in app
docker_fastapi_1 | raw_response = await run_endpoint_function(
docker_fastapi_1 | File "/usr/local/lib/python3.8/site-packages/fastapi/routing.py", line 135, in run_endpoint_function
docker_fastapi_1 | return await run_in_threadpool(dependant.call, **values)
docker_fastapi_1 | File "/usr/local/lib/python3.8/site-packages/starlette/concurrency.py", line 34, in run_in_threadpool
docker_fastapi_1 | return await loop.run_in_executor(None, func, *args)
docker_fastapi_1 | File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
docker_fastapi_1 | result = self.fn(*self.args, **self.kwargs)
docker_fastapi_1 | File "/app/api/main.py", line 83, in predict
docker_fastapi_1 | insert_into_db(X)
docker_fastapi_1 | File "/app/api/main.py", line 38, in insert_into_db
docker_fastapi_1 | cursor.execute(f"INSERT INTO public.\"API_Test\""
docker_fastapi_1 | IndexError: index 1 is out of bounds for axis 0 with size 1
这是我发送API调用的方式:
curl -X POST "http://0.0.0.0:3000/predict" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"input_data\":[[
1.354e+01, 1.436e+01, 8.746e+01, 5.663e+02, 9.779e-02, 8.129e-02,
6.664e-02, 4.781e-02, 1.885e-01, 5.766e-02, 2.699e-01, 7.886e-01,
2.058e+00, 2.356e+01, 8.462e-03, 1.460e-02, 2.387e-02, 1.315e-02,
1.980e-02, 2.300e-03, 1.511e+01, 1.926e+01, 9.970e+01, 7.112e+02,
1.440e-01, 1.773e-01, 2.390e-01, 1.288e-01, 2.977e-01, 7.259e-02]]}"
当我使用不带第二个postgres容器的AWS RDS的postgres实例的凭据来构建它并直接在
psycopg2.connect()
内部指定凭据而不使用环境变量和docker-compose并直接使用上面显示的Dockerfile进行构建时,此方法可以按预期工作;因此,我将接收到的数据插入postgres的代码大概很好。当我引入第二个容器时,问题就开始了。是什么原因导致此类错误,以及如何解决此问题?
最佳答案
您必须添加网络网络和depend_on 标志。试试这个:
version: '3.8'
services:
postgres:
image: postgres:12.4
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres_password
- POSTGRES_DB=postgres
networks:
- default
docker_fastapi:
# use the Dockerfile in the current directory.
build: .
ports:
# 3000 is what I send API calls to
- "3000:3000"
# this is postgres's port
# no need for this
# - "5432:5432"
networks:
- default
depends_on:
- postgres
# no need for this
# environment:
# these are the environment variables that I am using inside psycop2 to make connection.
# - POSTGRES_HOST=postgres
# - POSTGRES_PORT=5432
# - POSTGRES_USER=postgres
# - POSTGRES_PASSWORD=postgres_password
# - POSTGRES_DATABASE=postgres
关于docker - 不能使用带有psycopg2的另一个python3容器中的postgres容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64483669/
目前,我正在尝试配置 Django 以在项目中使用,并且在尝试运行 python manage.py syncdb 时遇到持续错误。 File "/x/x/x/x/x/x/base.py", line
sqlite 的 Python db-api 实现有一个方便的方法 executescript() 来执行多语句 SQL 脚本。它对于创建数据库非常有用。参见 sqlite driver docume
我有一个时间戳,我正在尝试使用 psycopg 将其插入到 postgres 中,但我无法解决问题,我认为这是因为我正在尝试使用 python 的字符串格式将日期放入正确的位置。我有这样的东西 val
我试图生成一些有效的 Postgresql 语句,但是 cursor.execute 方法以一种奇怪的方式解释我的参数。 这是我的代码切割: for key in data_dict.keys():
我在尝试插入数据库时遇到了问题: ur_psql.execute("""insert into smth(data, filedate, filedby)""" "
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 有关您编写的代码问题的问题必须在问题本身中描述具体问题——并包含有效代码以重现它。见
我们使用一个对象来保持与 PostgreSQL 数据库的连接并创建新的游标来处理请求。我观察到奇怪的行为:即使读取响应并关闭游标,请求仍然卡在数据库中,阻止更新表等。 当连接关闭时,它就消失了。 我了
我在更改我的 postgres 数据库中的表时遇到了一些问题。我正在使用 psycopg2 并使用 Python。我试图添加一个串行主键。它花了很长时间(大表),并且没有抛出任何错误,所以它做了一些事
当使用 psycopg 连接到 postgresql 数据库时,我拉了网线,没有出现任何错误。我如何在代码中检测到这一点以通知用户? 最佳答案 psycopg 无法检测到网络发生了什么。例如,如果您拔
我不断收到这个 error: psycopg2.ProgrammingError: column "someentry" does not exist. 当 someentry 不是列时,错误表明 s
我是 python 的初学者。我们使用这段代码来执行 SQL 命令。 cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (100,
我正在处理 Udacity 的在线项目。我正在使用他们配置的 vagrant 来运行包含数据库的服务器。不幸的是,当我试图赋予代码持久性时,服务器每次都会返回错误。我是 python 的新手,所以请原
我用psycopg2创建数据库失败,语法错误,但在前几行使用相同的语法是行得通的,为什么要问?两行代码,为什么是第二行语法错误? [代码] db_name = 'series_id' self._cu
这个问题在这里已经有了答案: ValueError: operation parameter must be str or unicode (1 个回答) 关闭 4 年前。 我最近修改了我的插入查询
如何修复 Python 中的 SQL 语句? 数据库连接有效。但是,cur.execute 返回 none,这是错误的。 我的代码 import os, pg, sys, re, psycopg2 t
我正在尝试运行这样的代码: query = "copy (select email from my_table) TO 'STDOUT' WITH (FORMAT csv, DELIMITER '|
在 OSX Mojave 上安装了 Python3 和 Postgres 11.3,运行 pip install psycopg2 并收到以下冗长的错误消息。 据我所知,满足 psycopg 的要求,
有没有办法让 psycopg 和 postgres 无需重新建立连接就可以处理错误,比如 MySQLdb?下面的注释版本适用于 MySQLdb,注释使其适用于 Psycopg2: results =
我收到了一个来自野外的 unicode 字符串,它导致我们的一些 psycopg2 语句失败。 我已将问题简化为 SSCE: import psycopg2 conn = psycopg2.conne
我正在尝试在 Windows(Windows 7、64 位)下安装 psycopg2。我正在使用 Python 2.7.2 from Python(x,y)和 PostgreSQL 9.2.1。 我的
我是一名优秀的程序员,十分优秀!