Postgresql 在Docker容器中启动Postgres

Postgresql 在Docker容器中启动Postgres,postgresql,docker,dockerfile,Postgresql,Docker,Dockerfile,为了进行测试,我尝试在docker容器中设置Postgres,以便我们的python应用程序可以针对它运行它的测试套件 这是我的Dockerfile: # Set the base image to Ubuntu FROM ubuntu:16.04 # Update the default application repository sources list RUN apt-get update && apt-get install -y \ python2.7 \

为了进行测试,我尝试在docker容器中设置Postgres,以便我们的python应用程序可以针对它运行它的测试套件

这是我的Dockerfile:

# Set the base image to Ubuntu
FROM ubuntu:16.04

# Update the default application repository sources list
RUN apt-get update && apt-get install -y \
  python2.7 \
  python-pip \
  python-dev \
  build-essential \
  libpq-dev \
  libsasl2-dev \
  libffi-dev \
  postgresql

USER postgres
RUN /etc/init.d/postgresql start && \
  psql -c "CREATE USER circle WITH SUPERUSER PASSWORD 'circle';" && \
  createdb -O darwin circle_test
USER root
RUN service postgresql stop && service postgresql start

# Upgrade pip
RUN pip install --upgrade pip

COPY . /app
WORKDIR /app

RUN pip install -r requirements.txt

EXPOSE 5000

# Set the container entrypoint
ENTRYPOINT ["gunicorn", "--config", "/app/config/gunicorn.py", "--access-logfile", "-", "--error-logfile", "-", "app:app"]
当我跑步时:

docker run --entrypoint python darwin:latest -m unittest discover -v -s test
我得到:

could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?
我能让它工作的唯一方法是,如果我使用ssh连接到容器中,重新启动postgres并直接运行测试套件


这里有我遗漏的东西吗?

在您的Dockerfile中

  • 配置阶段、运行指令(以及其他一些)

  • 你开始的过程,你投入其中

CMD

ENTRYPOINT

见文件

当一个容器完成了它在这个开始阶段必须做的事情时,它就死了

这就是为什么PostgreSQL的参考Dockerfile位于

CMD[“postgres”]

如果要启动多个进程,请参阅supervisord或此类工具(s6、daemontools…)


您的
RUN service postgresql stop&&service postgresql start
正在运行中,而不是在CMD或ENTRYPOINT中,因此当容器启动时,它被“遗忘”了查看位于我将
RUN
更改为
CMD
的参考postgresql Dockerfile,但它仍然显示相同的错误。Dockerfile中只能有一个CMD,使用了最新的,请参见我答案中的链接,以及非常有意义的链接。我可以用一个启动PG和gunicorn的bash脚本来交换我的最后一个
入口点吗?是的,这是一个选项,读吧,你可以将入口点视为动词,将CMD视为参数,看你是否只是运行它,如果你添加了wlp2s0(或eth1或任何),它开始监控wlan0设备在您的
docker运行结束时…
它监视wlp2s0(或eth1或任何)