0

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.

5
  • 1
    You can spawn postgres in another container and use it as database. Coordinate everything via docker compose. Or is there any reason for not doing so? Commented Mar 18, 2020 at 10:27
  • Nope, i was expecting Dockerfile as response, but your suggestion can be used as an alternative solution but i little more complicated, if you can provide a Dockerfile as example i will appreciate that. Commented Mar 18, 2020 at 10:35
  • Containers should be kept small and focused. You should use multiple small containers instead of one large one. Commented Mar 18, 2020 at 10:43
  • Okay, but i need Dockerfile examples to link everything that's what i'am expecting. Commented Mar 18, 2020 at 10:46
  • The Docker documentation has an official Django and PostgreSQL sample that's pretty close to what you need. Commented Mar 18, 2020 at 11:14

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.