Docker CentOS7.4 + PostgreSQL11.2

目录结构

root@localhost:~/docker/docker/centos7_pg11# tree 
.
├── Dockerfile
├── postgresql-11.2.tar.gz
├── postgresql.conf
├── postgresql-init
├── postgresql-setup
└── start_postgres.sh

0 directories, 6 files




Dockerfile

FROM centos:centos7.4.1708
MAINTAINER taot.jin  

ENV LANG en_US.UTF-8
ENV LANGUAGE en_US.UTF-8
ENV LC_ALL en_US.UTF-8

#RUN localedef -c -f UTF-8 -i en_US en_US.utf8  

RUN yum clean all && yum makecache && yum install -y wget tar gcc g++ rpm make

RUN mkdir /export
RUN useradd postgres -d /home/postgres

ADD postgresql-init /postgresql-init
ADD postgresql-setup /postgresql-setup
ADD start_postgres.sh /start_postgres.sh

RUN chmod +x /postgresql-init
RUN chmod +x /postgresql-setup
RUN chmod +x /start_postgres.sh

ADD ./postgresql-11.2.tar.gz /opt/

RUN yum install -y perl python
RUN yum install -y perl-ExtUtils-Embed
RUN yum install -y readline readline-devel
RUN yum install -y zlib zlib-devel
RUN yum install -y systemd systemd-devel
RUN yum install -y python python-devel
RUN yum install -y make flex flex-devel

RUN /postgresql-setup
#RUN /postgresql-init initdb

ADD ./postgresql.conf /export/pg110_data/postgresql.conf

RUN chown -R postgres.postgres /export/pg110_data
RUN echo "host    all             all             0.0.0.0/0               md5" >> /pg110_data/pg_hba.conf

EXPOSE 5432

CMD ["/start_postgres.sh"]


postgresql-init

#!/bin/sh
#
# postgresql-setup - Initialization and upgrade operations for PostgreSQL

# PGVERSION is the full package version, e.g., 9.0.2
# Note: the specfile inserts the correct value during package build
PGVERSION=11.2

# PGENGINE is the directory containing the postmaster executable
# Note: the specfile inserts the correct value during package build
PGENGINE=/opt/pg11/bin/

# PREVMAJORVERSION is the previous major version, e.g., 8.4, for upgrades
# Note: the specfile inserts the correct value during package build
PREVMAJORVERSION=10.0

# PREVPGENGINE is the directory containing the previous postmaster executable
# Note: the specfile inserts the correct value during package build
PREVPGENGINE=/opt/pg10/bin/

# Absorb configuration settings from the specified systemd service file,
# or the default "postgresql" service if not specified
SERVICE_NAME="$2"
if [ x"$SERVICE_NAME" = x ]; then
    SERVICE_NAME=postgresql
fi

# this parsing technique fails for PGDATA pathnames containing spaces,
# but there's not much I can do about it given systemctl's output format...
PGDATA="/export/pg110_data"
# PGDATA=`systemctl show -p Environment "${SERVICE_NAME}.service" |
#                 sed 's/^Environment=//' | tr ' ' '\n' |
#                 sed -n 's/^PGDATA=//p' | tail -n 1`
# if [ x"$PGDATA" = x ]; then
#     echo "failed to find PGDATA setting in ${SERVICE_NAME}.service"
#     exit 1
# fi
PGPORT="5432"
# PGPORT=`systemctl show -p Environment "${SERVICE_NAME}.service" |
#                 sed 's/^Environment=//' | tr ' ' '\n' |
#                 sed -n 's/^PGPORT=//p' | tail -n 1`
# if [ x"$PGPORT" = x ]; then
#     echo "failed to find PGPORT setting in ${SERVICE_NAME}.service"
#     exit 1
# fi

# Log file for initdb
PGLOG=/initdb.log

# Log file for pg_upgrade
PGUPLOG=/pgupgrade.log

export PGDATA
export PGPORT

# For SELinux we need to use 'runuser' not 'su'
if [ -x /sbin/runuser ]; then
    SU=runuser
else
    SU=su
fi

script_result=0

