繁体   English   中英

Docker、postgres、sqlalchemy - 无法连接到服务器:无法分配请求的地址

[英]Docker, postgres, sqlalchemy - could not connect to server: Cannot assign requested address

嗨,我正在尝试在 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  

应用程序/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 工作正常

我解决了这个问题。 这与我为from ubuntufrom python3两个Dockerfiles使用不同的图像这一事实有关

我将应用程序容器更改为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

希望这可以帮助处于同样困境的人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM