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 # Install some packages to use WAL
RUN apt-get update \ RUN apk add --no-cache \
&& apt-get install -y \ build-base \
build-essential \
curl \ curl \
daemontools \ libc6-compat \
libffi-dev \ libffi-dev \
libssl-dev \ linux-headers \
lzop \
pv \
python-dev \ python-dev \
py-cryptography \
&& curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | python \ && curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | python \
&& pip install 'wal-e<1.0.0' \ && pip --no-cache-dir install 'wal-e<1.0.0' envdir \
&& apt-get remove -y \ && rm -rf /var/cache/apk/* /tmp/* /var/tmp/*
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/*
# Add wale script # Add wale script
COPY setup-wale.sh /docker-entrypoint-initdb.d/ COPY setup-wale.sh /docker-entrypoint-initdb.d/

View File

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

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# wal-e specific # wal-e specific configuration
echo "wal_level = $WAL_LEVEL" >> $PGDATA/postgresql.conf echo "wal_level = $WAL_LEVEL" >> $PGDATA/postgresql.conf
echo "archive_mode = $ARCHIVE_MODE" >> $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 echo "archive_timeout = $ARCHIVE_TIMEOUT" >> $PGDATA/postgresql.conf