您好,我正在尝试在 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
我的数据库文件:
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
时,我收到了一个连接被拒绝错误
。
事件虽然我可以像这样从主机访问 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
最佳答案
我解决了这个问题。这与我为我的两个 Dockerfiles
使用不同的图像这一事实有关
来自 ubuntu
和 来自 python3
我将应用程序容器更改为使用 from ubuntu
并安装了 python,问题就消失了。
这是我修改后的 app
容器 Dockerfile
FROM ubuntu
RUN apt-get update \
&& apt-get install -y software-properties-common curl \
&& add-apt-repository ppa:jonathonf/python-3.6 \
&& apt-get remove -y software-properties-common \
&& apt autoremove -y \
&& apt-get update \
&& apt-get install -y python3.6 \
&& curl -o /tmp/get-pip.py "https://bootstrap.pypa.io/get-pip.py" \
&& python3.6 /tmp/get-pip.py \
&& apt-get remove -y curl \
&& apt autoremove -y \
&& rm -rf /var/lib/apt/lists/*
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
希望这对处于同样困境的人有所帮助。
关于python - Docker、postgres、sqlalchemy - 无法连接到服务器 : Cannot assign requested address,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48637148/