# code shared between initdb and upgrade actions
perform_initdb(){
    if [ ! -e "$PGDATA" ]; then
        mkdir "$PGDATA" || return 1
        chown postgres:postgres "$PGDATA"
        chmod go-rwx "$PGDATA"
    fi
    # Clean up SELinux tagging for PGDATA
    [ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA"

    # Create the initdb log file if needed
    if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]; then
        touch "$PGLOG" || return 1
        chown postgres:postgres "$PGLOG"
        chmod go-rwx "$PGLOG"
        [ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
    fi

    # Initialize the database
    $SU -l postgres -c "$PGENGINE/initdb --pgdata='$PGDATA' --auth='ident'" \
                    >> "$PGLOG" 2>&1 < /dev/null

    # Create directory for postmaster log files
    mkdir "$PGDATA/pg_log"
    chown postgres:postgres "$PGDATA/pg_log"
    chmod go-rwx "$PGDATA/pg_log"
    [ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA/pg_log"

    if [ -f "$PGDATA/PG_VERSION" ]; then
        return 0
    fi
    return 1
}

initdb(){
    if [ -f "$PGDATA/PG_VERSION" ]; then
        echo $"Data directory is not empty!"
        echo
        script_result=1
    else
        echo -n $"Initializing database ... "
        if perform_initdb; then
            echo $"OK"
        else
            echo $"failed, see $PGLOG"
            script_result=1
        fi
        echo
    fi
}

upgrade(){
    # must see previous version in PG_VERSION
    if [ ! -f "$PGDATA/PG_VERSION" -o \
         x`cat "$PGDATA/PG_VERSION"` != x"$PREVMAJORVERSION" ]
    then
        echo
        echo $"Cannot upgrade because the database in $PGDATA is not of"
        echo $"compatible previous version $PREVMAJORVERSION."
        echo
        exit 1
    fi
    if [ ! -x "$PGENGINE/pg_upgrade" ]; then
        echo
        echo $"Please install the postgresql-upgrade RPM."
        echo
        exit 5
    fi

    # Make sure service is stopped
    # Using service here makes it work both with systemd and other init systems
    service "$SERVICE_NAME" stop

    # Set up log file for pg_upgrade
    rm -f "$PGUPLOG"
    touch "$PGUPLOG" || exit 1
    chown postgres:postgres "$PGUPLOG"
    chmod go-rwx "$PGUPLOG"
    [ -x /sbin/restorecon ] && /sbin/restorecon "$PGUPLOG"

    # Move old DB to PGDATAOLD
    PGDATAOLD="${PGDATA}-old"
    rm -rf "$PGDATAOLD"
    mv "$PGDATA" "$PGDATAOLD" || exit 1

    # Create configuration file for upgrade process
    HBA_CONF_BACKUP="$PGDATAOLD/pg_hba.conf.postgresql-setup.`date +%s`"
    HBA_CONF_BACKUP_EXISTS=0

    if [ ! -f $HBA_CONF_BACKUP ]; then
        mv "$PGDATAOLD/pg_hba.conf" "$HBA_CONF_BACKUP"
        HBA_CONF_BACKUP_EXISTS=1

        # For fluent upgrade 'postgres' user should be able to connect
        # to any database without password.  Temporarily, no other type
        # of connection is needed.
        echo "local all postgres ident" > "$PGDATAOLD/pg_hba.conf"
    fi

    echo -n $"Upgrading database: "

    # Create empty new-format database
    if perform_initdb; then
        # Do the upgrade
        $SU -l postgres -c "$PGENGINE/pg_upgrade \
                        '--old-bindir=$PREVPGENGINE' \
                        '--new-bindir=$PGENGINE' \
                        '--old-datadir=$PGDATAOLD' \
                        '--new-datadir=$PGDATA' \
                        --link \
                        '--old-port=$PGPORT' '--new-port=$PGPORT' \
                        --user=postgres" >> "$PGUPLOG" 2>&1 < /dev/null
        if [ $? -ne 0 ]; then
            # pg_upgrade failed
            script_result=1
        fi
    else
        # initdb failed
        script_result=1
    fi

    # Move back the backed-up pg_hba.conf regardless of the script_result.
    if [ x$HBA_CONF_BACKUP_EXISTS = x1 ]; then
        mv -f "$HBA_CONF_BACKUP" "$PGDATAOLD/pg_hba.conf"
    fi

    if [ $script_result -eq 0 ]; then
        echo $"OK"
        echo
        echo $"The configuration files was replaced by default configuration."
        echo $"The previous configuration and data are stored in folder"
        echo $PGDATAOLD.
    else
        # Clean up after failure
        rm -rf "$PGDATA"
        mv "$PGDATAOLD" "$PGDATA"
        echo $"failed"
    fi
    echo
    echo $"See $PGUPLOG for details."
}

# See how we were called.
case "$1" in
    initdb)
        initdb
        ;;
    upgrade)
        upgrade
        ;;
    *)
        echo $"Usage: $0 {initdb|upgrade} [ service_name ]"
        exit 2
esac

exit $script_result

postgresql-setup

#!/bin/bash

PG_MAJOR=11
PG_VERSION=11.2
PG_SAVEDIR=/opt

yum install -y flex flex-devel perl perl-ExtUtils-Embed readline readline-devel zlib zlib-devel systemd systemd-devel python python-devel flex flex-devel bison bison-devel

#download postgresql tar
test ! -f ${PG_SAVEDIR}/postgresql-${PG_VERSION} || \
    (wget https://ftp.postgresql.org/pub/source/v${PG_VERSION}/postgresql-${PG_VERSION}.tar.gz -P ${PG_SAVEDIR} && \
        tar -zxvf ${PG_SAVEDIR}/postgresql-${PG_VERSION}.tar.gz -C ${PG_SAVEDIR}/)

#configure && initdb 
cd ${PG_SAVEDIR}/postgresql-11.2
./configure --prefix=/opt/pg11 --with-python --with-perl --with-segsize=2 --with-blocksize=32 --with-systemd --with-wal-blocksize=32 
make world && make install world
/postgresql-init initdb


start_postgres.sh

#!/bin/bash
su postgres <


创建postgresql image

sudo docker build -t postgresql .

 启动docker

root@localhost:~/postgresql-11.0.2/contrib/timescaledb/scripts/docker_postgres_snapshot# docker run -it postgresql  
waiting for server to start....[    2019-03-06 20:43:16.216 CST 11 5c7fc064.b 1  0]LOG:  listening on IPv4 address "0.0.0.0", port 5432
[    2019-03-06 20:43:16.216 CST 11 5c7fc064.b 2  0]LOG:  listening on IPv6 address "::", port 5432
[    2019-03-06 20:43:16.231 CST 11 5c7fc064.b 3  0]LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
[    2019-03-06 20:43:16.323 CST 11 5c7fc064.b 4  0]LOG:  redirecting log output to logging collector process
[    2019-03-06 20:43:16.323 CST 11 5c7fc064.b 5  0]HINT:  Future log output will appear in directory "log".
 done
server started
[root@872be44c312c /]# su postgres
[postgres@872be44c312c /]$ psql 
psql (11.2)
Type "help" for help.

postgres=# \dt+
Did not find any relations.
postgres=# 


 

你可能感兴趣的:(Docker)