在Docker容器中启动Postgres

为了testing,我试图在Docker容器中设置Postgres,以便我们的python应用程序可以运行它的testing套件。

这是我的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并直接运行testing套件。

有什么我在这里失踪?

在你有一个Dockerfile

  • configuration阶段,RUN指令(和其他一些)

  • 你开始的过程,也就是你放入的过程

CMD

要么

ENTRYPOINT

看文档

https://docs.docker.com/engine/reference/builder/#cmd

https://docs.docker.com/engine/reference/builder/#entrypoint

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

这就是为什么在PostgreSQL中引用Dockerfile的原因

https://github.com/docker-library/postgres/blob/3d4e5e9f64124b72aa80f80e2635aff0545988c6/9.6/Dockerfile

以。。结束

CMD ["postgres"]

如果你想启动几个进程,请参阅supervisord或这样的工具(s6,daemontools …)

https://docs.docker.com/engine/admin/using_supervisord/