Docker撰写不会等待Postgres启动

时间:2017-07-20 18:41:13

标签: django postgresql docker-compose

我根据python3图像制作了一个Dockerfile,这里是代码:

 FROM python:3

ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

这是我的docker-compose.yml文件,它启动一个简单的Django应用程序:

version: '2'

services:

  web:
    build: .
    env_file: composeexample/.env
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - "db"
    command: ["./wait-for-postgres.sh", "db", "--", "python", "news/"]
  db:
   image: postgresql
   volumes:
     - /var/lib/postgresql/data

我正在尝试从此页面执行./wait-for-postgres.sh脚本:https://docs.docker.com/compose/startup-order/

它有以下代码:

#!/bin/bash
# wait-for-postgres.sh

set -e

host="$1"
shift
cmd="$@"

until psql -h "$host" -U "postgres" -c '\l'; do
  >&2 echo "Postgres is unavailable - sleeping"
  sleep 1
done

>&2 echo "Postgres is up - executing command"
exec $cmd

但是,我在Docker上的Postgres图像有自己的Dockerfile,这是PostgreSQL Dockerfile:

#
# example Dockerfile for https://docs.docker.com/examples/postgresql_service/
#
FROM ubuntu

# Add the PostgreSQL PGP key to verify their Debian packages.
# It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc
RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8

# Add PostgreSQL's repository. It contains the most recent stable release
#     of PostgreSQL, ``9.3``.
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list

# Install ``python-software-properties``, ``software-properties-common`` and PostgreSQL 9.3
#  There are some warnings (in red) that show up during the build. You can hide
#  them by prefixing each apt-get statement with DEBIAN_FRONTEND=noninteractive
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

# Note: The official Debian and Ubuntu images automatically ``apt-get clean``
# after each ``apt-get``

# Run the rest of the commands as the ``postgres`` user created by the ``postgres-9.3`` package when it was ``apt-get installed``
USER postgres

# Create a PostgreSQL role named ``docker`` with ``docker`` as the password and
# then create a database `docker` owned by the ``docker`` role.
# Note: here we use ``&&\`` to run commands one after the other - the ``\``
#       allows the RUN command to span multiple lines.
RUN    /etc/init.d/postgresql start &&\
    psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
    createdb -O docker docker

# 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 5432

# Add VOLUMEs to allow backup of config, logs and databases
VOLUME  ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]

# Set the default command to run when starting the container
# 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时,第一次当我停止服务时(使用Ctrl + C)数据库正常启动并再次尝试,Web服务会说数据库服务正在启动。

然后我尝试在这一行中运行脚本:

command: ["./wait-for-postgres.sh", "db", "--", "python", "news/"]

但是postgres永远睡着了,说:

Postgres is unavailable - sleeping
./wait-for-postgres.sh: line 10: psql: command not found
你能帮我解决这个谜语吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

您的容器使用的web映像中没有安装PostgreSQL客户端,这就是您的docker运行失败apt的原因。

您可以使用FROM python:3 RUN apt-get update \ && apt-get install -y postgresql-client-9.4 \ && rm -rf /var/lib/apt/lists/* ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code ADD requirements.txt /code/ RUN pip install -r requirements.txt ADD . /code/ 安装客户端来解决问题:

$http.get("https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?earth_date=" + $scope.yyyy + "-" + $scope.mm + "-" + $scope.dd + "&camera=" + $scope.curCam + "&api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
    .then(function(response){
        console.log(response);
        $scope.roverdata = response.data
        console.log("this is the mars rover data: " + $scope.roverdata);
        console.log("these are the images: " + $scope.roverdata.photos[0].id);


        console.log("This is the length of the rover photos: " + $scope.roverdata.photos.length);

        //for loopo to push images returned 
        for (var i = 0; i <= $scope.roverdata.photos.length; i++) {
            //this isnt working below ---- figure out how to just push the img_src!
            $scope.roverphotos.push($scope.roverdata.photos[i].img_src);
        }
        console.log("These are the pushed rover photos: " + $scope.roverphotos);

        //just testing the image with the first item in the array, maybe use a loop to push all images returned into their own array and display a gallery-
        $scope.test2 = $scope.roverdata.photos[0].img_src;
    }); //end $http.get function(response)

编辑:更新PostgreSQL版本,python 3图像版本9.4。您可以随时添加正确的PPA并安装所需的版本。