如何构建安装了postgres的ubuntu Docker容器? [英] How to Build a ubuntu docker container with postgres installed in?

查看:36
本文介绍了如何构建安装了postgres的ubuntu Docker容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是挑战,我已经用pyqt5和其他一些库(包括postgresql)在python中编写了一个程序,现在的问题是,如何构建安装了postgresql的docker ubuntu容器?为了使一切正常,我必须将postgres用户设置为postgres,将密码设置为1234.

Here is the challenge, I've written a program in python with pyqt5 and some others libraries including postgresql, now the question is, how could build a docker ubuntu container with postgresql installed in ? And I have to set up the postgres user as postgres and password as 1234 in order to make everything working well.

我对如何编写好Dockerfile和尊重所有经验感到迷茫.

I'am lost in how to write well the Dockerfile and respecting all exigence.

预先感谢您提供解决方案,如果尚不清楚,请问我一个问题,我将在几分钟后予以澄清.

Thanks in advance for the solution and if something wasn't clear ask me a question than I will clarify in few minutes.

推荐答案

我整理了一个示例配置.

I have put together a sample configuration.

docker-compose.yml

version: '3'

volumes:
  local_postgres_data: {}
  local_postgres_data_backups: {}

services:
  web:
    build: 
      context: .
      dockerfile: ./compose/python/Dockerfile
    ports: 
      - "8000:8000"
    depends_on:
      - postgres
    env_file:
      - ./.envs/.postgres
    command: /start


  postgres:
    build:
      context: .
      dockerfile: ./compose/postgres/Dockerfile
    image: app_production_postgres
    volumes:
      - local_postgres_data:/var/lib/postgresql/data
      - local_postgres_data_backups:/backups
    env_file:
      - ./.envs/.postgres
    ports:
      - "5432:5432"

compose/postgres/Dockerfile

FROM postgres:11.3

compose/python/Dockerfile

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt

COPY ./compose/python/start /start
RUN sed -i 's/\r$//g' /start
RUN chmod +x /start

COPY ./compose/python/entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint
RUN chmod +x /entrypoint

ENTRYPOINT ["/entrypoint"]

compose/python/entrypoint

#!/bin/sh

set -o errexit
set -o nounset


if [ -z "${POSTGRES_USER}" ]; then
    base_postgres_image_default_user='postgres'
    export POSTGRES_USER="${base_postgres_image_default_user}"
fi
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"

postgres_ready() {
python << END
import sys

import psycopg2

try:
    psycopg2.connect(
        dbname="${POSTGRES_DB}",
        user="${POSTGRES_USER}",
        password="${POSTGRES_PASSWORD}",
        host="${POSTGRES_HOST}",
        port="${POSTGRES_PORT}",
    )
except psycopg2.OperationalError:
    sys.exit(-1)
sys.exit(0)

END
}
until postgres_ready; do
  >&2 echo 'Waiting for PostgreSQL to become available...'
  sleep 1
done
>&2 echo 'PostgreSQL is available'

exec "$@"

compose/python/start

#!/bin/sh

set -o errexit
set -o nounset


python -m http.server

requirements.txt

psycopg2>=2.7,<3.0

.envs/.postgres

# PostgreSQL
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_DB=your_app
POSTGRES_USER=debug
POSTGRES_PASSWORD=debug

此配置是 django cookiecutter

这篇关于如何构建安装了postgres的ubuntu Docker容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