From e0c2a2a1bd5187dcaa902fae2edbc4396408aa42 Mon Sep 17 00:00:00 2001 From: huzzy Date: Sun, 15 Mar 2026 17:20:32 +0600 Subject: [PATCH 1/4] Add postgres localip command --- README.md | 1 + common-functions | 12 ++++++++++++ subcommands/localip | 22 ++++++++++++++++++++++ tests/service_localip.bats | 29 +++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 subcommands/localip create mode 100644 tests/service_localip.bats diff --git a/README.md b/README.md index 19723b12..efb537dc 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ postgres:link [--link-flags...] # link the postgres service t postgres:linked # check if the postgres service is linked to an app postgres:links # list all apps linked to the postgres service postgres:list # list all postgres services +postgres:localip # print the local docker network ip for a running service postgres:logs [-t|--tail] # print the most recent log(s) for this service postgres:pause # pause a running postgres service postgres:promote # promote service as DATABASE_URL in diff --git a/common-functions b/common-functions index b8c834b0..31b3de4b 100755 --- a/common-functions +++ b/common-functions @@ -98,6 +98,18 @@ get_container_ip() { "$DOCKER_BIN" container inspect --format '{{ .NetworkSettings.IPAddress }}' "$CONTAINER_ID" 2>/dev/null } +service_local_ip() { + declare desc="retrieve the current local ip for a service container" + declare SERVICE="$1" + local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" + local ID="$(cat "$SERVICE_ROOT/ID")" + + "$DOCKER_BIN" container inspect "$ID" >/dev/null 2>&1 || dokku_log_fail "Service container does not exist" + is_container_status "$ID" "Running" || dokku_log_fail "Service container is not running" + + get_container_ip "$ID" +} + get_database_name() { declare desc="retrieve a sanitized database name" declare SERVICE="$1" diff --git a/subcommands/localip b/subcommands/localip new file mode 100644 index 00000000..895a1ab7 --- /dev/null +++ b/subcommands/localip @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/config" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/functions" + +service-localip-cmd() { + #E print the local docker network ip for a running service: + #E dokku $PLUGIN_COMMAND_PREFIX:localip lollipop + #A service, service to run command against + declare desc="print the local docker network ip for a running service" + local cmd="$PLUGIN_COMMAND_PREFIX:localip" argv=("$@") + [[ ${argv[0]} == "$cmd" ]] && shift 1 + declare SERVICE="$1" + + [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a valid name for the service" + verify_service_name "$SERVICE" + service_local_ip "$SERVICE" +} + +service-localip-cmd "$@" diff --git a/tests/service_localip.bats b/tests/service_localip.bats new file mode 100644 index 00000000..ad3fedb4 --- /dev/null +++ b/tests/service_localip.bats @@ -0,0 +1,29 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + dokku "$PLUGIN_COMMAND_PREFIX:create" l +} + +teardown() { + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l +} + +@test "($PLUGIN_COMMAND_PREFIX:localip) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:localip" + assert_contains "${lines[*]}" "Please specify a valid name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:localip) error when service does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:localip" not_existing_service + assert_contains "${lines[*]}" "service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:localip) success" { + local expected_ip + expected_ip="$(docker inspect "dokku.$PLUGIN_COMMAND_PREFIX.l" -f '{{ .NetworkSettings.IPAddress }}')" + + run dokku "$PLUGIN_COMMAND_PREFIX:localip" l + assert_success + assert_output "$expected_ip" +} From e934ba44fff836103cfdb3a0d65265250e2514bb Mon Sep 17 00:00:00 2001 From: huzzy Date: Sun, 15 Mar 2026 17:25:29 +0600 Subject: [PATCH 2/4] Mark localip subcommand executable --- subcommands/localip | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 subcommands/localip diff --git a/subcommands/localip b/subcommands/localip old mode 100644 new mode 100755 From f818418c40717ac984842f89ffd90bb1de025b89 Mon Sep 17 00:00:00 2001 From: huzzy Date: Sun, 15 Mar 2026 17:28:32 +0600 Subject: [PATCH 3/4] Fix localip for user-defined docker networks --- common-functions | 11 ++++++++++- tests/service_localip.bats | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/common-functions b/common-functions index 31b3de4b..8991194c 100755 --- a/common-functions +++ b/common-functions @@ -95,7 +95,16 @@ docker_ports_options() { get_container_ip() { declare desc="retrieve the ip address of a container" declare CONTAINER_ID="$1" - "$DOCKER_BIN" container inspect --format '{{ .NetworkSettings.IPAddress }}' "$CONTAINER_ID" 2>/dev/null + local IP_ADDRESS + + IP_ADDRESS=$("$DOCKER_BIN" container inspect --format '{{ .NetworkSettings.IPAddress }}' "$CONTAINER_ID" 2>/dev/null || true) + if [[ -n "$IP_ADDRESS" ]]; then + echo "$IP_ADDRESS" + return 0 + fi + + IP_ADDRESS=$("$DOCKER_BIN" container inspect --format '{{range .NetworkSettings.Networks}}{{println .IPAddress}}{{end}}' "$CONTAINER_ID" 2>/dev/null || true) + echo "$IP_ADDRESS" | awk 'NF { print; exit }' } service_local_ip() { diff --git a/tests/service_localip.bats b/tests/service_localip.bats index ad3fedb4..e3e216cd 100644 --- a/tests/service_localip.bats +++ b/tests/service_localip.bats @@ -22,6 +22,9 @@ teardown() { @test "($PLUGIN_COMMAND_PREFIX:localip) success" { local expected_ip expected_ip="$(docker inspect "dokku.$PLUGIN_COMMAND_PREFIX.l" -f '{{ .NetworkSettings.IPAddress }}')" + if [[ -z "$expected_ip" ]]; then + expected_ip="$(docker inspect "dokku.$PLUGIN_COMMAND_PREFIX.l" -f '{{range .NetworkSettings.Networks}}{{println .IPAddress}}{{end}}' | awk 'NF { print; exit }')" + fi run dokku "$PLUGIN_COMMAND_PREFIX:localip" l assert_success From 91782412eb36dd4c6f81b3e10a0e39d146adf048 Mon Sep 17 00:00:00 2001 From: huzzy Date: Sun, 29 Mar 2026 23:53:24 +0600 Subject: [PATCH 4/4] Remove localip command --- README.md | 1 - common-functions | 12 ------------ subcommands/localip | 22 ---------------------- tests/service_localip.bats | 32 -------------------------------- 4 files changed, 67 deletions(-) delete mode 100755 subcommands/localip delete mode 100644 tests/service_localip.bats diff --git a/README.md b/README.md index efb537dc..19723b12 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,6 @@ postgres:link [--link-flags...] # link the postgres service t postgres:linked # check if the postgres service is linked to an app postgres:links # list all apps linked to the postgres service postgres:list # list all postgres services -postgres:localip # print the local docker network ip for a running service postgres:logs [-t|--tail] # print the most recent log(s) for this service postgres:pause # pause a running postgres service postgres:promote # promote service as DATABASE_URL in diff --git a/common-functions b/common-functions index 8991194c..6196cfde 100755 --- a/common-functions +++ b/common-functions @@ -107,18 +107,6 @@ get_container_ip() { echo "$IP_ADDRESS" | awk 'NF { print; exit }' } -service_local_ip() { - declare desc="retrieve the current local ip for a service container" - declare SERVICE="$1" - local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" - local ID="$(cat "$SERVICE_ROOT/ID")" - - "$DOCKER_BIN" container inspect "$ID" >/dev/null 2>&1 || dokku_log_fail "Service container does not exist" - is_container_status "$ID" "Running" || dokku_log_fail "Service container is not running" - - get_container_ip "$ID" -} - get_database_name() { declare desc="retrieve a sanitized database name" declare SERVICE="$1" diff --git a/subcommands/localip b/subcommands/localip deleted file mode 100755 index 895a1ab7..00000000 --- a/subcommands/localip +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env bash -source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/config" -set -eo pipefail -[[ $DOKKU_TRACE ]] && set -x -source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" -source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/functions" - -service-localip-cmd() { - #E print the local docker network ip for a running service: - #E dokku $PLUGIN_COMMAND_PREFIX:localip lollipop - #A service, service to run command against - declare desc="print the local docker network ip for a running service" - local cmd="$PLUGIN_COMMAND_PREFIX:localip" argv=("$@") - [[ ${argv[0]} == "$cmd" ]] && shift 1 - declare SERVICE="$1" - - [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a valid name for the service" - verify_service_name "$SERVICE" - service_local_ip "$SERVICE" -} - -service-localip-cmd "$@" diff --git a/tests/service_localip.bats b/tests/service_localip.bats deleted file mode 100644 index e3e216cd..00000000 --- a/tests/service_localip.bats +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env bats -load test_helper - -setup() { - dokku "$PLUGIN_COMMAND_PREFIX:create" l -} - -teardown() { - dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l -} - -@test "($PLUGIN_COMMAND_PREFIX:localip) error when there are no arguments" { - run dokku "$PLUGIN_COMMAND_PREFIX:localip" - assert_contains "${lines[*]}" "Please specify a valid name for the service" -} - -@test "($PLUGIN_COMMAND_PREFIX:localip) error when service does not exist" { - run dokku "$PLUGIN_COMMAND_PREFIX:localip" not_existing_service - assert_contains "${lines[*]}" "service not_existing_service does not exist" -} - -@test "($PLUGIN_COMMAND_PREFIX:localip) success" { - local expected_ip - expected_ip="$(docker inspect "dokku.$PLUGIN_COMMAND_PREFIX.l" -f '{{ .NetworkSettings.IPAddress }}')" - if [[ -z "$expected_ip" ]]; then - expected_ip="$(docker inspect "dokku.$PLUGIN_COMMAND_PREFIX.l" -f '{{range .NetworkSettings.Networks}}{{println .IPAddress}}{{end}}' | awk 'NF { print; exit }')" - fi - - run dokku "$PLUGIN_COMMAND_PREFIX:localip" l - assert_success - assert_output "$expected_ip" -}