Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env-dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#
IMAGE_VERSION=4.0.1
21 changes: 21 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "docker" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"

- package-ecosystem: "pip" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "daily"
61 changes: 61 additions & 0 deletions .github/workflows/docker-build-n-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Docker Image CI

on:
push:
branches: [ "master", "dev" ]
pull_request:
branches: [ "master" ]
env:
IMAGE_NAME: gnzsnz/pgcli
PLATFORMS: linux/amd64,linux/arm64

jobs:
buildntest:
name: Build and test

runs-on: ubuntu-latest
continue-on-error: true

steps:

- name: Checkout
uses: actions/checkout@v4

- name: Get enviroment variables
run: |
grep -v '#' .env-dist | grep '=' > .env
while IFS= read -r line; do
echo $line >> $GITHUB_ENV ;
done < .env

- name: Set up QEMU
uses: docker/setup-qemu-action@v3
with:
platforms: ${{ env.PLATFORMS }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ env.IMAGE_NAME }}
ghcr.io/${{ env.IMAGE_NAME }}
flavor: |
latest=true

- name: Build Docker image
uses: docker/build-push-action@v5
with:
push: false
load: false
cache-from: type=gha
cache-to: type=gha,mode=max
context: .
platforms: ${{ env.PLATFORMS }}
build-args: |
IMAGE_VERSION=${{ env.IMAGE_VERSION}}
tags: ${{ env.IMAGE_NAME }}:${{ env.IMAGE_VERSION }}
labels: ${{ steps.meta.outputs.labels }}
70 changes: 70 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Docker Image Publishing

on:
release:
types: [published]

env:
IMAGE_NAME: gnzsnz/pgcli
PLATFORMS: linux/amd64,linux/arm64

jobs:
publish:
name: Build and test Jupyter Quant
runs-on: ubuntu-latest

steps:

- name: Checkout
uses: actions/checkout@v4

- name: Get enviroment variables
run: |
grep -v '#' .env-dist | grep '=' > .env
while IFS= read -r line; do
echo $line >> $GITHUB_ENV ;
done < .env

