不能使用带有psycopg2的另一个python3容器中的postgres容器

如何解决不能使用带有psycopg2的另一个python3容器中的postgres容器

我正在尝试使用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,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,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,_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,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,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,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-compose.yml”文件中大量postgres

alim91's answer的帮助下,以及我的实现;如果有人需要,这里就是工作的地方。

version: '3.8'

services: 
    postgres_instance:
        image: postgres:12.4
        # to expose postgres to local machine and monitor it in something like pgadmin
        ports: 
            - "5432:5432"
        restart: unless-stopped
        # to persist data,if containers are stoopped and resumed.
        volumes:
            - ./postgres-data:/var/lib/postgresql/data
        environment:
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=postgres_password
            - POSTGRES_DB=postgres
        networks:
            - default

    docker_fastapi:
        # using Dockerfile in current directory
        build: .
        # port I send API calls to
        ports:
            - "3000:3000"
        restart: always
        depends_on: 
            - postgres_instance
        networks:
            - default
        # these environment variables must be specified here,to be able to use from .py file inside this container.
        environment: 
            - POSTGRES_HOST=postgres_instance
            - POSTGRES_PORT=5432
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=postgres_password
            - POSTGRES_DB=postgres
可以看到

,将服务名称从postgres更改为postgres_instance可以解决所有问题。大概是因为postgres实际上是指数据库的托管位置,但是该标记与用户名和数据库名相同。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-