我知道这是一个有很多答案的问题,但我尝试的都没有解决我的问题。我正在使用这个Dockerfile:创建一个测试数据库
FROM postgres
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD postgres
ENV POSTGRES_DB training_db
COPY create_test_database.sql /docker-entrypoint-initdb.d/
docker build -t my-postgres-image .
docker run -d --name training-db my-postgres-image
docker start training-db
一旦创建并启动了它,我就可以通过dbeaver访问它,但不能从另一个项目访问,Dockerfile看起来是这样的:
FROM 613615412341.dkr.ecr.ap-southeast-2.amazonaws.com/cv-image:latest
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/Minsk
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get update && apt-get install -y
python3-dev
python3-tk
python3-pip
libglib2.0-0
libsm6
postgresql-server-dev-all
postgresql-common
openssh-client
libxext6
nano
pkg-config
rsync
&&
apt-get clean &&
apt-get autoremove &&
rm -rf /var/lib/apt/lists/*
RUN apt update && apt install -y git pkg-config libgl1-mesa-glx tcl libopencv-dev python-opencv
RUN pip3 install --upgrade setuptools
RUN pip3 install numpy
ADD requirements.txt /requirements.txt
RUN pip3 install -r /requirements.txt && rm /requirements.txt
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8
ADD . /code
WORKDIR /code
RUN echo $(ip -4 route list match 0/0 | cut -d' ' -f3)
使用docker撰写:
version: '2.3'
services:
training_pipeline:
build:
context: .
dockerfile: Dockerfile.service
runtime: nvidia
environment:
- AWS_DEFAULT_REGION=ap-southeast-2
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
network_mode: host
ports:
- "5432:5432"
entrypoint: ["python3", "main.py"]
training-db:
image: postgres
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: training_db
volumes:
- postgres:/var/lib/postgresql/data
volumes:
postgres:
然而,我不断得到:
training_pipeline_1 | args.func(args) # call the default function
training_pipeline_1 | File "/code/functions.py", line 46, in label_batch
training_pipeline_1 | pre_labeling = PreLabeling(site, connection_config, detector_config)
training_pipeline_1 | File "/code/pre_label/label.py", line 43, in __init__
training_pipeline_1 | connection_config, set_isolation_level=True, set_profiles=True
training_pipeline_1 | File "/code/helpers/data_base_connection.py", line 37, in __init__
training_pipeline_1 | database=config["database"],
training_pipeline_1 | File "/usr/local/lib/python3.6/dist-packages/psycopg2/__init__.py", line 127, in connect
training_pipeline_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
training_pipeline_1 | psycopg2.OperationalError: could not connect to server: Connection refused
training_pipeline_1 | Is the server running on host "172.17.0.1" and accepting
training_pipeline_1 | TCP/IP connections on port 5432?
这是我的数据连接器类的一个片段(跳过不必要的方法(:
import logging
from typing import Any, Dict, List, Optional
import psycopg2
class DataBaseConnection:
"""
Class to perform transactions with training database.
"""
def __init__(
self,
config: dict,
):
"""
Args:
config: configuration file to connect to the database where annotations are stored.
"""
self._connection = psycopg2.connect(
user=config["user"],
password=config["password"],
host=config["host"],
port=config["port"],
database=config["database"],
)
self._cursor = None
self._next = None
if __name__=="__main__":
config = {
"database": "training_db",
"host": "172.17.0.1",
"password": "postgres",
"port": 5432,
"user": "postgres"
}
data_connector: DataBaseConnection = DataBaseConnection(
config
)
如我所见,您有两个容器
- 一个用于数据库(训练数据库(
- 一个用于应用程序(培训管道(
Docker compose允许您在同一网络中运行多个容器,它为您带来了神奇的效果。最简单的方法是更新docker组件以包含数据库:
版本:"2.3"服务:training_pipeline:内部版本:上下文:。dockerfile:dockerfile.service运行时:nvidia环境:-AWS_DEFAULT_REGION=ap-东南-2-AWS_ACCESS_KEY_ID=${AWS_ACCESS_6ey_ID}-AWS_SECRET_ACCESS_KEY=${AWS_SECRET _ACCESS_KEY}network_mode:主机端口:-";5432:5432";入口点:["python3","main.py"]培训数据库:图像:postgres环境:POSTGRES_PASSWORD:我的密码POSTGRES_USER:myuserPOSTGRES_DB:mydb卷:-postgres:/var/lib/postgresql/data卷:postgres:
您可以使用:docker-compose up -d training-db
启动数据库。而且,在python中,该数据库将通过DNStraining-db
可用。
另一种解决方案是手动创建一个网络,并在网络中运行您的容器:
docker network create training-network
docker run -d --name training-db --net training-network my-postgres-image
并且,在docker compose文件中,将网络添加为外部(别名,不由compose管理(:
version: '2.4'
services:
training_pipeline:
[...]
networks:
- training-network
networks:
training-network:
external: true