簡體   English   中英

在Docker容器中啟動Postgres

[英]Starting Postgres in Docker Container

為了測試,我試圖在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並直接運行測試套件。

這里有什么我想念的嗎?

在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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM