简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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