【发布时间】:2026-01-31 14:30:01
【问题描述】:
您好,我正在尝试在 docker 上设置我的项目。结构是这样的
Project
-- docker-compose.yml
-- docker
-- app
-- Dockerfile
-- my-project-0.1.tar.gz
-- db
-- Dockerfile
-- db_dump.dump
docker-compose 文件如下所示:
version: '3'
services:
db:
build: ./docker/db
ports:
- "5432:5432"
app:
build: ./docker/app
depends_on:
- db
command: getdatacommand
restart: always
app/Dockerfile 看起来像这样
FROM python:3
COPY myproject-0.1.tar.gz /tmp
WORKDIR /tmp
RUN pip install myproject-0.1.tar.gz
# SETUP DB ENVIROMENT VARIABLES
ENV DB_DRIVER='postgres'
ENV DB_HOST='db'
ENV DB_PORT='5432'
ENV DB_USERNAME='myuser'
ENV DB_PASSWORD='secretpass'
ENV DB_DBNAME='db-dev'
...
EXPOSE 5000
我参考:https://docs.docker.com/engine/examples/postgresql_service/ 创建我的 postgres Dockerfile
我的数据库 dockerfile:
FROM ubuntu
# SETUP DB ENVIROMENT VARIABLES
ENV BB_DB_PORT='5432'
ENV BB_DB_USERNAME='myuser'
ENV BB_DB_PASSWORD='secretpass'
ENV BB_DB_DBNAME='db-dev'
COPY bb-dev.dump /tmp
WORKDIR /tmp
RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3
USER postgres
# Start postgres create db and restore dump
RUN /etc/init.d/postgresql start &&\
psql --command psql --command "CREATE USER ${BB_DB_USERNAME} WITH SUPERUSER PASSWORD '${BB_DB_PASSWORD}';" &&\
createdb -O ${BB_DB_USERNAME} ${BB_DB_DBNAME} &&\
psql ${BB_DB_DBNAME} < bb-dev.dump
# Adjust PostgreSQL configuration so that remote connections to the
# database are possible.
RUN echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/9.3/main/pg_hba.conf
# And add ``listen_addresses`` to ``/etc/postgresql/9.3/main/postgresql.conf``
RUN echo "listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf
# Expose the PostgreSQL port
EXPOSE ${BB_DB_PORT}
# Set the default command to run when starting the container
CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"]
但是,当我运行docker-compose up 时,我得到一个connection refused error。
事件虽然我可以像这样从主机访问 postgres 容器:
psql -h localhost -p 5432 -d db-dev -U myuser --password
我是否遗漏了一些明显的东西?
更新
为了确保在应用程序开始读取数据库之前数据库已准备就绪,我将docker-compose 文件修改为如下所示:
version: '2.1'
services:
db:
build: ./docker/db
ports:
- "5432:5432"
healthcheck:
test: ["CMD", "pg_isready -h localhost -p 5432 -U myuser"]
interval: 30s
timeout: 10s
retries: 5
app:
build: ./docker/app
depends_on:
db:
condition: service_healthy
command: getdatacommand
restart: always
在这种情况下,应用程序容器无法启动,我收到一条消息说container is unhealthy。但是,如果我注释掉应用程序容器并仅使用 db 容器运行 docker-compose up,那么 pg_isready 工作正常
【问题讨论】:
-
db 容器可能启动缓慢,而应用程序容器希望它立即启动,这可能是导致问题的原因。如果确实如此,请参阅github.com/pydanny/cookiecutter-django/blob/master/… 了解可能的解决方法。
-
但是
depends_on值不应该在启动app容器之前自动确保db已启动吗? -
postgres 容器启动并不意味着数据库服务器已为传入连接做好准备。这只是意味着容器已启动(并且正在设置,或者正在设置)
-
我尝试了你的建议,这次在我的
docker-compose.yml中使用了healthcheck。那并没有解决它。我已经更新了我的问题
标签: python postgresql docker docker-compose