在Docker容器中启动Postgres

时间:2017-04-19 17:45:56

标签: 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 \
  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?

我可以让它工作的唯一方法是,如果我进入容器,重新启动postgres并直接运行测试套件。

我在这里找不到什么东西?

1 个答案:

答案 0 :(得分:1)

在Dockerfile中你有

  • 配置阶段,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/