Images Postgres Alpine, optimization (#207)

This commit is contained in:
3_1_3_u 2018-02-26 17:43:54 +02:00 committed by Kyâne Pichou
parent e895c0d4dd
commit 4e242572f1
3 changed files with 59 additions and 55 deletions

View File

@ -1,24 +1,19 @@
FROM postgres:9.4
FROM postgres:9.4-alpine
ENV DEFAULT_TIMEZONE UTC
# Install some packages to use WAL
RUN apt-get update \
&& apt-get install -y \
build-essential \
RUN apk add --no-cache \
build-base \
curl \
daemontools \
libc6-compat \
libffi-dev \
libssl-dev \
lzop \
pv \
linux-headers \
python-dev \
py-cryptography \
&& curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | python \
&& pip install 'wal-e<1.0.0' \
&& apt-get remove -y \
build-essential \
python-dev \
&& apt-get autoremove -y \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin /tmp/* /var/tmp/*
&& pip --no-cache-dir install 'wal-e<1.0.0' envdir \
&& rm -rf /var/cache/apk/* /tmp/* /var/tmp/*
# Add wale script
COPY setup-wale.sh /docker-entrypoint-initdb.d/

View File

@ -1,22 +1,33 @@
#!/bin/bash
# if wal backup is not enabled, use minimal wal logging to reduce disk space
: ${WAL_LEVEL:=minimal}
: ${ARCHIVE_MODE:=off}
: ${ARCHIVE_TIMEOUT:=60}
export WAL_LEVEL
export ARCHIVE_MODE
export ARCHIVE_TIMEOUT
# PGDATA is defined in upstream postgres dockerfile
# if wal-e backup is not enabled, use minimal wal-e logging to reduce disk space
export WAL_LEVEL=${WAL_LEVEL:-minimal}
export ARCHIVE_MODE=${ARCHIVE_MODE:-off}
export ARCHIVE_TIMEOUT=${ARCHIVE_TIMEOUT:-60}
function update_conf () {
if [ -f $PGDATA/postgresql.conf ]; then
sed -i "s/wal_level =.*$/wal_level = $WAL_LEVEL/g" $PGDATA/postgresql.conf
sed -i "s/archive_mode =.*$/archive_mode = $ARCHIVE_MODE/g" $PGDATA/postgresql.conf
sed -i "s/archive_timeout =.*$/archive_timeout = $ARCHIVE_TIMEOUT/g" $PGDATA/postgresql.conf
wal=$1
# PGDATA is defined in upstream postgres dockerfile
config_file=$PGDATA/postgresql.conf
# Check if configuration file exists. If not, it probably means that database is not initialized yet
if [ ! -f $config_file ]; then
return
fi
# Reinitialize config
sed -i "s/log_timezone =.*$//g" $PGDATA/postgresql.conf
sed -i "s/timezone =.*$//g" $PGDATA/postgresql.conf
sed -i "s/wal_level =.*$//g" $config_file
sed -i "s/archive_mode =.*$//g" $config_file
sed -i "s/archive_timeout =.*$//g" $config_file
sed -i "s/archive_command =.*$//g" $config_file
# Configure wal-e
if [ "$wal" = true ] ; then
/docker-entrypoint-initdb.d/setup-wale.sh
fi
echo "log_timezone = $DEFAULT_TIMEZONE" >> $config_file
echo "timezone = $DEFAULT_TIMEZONE" >> $config_file
}
if [ "${1:0:1}" = '-' ]; then
@ -24,30 +35,28 @@ if [ "${1:0:1}" = '-' ]; then
fi
if [ "$1" = 'postgres' ]; then
# Check wal-e variables
wal_enable=true
VARS=(AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY WALE_S3_PREFIX AWS_REGION)
for v in ${VARS[@]}; do
if [ "${!v}" = "" ]; then
echo "$v is required for Wal-E but not set. Skipping Wal-E setup."
update_conf
# Run the postgresql entrypoint
. /docker-entrypoint.sh
exit
wal_enable=false
fi
done
umask u=rwx,g=rx,o=
mkdir -p /etc/wal-e.d/env
# Setup wal-e env variables
if [ "$wal_enable" = true ] ; then
for v in ${VARS[@]}; do
echo "${!v}" > /etc/wal-e.d/env/$v
export $v="${!v}"
done
chown -R root:postgres /etc/wal-e.d
WAL_LEVEL=archive
ARCHIVE_MODE=on
fi
# Update postgresql configuration
update_conf $wal_enable
update_conf
# Run the postgresql entrypoint
. /docker-entrypoint.sh
fi

View File

@ -1,7 +1,7 @@
#!/bin/bash
# wal-e specific
# wal-e specific configuration
echo "wal_level = $WAL_LEVEL" >> $PGDATA/postgresql.conf
echo "archive_mode = $ARCHIVE_MODE" >> $PGDATA/postgresql.conf
echo "archive_command = 'envdir /etc/wal-e.d/env /usr/local/bin/wal-e wal-push %p'" >> $PGDATA/postgresql.conf
echo "archive_command = '/usr/bin/wal-e wal-push %p'" >> $PGDATA/postgresql.conf
echo "archive_timeout = $ARCHIVE_TIMEOUT" >> $PGDATA/postgresql.conf