- name: Set up QEMU
uses: docker/setup-qemu-action@v3
with:
platforms: ${{ env.PLATFORMS }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ env.IMAGE_NAME }}
ghcr.io/${{ env.IMAGE_NAME }}
flavor: |
latest=true

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
IMAGE_VERSION=${{ env.IMAGE_VERSION}}
platforms: ${{ env.PLATFORMS }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
36 changes: 36 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- id: check-shebang-scripts-are-executable
- id: check-executables-have-shebangs
- id: requirements-txt-fixer
- repo: https://github.com/jumanjihouse/pre-commit-hooks
rev: 3.0.0
hooks:
- id: shellcheck
- id: shfmt
- repo: https://github.com/hadolint/hadolint
rev: v2.12.0
hooks:
- id: hadolint
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.37.0
hooks:
- id: markdownlint
args: ['--disable', 'MD013']
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.27.1
hooks:
- id: check-github-workflows
- id: check-github-actions
- repo: https://github.com/wemake-services/dotenv-linter
rev: 0.4.0 # Use the ref you want to point at
hooks:
- id: dotenv-linter
22 changes: 17 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
FROM python:3.7.0-slim
MAINTAINER Dennis Coldwell <[email protected]>
RUN apt-get -y update && apt-get -y install libpq-dev build-essential less
RUN pip install pgcli
ADD run-pgcli.sh /bin/run-pgcli.sh
FROM python:3-slim
ARG IMAGE_VERSION
ARG DEBIAN_FRONTEND=noninteractive

COPY run-pgcli.sh /bin/run-pgcli.sh
COPY requirements.txt /requirements.txt

RUN apt-get -y update && \
apt-get -y install --no-install-recommends less=590-2 && \
apt-get clean && rm -rf /var/lib/apt/lists/* && \
pip install --no-cache-dir -r requirements.txt

ENV PAGER=less
ENTRYPOINT ["run-pgcli.sh"]

LABEL org.opencontainers.image.source=https://github.com/gnzsnz/pgcli-docker
LABEL org.opencontainers.image.url=https://github.com/gnzsnz/pgcli-docker/pkgs/container/pgcli
LABEL org.opencontainers.image.description="A dockerized pgcli."
LABEL org.opencontainers.image.licenses="MIT"
LABEL org.opencontainers.image.version=${IMAGE_VERSION}
1 change: 0 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

53 changes: 38 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,67 @@
# pgcli-docker
Dockerized version of the [pgcli](http://pgcli.com/) tool for postgres.

Dockerized version of the [pgcli](http://pgcli.com/) tool for postgres.

![Screenshot.gif](https://raw.githubusercontent.com/dencold/pgcli-docker/master/screencap.gif)

### Quick usage
## Quick usage

1. Pull the docker image: `docker pull dencold/pgcli`
2. Run the container: `docker run -it --rm dencold/pgcli DB_URL`
3. Or, if you already have a postgres container, you can just do this: `docker run -it --link my-postgres:postgres --rm dencold/pgcli`
3. Or, if you already have a postgres container, you can just do this:
`docker run -it --link my-postgres:postgres --rm dencold/pgcli`

Note that *DB_URL* is in the format of:

`postgresql://user:password@host:port/dbname`

For more information on database connection strings, see the [postgres documentation](http://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING).
For more information on database connection strings, see the
[postgres documentation](http://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING).

For other run options, see the "Running" section, below.

### Why a docker version?
## Why a docker version?

Installing pgcli is a [fairly straightforward process](http://pgcli.com/install), as long as you already have postgres installed, you can generally get away with a straight `pip install`, however there are some gotchas. For example, on linux there are a host of other dependencies, from the pgcli installation guide:
Installing pgcli is a [fairly straightforward process](http://pgcli.com/install),
as long as you already have postgres installed, you can generally get away with
a straight `pip install`, however there are some gotchas. For example, on linux
there are a host of other dependencies, from the pgcli installation guide:

> Pgcli uses psycopg to talk to postgres database. In order to install psycopg, you will need libpq and python-dev installed on your system. Since psycopg is a C extension for Python, a C compiler is needed to install it on your system.
> Pgcli uses psycopg to talk to postgres database. In order to install psycopg,
> you will need libpq and python-dev installed on your system. Since psycopg is
> a C extension for Python, a C compiler is needed to install it on your system.

If you are already using [docker](https://www.docker.com/), life can be a lot easier. You are a simple pull away from getting pgcli on whatever environment you are running:
If you are already using [docker](https://www.docker.com/), life can be a lot
easier. You are a simple pull away from getting pgcli on whatever environment
you are running:

`docker pull dencold/pgcli`

There are a number of additional benefits of using a docker container, we'll see these in the next section...
There are a number of additional benefits of using a docker container, we'll
see these in the next section...

### Running
## Running

I generally run my development apps with a separate container for the postgres database. Hooking up the database to the application is achieved via [docker links](https://docs.docker.com/userguide/dockerlinks/). In order to make it easy to use `pgcli` on any postgres container, I've made a simple docker wrapper for the command. It is smart about pulling in the linked postgres environment variables and makes it easy to use on your own instances.
I generally run my development apps with a separate container for the postgres
database. Hooking up the database to the application is achieved via
[docker links](https://docs.docker.com/userguide/dockerlinks/). In order to
make it easy to use `pgcli` on any postgres container, I've made a simple
docker wrapper for the command. It is smart about pulling in the linked
postgres environment variables and makes it easy to use on your own instances.

If you have one of these postgres containers already running, all you need to do is link the postgres container to pgcli. For example, if your postgres container was named `my-postgres`, you can attach pgcli with this one-liner:
If you have one of these postgres containers already running, all you need to
do is link the postgres container to pgcli. For example, if your postgres
container was named `my-postgres`, you can attach pgcli with this one-liner:

```docker run -it --link my-postgres:postgres --rm dencold/pgcli```

It will determine host, port, and login details directly from the linked postgres container's environment variables. You should be dropped right into a pgcli session without the need to type in any credentials. Awesomesauce!
It will determine host, port, and login details directly from the linked
postgres container's environment variables. You should be dropped right into a
pgcli session without the need to type in any credentials. Awesomesauce!

### Version Notes
## Version Notes

This package used to pin pgcli's version to guarantee combatibility, but that's no longer the case. The docker container is created using `pip install pgcli` so you'll automatically get the latest version of the tool when you run your docker pull.
This package used to pin pgcli's version to guarantee combatibility, but that's
no longer the case. The docker container is created using `pip install pgcli`
so you'll automatically get the latest version of the tool when you run your
docker pull.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pgcli==4.0.1
psycopg[binary]==3.1.18
39 changes: 19 additions & 20 deletions run-pgcli.sh
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
#!/bin/bash
#
#
# Simple wrapper for the pgli tool. This script allows us to pull in linked
# docker containers if the user decides to bring up the cli that way.

db_url=$1

usage() {
echo "3 ways to use this container:"
echo ""
echo "1. Directly pass the database url:"
echo " docker run -it --rm dencold/pgcli postgres://DBUSER:DBPASS@DBHOST:DBPORT"
echo "2. Pass an environment variable via docker's '-e' argument"
echo " docker run -it --rm -e DB_URL=postgres://DBUSER:DBPASS@DBHOST:DBPORT dencold/pgcli"
echo "3. Automatically connect via a linked postgres container (my favorite):"
echo " docker run -it --rm --link YOURCONTAINER:postgres dencold/pgcli"
echo "3 ways to use this container:"
echo ""
echo "1. Directly pass the database url:"
echo " docker run -it --rm dencold/pgcli postgres://DBUSER:DBPASS@DBHOST:DBPORT"
echo "2. Pass an environment variable via docker's '-e' argument"
echo " docker run -it --rm -e DB_URL=postgres://DBUSER:DBPASS@DBHOST:DBPORT dencold/pgcli"
echo "3. Automatically connect via a linked postgres container (my favorite):"
echo " docker run -it --rm --link YOURCONTAINER:postgres dencold/pgcli"
}

if [ -n "$db_url" ]; then
# 1st priority goes to any argument passed to the script
pgcli "$db_url"
# 1st priority goes to any argument passed to the script
pgcli "$db_url"
elif [ -n "$DB_URL" ]; then
# next, if a DB_URL environment variable is set, use that
pgcli "$DB_URL"
# next, if a DB_URL environment variable is set, use that
pgcli "$DB_URL"
elif [ -n "$POSTGRES_PORT_5432_TCP_ADDR" ]; then
# if nothing is set, we try to construct a db_url from the env vars that docker
# automatically sets for the postgres container
pgcli postgres://$POSTGRES_ENV_POSTGRES_USER:$POSTGRES_ENV_POSTGRES_PASSWORD@$POSTGRES_PORT_5432_TCP_ADDR:$POSTGRES_PORT_5432_TCP_PORT
# if nothing is set, we try to construct a db_url from the env vars that docker
# automatically sets for the postgres container
pgcli "postgres://$POSTGRES_ENV_POSTGRES_USER:$POSTGRES_ENV_POSTGRES_PASSWORD@$POSTGRES_PORT_5432_TCP_ADDR:$POSTGRES_PORT_5432_TCP_PORT"
else
echo "Database URL not provided, please try again."
echo ""
usage
echo "Database URL not provided, please try again."
echo ""
usage
fi