Skip to content

Commit fb95d55

Browse files
committed
Initial checkin
0 parents  commit fb95d55

File tree

10 files changed

+256
-0
lines changed

10 files changed

+256
-0
lines changed

.dockerignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.md
2+
test
3+
LICENSE
4+
Makefile
5+
Vagrantfile
6+
.DS_Store
7+
*.swp
8+
bats

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "bats"]
2+
path = bats
3+
url = https://github.com/bats-core/bats-core/

.travis.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
branches:
3+
only:
4+
- master
5+
6+
dist: trusty
7+
language: minimal
8+
9+
jobs:
10+
include:
11+
- stage: release
12+
script:
13+
- echo "${DOCKER_PASSWORD}" | docker login -u "${DOCKER_USERNAME}" --password-stdin
14+
- make build DEBUG=false
15+
- make tag_latest DEBUG=false
16+
- make release DEBUG=false
17+
- curl -s -X POST "https://hooks.microbadger.com/images/${TRAVIS_REPO_SLUG}/AG15xvvq_g10B9tU3v8EB9E9eco="

Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM gliderlabs/alpine:3.4
2+
MAINTAINER Madhav Raj Maharjan <madhav.maharjan@gmail.com>
3+
4+
ARG VCS_REF
5+
ARG POSTGRESQL_VERSION
6+
ARG DEBUG=false
7+
8+
LABEL description="Docker container with Postgresql Client" os_version="Alpine 3.4" \
9+
org.label-schema.vcs-ref=${VCS_REF} org.label-schema.vcs-url="https://github.com/madharjan/docker-postgresql-client"
10+
11+
ENV HOME /root
12+
ENV ALPINE_VERSION 3.4
13+
ENV POSTGRESQL_VERSION ${POSTGRESQL_VERSION}
14+
15+
COPY ./psql.sh /
16+
17+
RUN apk update && \
18+
apk add bash postgresql-client && \
19+
rm -rf /var/cache/apk/* \
20+
rm -rf /tmp/* && \
21+
chmod 755 /psql.sh
22+
23+
WORKDIR /root
24+
25+
ENTRYPOINT ["/psql.sh"]

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2016 Madhav Raj Maharjan (madhav.maharjan@gmail.com)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

Makefile

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
NAME = madharjan/docker-postgresql-client
3+
VERSION = 9.5
4+
5+
DEBUG ?= true
6+
7+
DOCKER_USERNAME ?= $(shell read -p "DockerHub Username: " pwd; echo $$pwd)
8+
DOCKER_PASSWORD ?= $(shell stty -echo; read -p "DockerHub Password: " pwd; stty echo; echo $$pwd)
9+
DOCKER_LOGIN ?= $(shell cat ~/.docker/config.json | grep "docker.io" | wc -l)
10+
11+
.PHONY: all build run test stop clean tag_latest release clean_images
12+
13+
all: build
14+
15+
docker_login:
16+
ifeq ($(DOCKER_LOGIN), 1)
17+
@echo "Already login to DockerHub"
18+
else
19+
@docker login -u $(DOCKER_USERNAME) -p $(DOCKER_PASSWORD)
20+
endif
21+
22+
build:
23+
docker build \
24+
--build-arg POSTGRESQL_VERSION=$(VERSION) \
25+
--build-arg VCS_REF=`git rev-parse --short HEAD` \
26+
--build-arg DEBUG=${DEBUG} \
27+
-t $(NAME):$(VERSION) --rm .
28+
29+
run:
30+
@if ! docker images $(NAME) | awk '{ print $$2 }' | grep -q -F $(VERSION); then echo "$(NAME) version $(VERSION) is not yet built. Please run 'make build'"; false; fi
31+
32+
docker run -d \
33+
-e POSTGRESQL_DATABASE=mydb \
34+
-e POSTGRESQL_USERNAME=myuser \
35+
-e POSTGRESQL_PASSWORD=mypass \
36+
-e DEBUG=${DEBUG} \
37+
--name postgresql_client madharjan/docker-postgresql:9.5
38+
sleep 2
39+
40+
test:
41+
sleep 5
42+
./bats/bin/bats test/tests.bats
43+
44+
stop:
45+
docker exec postgresql_client /bin/bash -c "sv stop postgresql" 2> /dev/null || true
46+
sleep 4
47+
docker stop postgresql_client 2> /dev/null || true
48+
49+
clean: stop
50+
docker rm postgresql_client 2> /dev/null || true
51+
docker images | grep "<none>" | awk '{print$3 }' | xargs docker rmi 2> /dev/null || true
52+
53+
publish: docker_login run test clean
54+
docker push $(NAME)
55+
56+
tag_latest:
57+
docker tag $(NAME):$(VERSION) $(NAME):latest
58+
59+
release: docker_login run test clean tag_latest
60+
docker push $(NAME)
61+
62+
clean_images: clean
63+
docker rmi $(NAME):latest $(NAME):$(VERSION) 2> /dev/null || true
64+
docker logout
65+
66+

README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# docker-postgresql-client
2+
3+
[![Build Status](https://travis-ci.com/madharjan/docker-postgresql-client.svg?branch=master)](https://travis-ci.com/madharjan/docker-postgresql-client)
4+
[![Layers](https://images.microbadger.com/badges/image/madharjan/docker-postgresql-client.svg)](http://microbadger.com/images/madharjan/docker-postgresql-client)
5+
6+
Docker container with PostgreSQL Client Server based on [gliderlabs/alpine](https://github.com/gliderlabs/docker-alpine/)
7+
8+
## Features
9+
10+
* Bats [bats-core/bats-core](https://github.com/bats-core/bats-core) based test cases
11+
12+
## PostgreSQL Client 9.5 (docker-postgresql-client)
13+
14+
### Environment
15+
16+
| Variable | Default | Example |
17+
|----------------------|--------------|----------------|
18+
| POSTGRESQL_HOST | | 192.168.1.1 |
19+
| POSTGRESQL_PORT | 5432 | 1235 |
20+
| POSTGRESQL_DATABASE | postgres | mydb |
21+
| POSTGRESQL_USERNAME | postgres | myuser |
22+
| POSTGRESQL_PASSWORD | | mypass |
23+
24+
## Build
25+
26+
```bash
27+
# clone project
28+
git clone https://github.com/madharjan/docker-postgresql-client
29+
cd docker-postgresql-client
30+
31+
# login to DockerHub
32+
docker login
33+
34+
# build
35+
make
36+
37+
# tests
38+
make run
39+
make test
40+
41+
# clean
42+
make clean
43+
```
44+
45+
## Run
46+
47+
### Postgres Server (docker-postgresql)
48+
49+
```bash
50+
# stop & remove previous instances
51+
docker stop postgresql
52+
docker rm postgresql
53+
# run container
54+
docker run -d \
55+
-e POSTGRESQL_PASSWORD=mypass \
56+
--name postgresql \
57+
madharjan/docker-postgresql:9.5
58+
```
59+
60+
### Postgres Client (docker-postgresql-client)
61+
62+
```bash
63+
# psql console
64+
docker run --rm -it \
65+
--link postgresql:db \
66+
-e POSTGRESQL_HOST=db \
67+
-e POSTGRESQL_PASSWORD=mypass \
68+
madharjan/docker-postgresql-client:9.5
69+
70+
# psql script
71+
docker run --rm -it \
72+
--link postgresql:db \
73+
-e POSTGRESQL_HOST=db \
74+
-e POSTGRESQL_PASSWORD=mypass \
75+
madharjan/docker-postgresql-client:9.5 \
76+
-c 'select user from user'
77+
```
78+
79+
### Cleanup
80+
81+
```bash
82+
docker stop postgresql
83+
docker rm postgresql
84+
85+
``

bats

Submodule bats added at c706d14

psql.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
set -e
3+
4+
if [ "${DEBUG}" = true ]; then
5+
set -x
6+
fi
7+
8+
DEF_POSTGRESQL_HOST=
9+
DEF_POSTGRESQL_PORT=5432
10+
DEF_POSTGRESQL_DATABASE=postgres
11+
DEF_POSTGRESQL_USERNAME=postgres
12+
DEF_POSTGRESQL_PASSWORD=""
13+
14+
POSTGRESQL_HOST=${POSTGRESQL_HOST:-$DEF_POSTGRESQL_HOST}
15+
POSTGRESQL_PORT=${POSTGRESQL_PORT:-$DEF_POSTGRESQL_PORT}
16+
POSTGRESQL_DATABASE=${POSTGRESQL_DATABASE:-$DEF_POSTGRESQL_DATABASE}
17+
POSTGRESQL_USERNAME=${POSTGRESQL_USERNAME:-$DEF_POSTGRESQL_USERNAME}
18+
POSTGRESQL_PASSWORD=${POSTGRESQL_PASSWORD:-$DEF_POSTGRESQL_PASSWORD}
19+
20+
exec /usr/bin/psql postgresql://${POSTGRESQL_USERNAME}:${POSTGRESQL_PASSWORD}@${POSTGRESQL_HOST}:${POSTGRESQL_PORT}/${POSTGRESQL_DATABASE} "$@"

test/tests.bats

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
@test "checking connect & execute sql: postgres-server" {
3+
run docker run --rm -it \
4+
--link postgresql_client:db \
5+
-e POSTGRESQL_HOST=db \
6+
-e POSTGRESQL_DATABASE=mydb \
7+
-e POSTGRESQL_USERNAME=myuser \
8+
-e POSTGRESQL_PASSWORD=mypass \
9+
madharjan/docker-postgresql-client:9.5 \
10+
-P tuples_only -c 'select user from user;'
11+
[ "$status" -eq 0 ]
12+
}

0 commit comments

Comments
 (0)