Skip to content

Commit 3f9f633

Browse files
committed
Initial commit
0 parents  commit 3f9f633

10 files changed

+415
-0
lines changed

.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!entrypoint

CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## 0.1.0
2+
3+
Initial development release.
4+
5+
#### Notes
6+
7+
- Based off [tklx/base:0.1.0](https://github.com/tklx/base/releases/tag/0.1.0).
8+
- PostgreSQL installed directly from Debian.
9+
- Uses tini for zombie reaping and signal forwarding.
10+
- Uses gosu for dropping privileges to postgres user.
11+
- Includes ``VOLUME /var/lib/postgresql/data`` for persistence.
12+
- Includes ``EXPOSE 5432`` for container linking.
13+
- Basic bats testing suite.
14+

Dockerfile

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
FROM tklx/base:0.1.0
2+
3+
ENV PGDATA=/var/lib/postgresql/data PATH=/usr/lib/postgresql/9.4/bin:$PATH LANG=en_US.UTF-8 LOCALE=en_US.UTF-8
4+
5+
ENV TINI_VERSION=v0.9.0
6+
RUN set -x \
7+
&& TINI_URL=https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini \
8+
&& TINI_GPGKEY=0527A9B7 \
9+
&& export GNUPGHOME="$(mktemp -d)" \
10+
&& apt-get update \
11+
&& apt-get -y install wget ca-certificates \
12+
&& wget -O /tini ${TINI_URL} \
13+
&& wget -O /tini.asc ${TINI_URL}.asc \
14+
&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys ${TINI_GPGKEY} \
15+
&& gpg --verify /tini.asc \
16+
&& chmod +x /tini \
17+
&& rm -r ${GNUPGHOME} /tini.asc \
18+
&& apt-get purge -y --auto-remove wget ca-certificates \
19+
&& apt-clean --aggressive
20+
21+
ENV GOSU_VERSION=1.9
22+
RUN set -x \
23+
&& GOSU_URL=https://github.com/tianon/gosu/releases/download/${GOSU_VERSION}/gosu-$(dpkg --print-architecture) \
24+
&& GOSU_GPGKEY=B42F6819007F00F88E364FD4036A9C25BF357DD4 \
25+
&& export GNUPGHOME="$(mktemp -d)" \
26+
&& apt-get update \
27+
&& apt-get -y install wget ca-certificates \
28+
&& wget -O /usr/local/sbin/gosu ${GOSU_URL} \
29+
&& wget -O /usr/local/sbin/gosu.asc ${GOSU_URL}.asc \
30+
&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys ${GOSU_GPGKEY} \
31+
&& gpg --verify /usr/local/sbin/gosu.asc \
32+
&& chmod +x /usr/local/sbin/gosu && gosu nobody true \
33+
&& rm -r ${GNUPGHOME} /usr/local/sbin/gosu.asc \
34+
&& apt-get purge -y --auto-remove wget ca-certificates \
35+
&& apt-clean --aggressive
36+
37+
RUN set -x \
38+
&& apt-get update \
39+
&& apt-get -y install postgresql \
40+
&& localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8 \
41+
&& sed -ri 's/#(create_main_cluster) .*$/\1 = false/' /etc/postgresql-common/createcluster.conf \
42+
&& apt-clean --aggressive
43+
44+
VOLUME "/var/lib/postgresql/data"
45+
RUN set -x && mkdir -p $PGDATA && chown -R postgres:postgres $PGDATA
46+
47+
COPY entrypoint /entrypoint
48+
ENTRYPOINT ["/tini", "--", "/entrypoint"]
49+
EXPOSE 5432
50+
CMD ["postgres"]
51+

README.md

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# tklx/postgresql - SQL database
2+
3+
[![CircleCI](https://circleci.com/gh/tklx/postgres.svg?style=shield)](https://circleci.com/gh/tklx/postgres)
4+
5+
[PostgreSQL][postgres], often simply Postgres, is an object-relational database management system (ORDBMS) with an emphasis on extensibility and standards-compliance. As a database server, its primary function is to store data securely, and to allow for retrieval at the request of other software applications. It can handle workloads ranging from small single-machine applications to large Internet-facing applications with many concurrent users.
6+
7+
## Features
8+
9+
- Based on the super slim [tklx/base][base] (Debian GNU/Linux).
10+
- PostgreSQL installed directly from Debian.
11+
- Uses [tini][tini] for zombie reaping and signal forwarding.
12+
- Uses [gosu][gosu] for dropping privileges to postgres user.
13+
- Includes ``VOLUME /var/lib/postgresql/data`` for persistence.
14+
- Includes ``EXPOSE 5432``, so standard container linking will make it
15+
automatically available to the linked containers.
16+
17+
## Usage
18+
19+
### Start a postgres instance and connect to it from an application
20+
21+
```console
22+
$ docker run --name some-postgres -e POSTGRES_PASSWORD='secretpass' -d tklx/postgres
23+
$ docker run --name some-app --link some-postgres:postgres -d app-that-uses-postgres
24+
```
25+
26+
### Initialization
27+
28+
The image can be initialized by passing the following options as [environment variables][1] to ```docker run```:
29+
30+
PGDATA - Postgres data location (/var/lib/postgresql/data by default)
31+
POSTGRES_DB - name of database to initialize
32+
POSTGRES_USER - name of database user to assign as owner of POSTGRES_DB
33+
POSTGRES_PASSWORD - password for database user or ```postgres``` if user not specified
34+
35+
```POSTGRES_PASSWORD``` is required for remote access (e. g. from linked containers). If it is not supplied and the datastore was not already initialized, only local access will be allowed.
36+
37+
### Tips
38+
39+
```console
40+
# postgresql client options
41+
$ docker run --rm tklx/postgresql psql --help
42+
43+
# postgres options
44+
$ docker run --rm tklx/postgresql --help
45+
46+
# launch a regular PostgreSQL instance
47+
$ docker run --name some-postgres -e POSTGRES_USER=someuser POSTGRES_DB=somedb -e POSTGRES_PASSWORD=mysecretpassword -d tklx/postgres
48+
49+
# link an application to it
50+
$ docker run --name some-app --link some-postgres:tklx/postgres -d app-that-uses-postgres
51+
52+
# access through psql
53+
$ docker run -it --rm --link some-postgres:tklx/postgres tklx/postgres psql -h postgres -U postgres
54+
psql (9.4.8)
55+
Type "help" for help.
56+
57+
postgres=# SELECT 1;
58+
?column?
59+
----------
60+
1
61+
(1 row)
62+
63+
# local access through psql
64+
$ docker exec -it some-postgres psql -h localhost -U postgres
65+
66+
```
67+
68+
## Automated builds
69+
70+
The [Docker image](https://hub.docker.com/r/tklx/postgres/) is built, tested and pushed by [CircleCI](https://circleci.com/gh/tklx/postgres) from source hosted on [GitHub](https://github.com/tklx/postgres).
71+
72+
* Tag: ``x.y.z`` refers to a [release](https://github.com/tklx/mongodb/releases) (recommended).
73+
* Tag: ``latest`` refers to the master branch.
74+
75+
## Status
76+
77+
Currently on major version zero (0.y.z). Per [Semantic Versioning][semver],
78+
major version zero is for initial development, and should not be considered
79+
stable. Anything may change at any time.
80+
81+
## Issue Tracker
82+
83+
TKLX uses a central [issue tracker][tracker] on GitHub for reporting and
84+
tracking of bugs, issues and feature requests.
85+
86+
[postgres]: https://www.postgresql.org/
87+
[base]: https://github.com/tklx/base
88+
[tini]: https://github.com/krallin/tini
89+
[gosu]: https://github.com/tianon/gosu
90+
[semver]: http://semver.org/
91+
[tracker]: https://github.com/tklx/tracker/issues

circle.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
machine:
2+
services:
3+
- docker
4+
environment:
5+
appname: "postgres"
6+
7+
dependencies:
8+
override:
9+
- docker info
10+
- docker pull tklx/$appname:latest || true
11+
- docker build -t tklx/$appname:latest .
12+
- docker images
13+
14+
test:
15+
pre:
16+
- git clone https://github.com/tklx/bats.git
17+
- sudo bats/install.sh /usr/local
18+
override:
19+
- IMAGE=tklx/$appname:latest bats --tap tests/basics.bats
20+
21+
deployment:
22+
latest:
23+
branch: master
24+
commands:
25+
- docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS
26+
- docker push tklx/$appname:latest
27+
release:
28+
tag: /^([0-9]+)\.([0-9]+)\.([0-9]+)$/
29+
owner: tklx
30+
commands:
31+
- docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS
32+
- docker tag tklx/$appname:latest tklx/$appname:$CIRCLE_TAG
33+
- docker push tklx/$appname:$CIRCLE_TAG

contrib/generate-changelog

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash -e
2+
3+
fatal() { echo "fatal [$(basename $0)]: $@" 1>&2; exit 1; }
4+
5+
usage() {
6+
cat<<EOF
7+
Syntax: $0
8+
Convenience script to output updated CHANGELOG.md for manual editing
9+
10+
Notes::
11+
12+
- auto-increments major.minor.PATCH from latest git tag (or 0.1.0)
13+
- includes short git log of changes since latest git tag (or all)
14+
15+
EOF
16+
exit 1
17+
}
18+
19+
increment_version() {
20+
version=$1
21+
last_char=${version#${version%?}}
22+
echo $version | sed 's/[0-9]$/'"$((last_char+1))"'/'
23+
}
24+
25+
get_log() {
26+
since=$1
27+
[ "$since" ] && between=$since..HEAD
28+
git --no-pager log $between --reverse --pretty=format:'- %s'
29+
}
30+
31+
[[ "$#" = "0" ]] || usage
32+
33+
latest=$(git tag -l | head -1)
34+
gitlog=$(get_log $latest)
35+
[ "$latest" ] && version=$(increment_version $latest)
36+
37+
echo -e "## ${version:-0.1.0}\n"
38+
echo -e "<replace with high level summary>\n"
39+
echo -e "#### New features\n"
40+
echo -e "#### Bugfixes\n"
41+
echo "${gitlog}" | grep -i fix && echo || true
42+
echo -e "#### Other changes\n"
43+
echo "${gitlog}" | grep -vi fix && echo || true
44+
[ -e CHANGELOG.md ] && cat CHANGELOG.md
45+
46+
exit 0

docs/release.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## update changelog, create signed tag and push to github
2+
3+
```
4+
contrib/generate-changelog > CHANGELOG.tmp
5+
mv CHANGELOG.tmp CHANGELOG.md
6+
$EDITOR CHANGELOG.md # verify version is correct and tweak
7+
VERSION=$(head -1 CHANGELOG.md | awk '{print $2}')
8+
git add CHANGELOG.md
9+
git commit -m "changelog: updated for $VERSION release"
10+
git tag -s -m "$VERSION release" $VERSION
11+
git push github
12+
git push github --tags
13+
```
14+

entrypoint

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/bash -e
2+
3+
RUNDIR=/run/postgresql
4+
5+
if [[ ${1:0:1} = '-' ]]; then
6+
set -- postgres "$@"
7+
fi
8+
9+
if [[ $1 = postgres ]]; then
10+
mkdir -p "$PGDATA"
11+
chmod 700 "$PGDATA"
12+
chown -R postgres "$PGDATA" "$RUNDIR"
13+
chmod g+s "$RUNDIR"
14+
15+
# look specifically for PG_VERSION, as it is expected in the DB dir
16+
if [[ ! -s $PGDATA/PG_VERSION ]]; then
17+
eval "gosu postgres initdb $POSTGRES_INITDB_ARGS"
18+
19+
if [[ -n $POSTGRES_PASSWORD ]]; then
20+
pass="PASSWORD '$POSTGRES_PASSWORD'"
21+
22+
echo "host all all 0.0.0.0/0 md5" >> "$PGDATA/pg_hba.conf"
23+
sed -ri "s|^#?(listen_addresses)\s*=\s*\S+.*|\1 = '*'|" $PGDATA/postgresql.conf
24+
else
25+
pass=
26+
echo 'WARNING: No $POSTGRES_PASSWORD set! Only local access enabled.'
27+
fi
28+
29+
gosu postgres pg_ctl -o "-c listen_addresses='localhost'" -w start
30+
31+
: ${POSTGRES_USER:=postgres}
32+
: ${POSTGRES_DB:=$POSTGRES_USER}
33+
export POSTGRES_USER POSTGRES_DB
34+
35+
psql=( psql -v ON_ERROR_STOP=1 )
36+
37+
if [[ $POSTGRES_DB != postgres ]]; then
38+
"${psql[@]}" --username postgres <<<"CREATE DATABASE \"$POSTGRES_DB\";"
39+
echo
40+
fi
41+
42+
if [[ $POSTGRES_USER = postgres ]]; then
43+
op='ALTER'
44+
else
45+
op='CREATE'
46+
fi
47+
48+
"${psql[@]}" --username postgres <<<"$op USER \"$POSTGRES_USER\" WITH SUPERUSER $pass;"
49+
50+
psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" )
51+
52+
gosu postgres pg_ctl -m fast -w stop
53+
54+
echo 'PostgreSQL init process complete; ready for start up.'
55+
fi
56+
57+
exec gosu postgres "$@"
58+
fi
59+
60+
exec "$@"
61+

tests/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Install dependencies
2+
3+
```console
4+
git clone https://github.com/tklx/bats.git
5+
bats/install.sh /usr/local
6+
```
7+
8+
## Run the tests
9+
10+
```console
11+
IMAGE=tklx/postgres bats --tap tests/basics.bats
12+
13+
init: running tklx/postgres
14+
init: waiting for tklx/postgres to accept connections...
15+

0 commit comments

Comments
 (0)