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

时间:2018-02-06 06:58:24

标签: python postgresql docker docker-compose

您好我正在尝试在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

我的db 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  

在这种情况下,app容器无法启动,我收到一条消息container is unhealthy。但是,如果我注释掉应用容器并仅使用docker-compose up容器

运行db,则pg_isready正常工作正常

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。这与我为我的两个Dockerfiles使用不同的图像这一事实有关 from ubuntufrom 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

希望这有助于处于同样困境的人。