我对flask,postgres和docker非常陌生。我正在尝试对我们的应用程序进行docker化,以便新开发人员不必担心安装python依赖关系就可以开始开发。我目前有两个容器,一个用于flask应用程序,一个用于postgres db。 我遇到的问题是,当我将两者组合在一起时,它们无法彼此连接。我已经定义了自己的网络,以确保它们在同一网络上运行,但这似乎仍无法解决问题。当我在容器外部运行flask应用程序并尝试连接到容器化的postgres db时,我没有任何问题,连接正常。
任何帮助将不胜感激 预先感谢
flask应用程序的Dockerfile
FROM python:3.7-alpine3.7
LABEL maintainer="uwblueprint"
LABEL org.label-schema.schema-version="1.0"
LABEL org.label-schema.name="elevate-api"
LABEL org.label-schema.vcs-url="https://gitlab.com/uwblueprint/elevate-api/"
LABEL org.label-schema.vendor="UW Blueprint"
## Copy source and environment-config files.
COPY app/ ./app/
WORKDIR /app
COPY Pipfile Pipfile.lock ./
## Install external dependencies.
RUN apk add --no-cache libpq
## Install application dependencies.
RUN apk add --no-cache --virtual build-deps \
gcc musl-dev postgresql-dev libffi-dev && \
pip3 install --upgrade pip pipenv gunicorn && \
pipenv --python 3.7 && \
pipenv install --system --deploy && \
apk del build-deps
EXPOSE 5000
CMD ["flask", "run"]
Docker Compose File
version: '3.7'
services:
database:
image: registry.gitlab.com/uwblueprint/elevate-api/postgres:latest
build:
context: ./external/postgres
cache_from:
- registry.gitlab.com/uwblueprint/elevate-api/postgres:latest
- postgres:10.5-alpine
container_name: database
env_file: ./external/postgres/configs/.env
volumes:
- postgres.data:/var/lib/postgresql/data # persist data
ports:
- "5432:5432"
networks:
- "api_net"
api:
build: .
ports:
- "5000:5000"
networks:
- "api_net"
networks:
api_net:
volumes:
postgres.data:
Python SQL炼金术代码
import os
from flask_sqlalchemy import SQLAlchemy
from app import app
# Make sure that DB_PASS is an environment variable:
##if 'DB_PASS' not in os.environ:
## raise EnvironmentError("Could not find environment variable 'DB_PASS',")
##user = os.environ.get("DB_USER", "robot")
user ="fakeUser"
password = "fakePassword"
##password = os.environ["DB_PASS"]
host = os.environ.get("DB_HOST", "0.0.0.0")
port = os.environ.get("DB_PORT", "5432")
name = os.environ.get("DB_NAME", "elevate")
# Configure flask-sqlalchemy.
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://%s:%s@%s:%s/%s" % (
user, password, host, port, name
)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
# TODO: Find the optimal pool recycle time for PostgreSQL.
app.config["SQLALCHEMY_POOL_RECYCLE"] = 7200 # in milliseconds
# The db object is aware of the Flask application lifecycle, and will do things
# like close the database session when a Flask request ends.
#
# This saves a lot of headaches compared to the plain SQLAlchemy library, where
# I kept running into issues about database-session related objects trying
# to access the session when I had closed it prematurely (due to SQLAlchemy's
# lazy-loading data acess methods).
db = SQLAlchemy(app)
答案 0 :(得分:0)
将DB_HOST设置为“数据库”,这是容器的名称。
答案 1 :(得分:0)
在docker-compose内部,您可以按服务名称在服务之间建立连接。您可以在documentation中阅读有关内容。
默认情况下,Compose为您的应用设置单个网络。服务的每个容器都加入默认网络,并且都可以被该网络上的其他容器访问,并且可以在与容器名称相同的主机名下被发现。
将host
或DB_HOST
的环境设置为database
。 127.0.0.1
无法正常工作。