简体   繁体   English

在Docker容器中启动Postgres

[英]Starting Postgres in Docker Container

For testing, I'm trying to setup Postgres inside of a docker container so that our python app can run it's test suite against it. 为了测试,我试图在docker容器中设置Postgres,以便我们的python应用程序可以运行它的测试套件。

Here's my Dockerfile: 这是我的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"]

When I run: 当我跑:

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

I'm getting: 我越来越:

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?

The only way I can get it to work is if I ssh into the container, restart postgres and run the test suite directly. 我可以让它工作的唯一方法是,如果我进入容器,重新启动postgres并直接运行测试套件。

Is there something I'm missing here? 这里有什么我想念的吗?

In a Dockerfile you have 在Dockerfile中你有

  • a configuration phase, the RUN directive (and some others) 配置阶段,RUN指令(和其他一些)

  • the process(es) you start, that you put in either 你开始的过程,你输入的过程

CMD

or 要么

ENTRYPOINT

see the docs 看文档

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

and

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

when a container has completed what it has to do in this start phase, it dies. 当一个容器完成了它在这个开始阶段必须做的事情时,它就会死掉。

This is why the reference Dockerfile for PostgreSQL, at 这就是PostgreSQL的引用Dockerfile的原因

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

ends with 以。。结束

CMD ["postgres"]

if you want to start several processes, see supervisord or such tool (s6, daemontools...) 如果你想启动几个进程,请参阅supervisord或此类工具(s6,daemontools ......)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM