How to Build a ubuntu docker container with postgres installed in?

无人久伴 提交于 2021-01-29 14:23:03

问题


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.

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.


回答1:


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

This configuration is cut down version of docker project generated by django cookiecutter



来源:https://stackoverflow.com/questions/60737540/how-to-build-a-ubuntu-docker-container-with-postgres-installed-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!