From 22a4e8701928112c9018204da90aea19f865ac39 Mon Sep 17 00:00:00 2001 From: gonzo Date: Thu, 21 Mar 2024 11:46:56 +0100 Subject: [PATCH 1/4] update to latest python base image, install psycopg-binary, remove unnecessary packages --- Dockerfile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7157fd7..dd1f1bd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,8 @@ -FROM python:3.7.0-slim +FROM python:3-slim MAINTAINER Dennis Coldwell -RUN apt-get -y update && apt-get -y install libpq-dev build-essential less -RUN pip install pgcli +RUN apt-get -y update && DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends less && \ + apt-get clean && rm -rf /var/lib/apt/lists/* && \ + pip install --no-cache-dir psycopg[binary] pgcli ADD run-pgcli.sh /bin/run-pgcli.sh ENV PAGER=less ENTRYPOINT ["run-pgcli.sh"] From d1b4b5db142ab914746c0101b91b3fb472b8af94 Mon Sep 17 00:00:00 2001 From: gonzo Date: Wed, 3 Apr 2024 15:31:19 +0200 Subject: [PATCH 2/4] many changes - implement github actions - implement dependabot - refactor Dockerfile - implement pre-commit hooks Changes to be committed: new file: .env-dist new file: .github/dependabot.yml new file: .github/workflows/docker-build-n-test.yml new file: .github/workflows/docker-publish.yml new file: .pre-commit-config.yaml modified: Dockerfile new file: requirements.txt --- .env-dist | 2 + .github/dependabot.yml | 21 +++++++ .github/workflows/docker-build-n-test.yml | 61 ++++++++++++++++++++ .github/workflows/docker-publish.yml | 70 +++++++++++++++++++++++ .pre-commit-config.yaml | 35 ++++++++++++ Dockerfile | 19 ++++-- requirements.txt | 2 + 7 files changed, 206 insertions(+), 4 deletions(-) create mode 100644 .env-dist create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/docker-build-n-test.yml create mode 100644 .github/workflows/docker-publish.yml create mode 100644 .pre-commit-config.yaml create mode 100644 requirements.txt diff --git a/.env-dist b/.env-dist new file mode 100644 index 0000000..6a24dd8 --- /dev/null +++ b/.env-dist @@ -0,0 +1,2 @@ +# +IMAGE_VERSION=4.0.1 diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..c6acb1e --- /dev/null +++ b/.github/dependabot.yml @@ -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" diff --git a/.github/workflows/docker-build-n-test.yml b/.github/workflows/docker-build-n-test.yml new file mode 100644 index 0000000..ff3c381 --- /dev/null +++ b/.github/workflows/docker-build-n-test.yml @@ -0,0 +1,61 @@ +name: Docker Image CI + +on: + push: + branches: [ "master", "dev" ] + pull_request: + branches: [ "master" ] +env: + IMAGE_NAME: gnzsnz/pgcli-docker + 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 }} diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..5103619 --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,70 @@ +name: Docker Image Publishing + +on: + release: + types: [published] + +env: + IMAGE_NAME: gnzsnz/pgcli-docker + 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 }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..f461671 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,35 @@ +# 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 + - 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 diff --git a/Dockerfile b/Dockerfile index dd1f1bd..b6558ea 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,20 @@ FROM python:3-slim -MAINTAINER Dennis Coldwell -RUN apt-get -y update && DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends less && \ +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-1ubuntu0.22.04.2 && \ apt-get clean && rm -rf /var/lib/apt/lists/* && \ - pip install --no-cache-dir psycopg[binary] pgcli -ADD run-pgcli.sh /bin/run-pgcli.sh + 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-docker +LABEL org.opencontainers.image.description="A dockerized pgcli." +LABEL org.opencontainers.image.licenses="MIT" +LABEL org.opencontainers.image.version=${IMAGE_VERSION} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5a47673 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +pgcli==4.0.1 +psycopg[binary]==3.1.18 From 81c18f95cae4c4c5ce34224b5e835273776ae5aa Mon Sep 17 00:00:00 2001 From: gonzo Date: Wed, 3 Apr 2024 15:55:33 +0200 Subject: [PATCH 3/4] fix less version --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index b6558ea..7d9f28e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ 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-1ubuntu0.22.04.2 && \ + 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 From 56f2726bd5991ceb202a2a509f63a186cc0f4d6c Mon Sep 17 00:00:00 2001 From: gonzo Date: Wed, 3 Apr 2024 20:45:11 +0200 Subject: [PATCH 4/4] apply linter on all files rename docker image in repository Changes to be committed: modified: .github/workflows/docker-build-n-test.yml modified: .github/workflows/docker-publish.yml modified: .pre-commit-config.yaml modified: Dockerfile modified: LICENSE modified: README.md modified: run-pgcli.sh --- .github/workflows/docker-build-n-test.yml | 2 +- .github/workflows/docker-publish.yml | 2 +- .pre-commit-config.yaml | 1 + Dockerfile | 2 +- LICENSE | 1 - README.md | 53 ++++++++++++++++------- run-pgcli.sh | 39 ++++++++--------- 7 files changed, 61 insertions(+), 39 deletions(-) diff --git a/.github/workflows/docker-build-n-test.yml b/.github/workflows/docker-build-n-test.yml index ff3c381..2c0b837 100644 --- a/.github/workflows/docker-build-n-test.yml +++ b/.github/workflows/docker-build-n-test.yml @@ -6,7 +6,7 @@ on: pull_request: branches: [ "master" ] env: - IMAGE_NAME: gnzsnz/pgcli-docker + IMAGE_NAME: gnzsnz/pgcli PLATFORMS: linux/amd64,linux/arm64 jobs: diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index 5103619..aa86d91 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -5,7 +5,7 @@ on: types: [published] env: - IMAGE_NAME: gnzsnz/pgcli-docker + IMAGE_NAME: gnzsnz/pgcli PLATFORMS: linux/amd64,linux/arm64 jobs: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f461671..79d8e58 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -24,6 +24,7 @@ repos: rev: v0.37.0 hooks: - id: markdownlint + args: ['--disable', 'MD013'] - repo: https://github.com/python-jsonschema/check-jsonschema rev: 0.27.1 hooks: diff --git a/Dockerfile b/Dockerfile index 7d9f28e..093c39f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,7 +14,7 @@ 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-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} diff --git a/LICENSE b/LICENSE index e3319b2..08e7ac7 100644 --- a/LICENSE +++ b/LICENSE @@ -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. - diff --git a/README.md b/README.md index 73ccf8b..f2c35cf 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/run-pgcli.sh b/run-pgcli.sh index 8609740..b363c87 100755 --- a/run-pgcli.sh +++ b/run-pgcli.sh @@ -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 -