From da2b54823b85043f28ed6c966e62f09d20817165 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 21 Mar 2021 21:43:53 -0400 Subject: [PATCH] feat: add ability to specify a custom Dockerfile for an app --- docs/appendices/0.25.0-migration-guide.md | 4 + docs/deployment/builders/dockerfiles.md | 82 +++++++++++++++-- plugins/builder-dockerfile/builder-build | 19 +++- plugins/builder-dockerfile/commands | 15 +++ plugins/builder-dockerfile/help-functions | 33 +++++++ plugins/builder-dockerfile/internal-commands | 91 +++++++++++++++++++ plugins/builder-dockerfile/report | 6 ++ .../builder-dockerfile/subcommands/default | 6 ++ plugins/builder-dockerfile/subcommands/report | 6 ++ plugins/builder-dockerfile/subcommands/set | 30 ++++++ 10 files changed, 284 insertions(+), 8 deletions(-) create mode 100755 plugins/builder-dockerfile/commands create mode 100755 plugins/builder-dockerfile/help-functions create mode 100755 plugins/builder-dockerfile/internal-commands create mode 100755 plugins/builder-dockerfile/report create mode 100755 plugins/builder-dockerfile/subcommands/default create mode 100755 plugins/builder-dockerfile/subcommands/report create mode 100755 plugins/builder-dockerfile/subcommands/set diff --git a/docs/appendices/0.25.0-migration-guide.md b/docs/appendices/0.25.0-migration-guide.md index 6361d512d..1e75ddbf8 100644 --- a/docs/appendices/0.25.0-migration-guide.md +++ b/docs/appendices/0.25.0-migration-guide.md @@ -3,3 +3,7 @@ ## Changes - The network plugin can now set an `initial-network` for all containers on creation. This is a replacement for specifying the `--network` flag via the `docker-options` plugin. Please see the [network documentation](/docs/networking/network.md#attaching-an-app-to-a-network) for more information. + +## Deprecations + +- In previous versions of Dokku, the only way to specify a custom `Dockerfile` was to use the `docker-options` plugin to set the `--file` flag for a docker build. As of 0.25.0, the `builder-dockerfile:set` command should be used instead, as outlined in the [docs here](/docs/deployment/builders/dockerfiles.md#changingthe-dockerfile-location). Usage of the old method should be migrated to the new method. diff --git a/docs/deployment/builders/dockerfiles.md b/docs/deployment/builders/dockerfiles.md index ef52f71fc..33b3c4c69 100644 --- a/docs/deployment/builders/dockerfiles.md +++ b/docs/deployment/builders/dockerfiles.md @@ -27,6 +27,82 @@ If an application was previously deployed via buildpacks, the following commands dokku config:unset --no-restart node-js-app DOKKU_PROXY_PORT_MAP ``` +### Changing the `Dockerfile` location + +> The previous method to perform this - via `docker-options:add` - should be removed in favor of the `builder-dockerfile:set` command outlined here. + +When deploying a monorepo, it may be desirable to specify the specific path of the `Dockerfile` file to use for a given app. This can be done via the `builder-dockerfile:set` command. + +```shell +dokku builder-dockerfile:set node-js-app dockerfile-path Dockerfile2 +``` + +The default value may be set by passing an empty value for the option: + +```shell +dokku builder-dockerfile:set node-js-app dockerfile-path +``` + +The `dockerfile-path` property can also be set globally. The global default is `Dockerfile`, and the global value is used when no app-specific value is set. + +```shell +dokku builder-dockerfile:set --global dockerfile-path Dockerfile2 +``` + +The default value may be set by passing an empty value for the option. + +```shell +dokku builder-dockerfile:set --global dockerfile-path +``` + +### Displaying builder-dockerfile reports for an app + +> New as of 0.25.0 + +You can get a report about the app's storage status using the `builder-dockerfile:report` command: + +```shell +dokku app-json:report +``` + +``` +=====> node-js-app builder-dockerfile information + Builder-dockerfile computed dockerfile path: Dockerfile2 + Builder-dockerfile global dockerfile path: Dockerfile + Builder-dockerfile dockerfile path: Dockerfile2 +=====> python-sample builder-dockerfile information + Builder-dockerfile computed dockerfile path: Dockerfile + Builder-dockerfile global dockerfile path: Dockerfile + Builder-dockerfile dockerfile path: +=====> ruby-sample builder-dockerfile information + Builder-dockerfile computed dockerfile path: Dockerfile + Builder-dockerfile global dockerfile path: Dockerfile + Builder-dockerfile dockerfile path: +``` + +You can run the command for a specific app also. + +```shell +dokku builder-dockerfile:report node-js-app +``` + +``` +=====> node-js-app builder-dockerfile information + Builder-dockerfile computed dockerfile path: Dockerfile2 + Builder-dockerfile global dockerfile path: Dockerfile + Builder-dockerfile dockerfile path: Dockerfile2 +``` + +You can pass flags which will output only the value of the specific information you want. For example: + +```shell +dokku builder-dockerfile:report node-js-app --builder-dockerfile-dockerfile-path +``` + +``` +Dockerfile2 +``` + ### Build-time configuration variables For security reasons - and as per [Docker recommendations](https://github.com/docker/docker/issues/13490) - Dockerfile-based deploys have variables available only during runtime. @@ -37,12 +113,6 @@ For users that require customization in the `build` phase, you may use build arg dokku docker-options:add node-js-app build '--build-arg NODE_ENV=production' ``` -The location of the `Dockerfile` may also be specified. If the location is changed, the repository **must also** have a `Dockerfile` in the root directory in order to trigger a dockerfile-based deploy. - -```shell -dokku docker-options:add node-js-app build '--file Dockerfile.dokku' -``` - Once set, the Dockerfile usage would be as follows: ```Dockerfile diff --git a/plugins/builder-dockerfile/builder-build b/plugins/builder-dockerfile/builder-build index 7ff239b92..6a4fc60a8 100755 --- a/plugins/builder-dockerfile/builder-build +++ b/plugins/builder-dockerfile/builder-build @@ -1,7 +1,9 @@ #!/usr/bin/env bash +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_AVAILABLE_PATH/config/functions" +source "$PLUGIN_AVAILABLE_PATH/builder-dockerfile/internal-functions" set -eo pipefail [[ $DOKKU_TRACE ]] && set -x -source "$PLUGIN_AVAILABLE_PATH/config/functions" trigger-builder-dockerfile-builder-build() { declare desc="builder-dockerfile builder-build plugin trigger" @@ -19,6 +21,19 @@ trigger-builder-dockerfile-builder-build() { pushd "$SOURCECODE_WORK_DIR" &>/dev/null + local NEW_DOCKERFILE="$(fn-builder-dockerfile-computed-dockerfile-path "$APP")" + if [[ -f "$NEW_DOCKERFILE" ]]; then + if [[ "$NEW_DOCKERFILE" != "Dockerfile" ]]; then + mv "$NEW_DOCKERFILE" Dockerfile + fi + else + rm Dockerfile + fi + + if [[ ! -f Dockerfile ]]; then + dokku_log_fail "Invalid dockerfile-path specified: $dockerfile_path" + fi + # extract first port from Dockerfile dos2unix Dockerfile local DOCKERFILE_PORTS=$(get_dockerfile_exposed_ports Dockerfile) @@ -34,7 +49,7 @@ trigger-builder-dockerfile-builder-build() { declare -a ARG_ARRAY eval "ARG_ARRAY=($DOCKER_ARGS)" - "$DOCKER_BIN" image build "${DOCKER_BUILD_LABEL_ARGS[@]}" $DOKKU_GLOBAL_BUILD_ARGS "${ARG_ARRAY[@]}" "${DOKKU_DOCKER_BUILD_OPTS[@]}" -t $IMAGE . + "$DOCKER_BIN" image build --file "$dockerfile_path" "${DOCKER_BUILD_LABEL_ARGS[@]}" $DOKKU_GLOBAL_BUILD_ARGS "${ARG_ARRAY[@]}" "${DOKKU_DOCKER_BUILD_OPTS[@]}" -t $IMAGE . plugn trigger post-build-dockerfile "$APP" } diff --git a/plugins/builder-dockerfile/commands b/plugins/builder-dockerfile/commands new file mode 100755 index 000000000..8ccfa20ee --- /dev/null +++ b/plugins/builder-dockerfile/commands @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_AVAILABLE_PATH/builder-dockerfile/help-functions" + +case "$1" in + help | builder-dockerfile:help) + cmd-builder-dockerfile-help "$@" + ;; + + *) + exit "$DOKKU_NOT_IMPLEMENTED_EXIT" + ;; + +esac diff --git a/plugins/builder-dockerfile/help-functions b/plugins/builder-dockerfile/help-functions new file mode 100755 index 000000000..ec1c52e38 --- /dev/null +++ b/plugins/builder-dockerfile/help-functions @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-builder-dockerfile-help() { + declare desc="help command" + declare CMD="$1" + local plugin_name="builder-dockerfile" + local plugin_description="Manage the dockerfile builder integration for an app" + + if [[ "$CMD" == "${plugin_name}:help" ]]; then + echo -e "Usage: dokku ${plugin_name}[:COMMAND]" + echo '' + echo "$plugin_description" + echo '' + echo 'Additional commands:' + fn-help-content | sort | column -c2 -t -s, + elif [[ $(ps -o command= $PPID) == *"--all"* ]]; then + fn-help-content + else + cat <] [], Displays a builder-dockerfile report for one or more apps + builder-dockerfile:set (), Set or clear a builder-dockerfile property for an app +help_content +} diff --git a/plugins/builder-dockerfile/internal-commands b/plugins/builder-dockerfile/internal-commands new file mode 100755 index 000000000..88503617b --- /dev/null +++ b/plugins/builder-dockerfile/internal-commands @@ -0,0 +1,91 @@ +#!/usr/bin/env bash +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-builder-dockerfile-report() { + declare desc="displays a builder-dockerfile report for one or more apps" + declare cmd="builder-dockerfile:report" + [[ "$1" == "$cmd" ]] && shift 1 + declare APP="$1" INFO_FLAG="$2" + local INSTALLED_APPS=$(dokku_apps) + + if [[ -n "$APP" ]] && [[ "$APP" == --* ]]; then + INFO_FLAG="$APP" + APP="" + fi + + if [[ -z "$APP" ]] && [[ -z "$INFO_FLAG" ]]; then + INFO_FLAG="true" + fi + + if [[ -z "$APP" ]]; then + for app in $INSTALLED_APPS; do + cmd-builder-dockerfile-report-single "$app" "$INFO_FLAG" | tee || true + done + else + cmd-builder-dockerfile-report-single "$APP" "$INFO_FLAG" + fi +} + +cmd-builder-dockerfile-report-single() { + declare APP="$1" INFO_FLAG="$2" + if [[ "$INFO_FLAG" == "true" ]]; then + INFO_FLAG="" + fi + verify_app_name "$APP" + local flag_map=( + "--builder-dockerfile-computed-dockerfile-path: $(fn-builder-dockerfile-computed-dockerfile-path "$APP")" + "--builder-dockerfile-global-dockerfile-path: $(fn-builder-dockerfile-global-dockerfile-path "$APP")" + "--builder-dockerfile-dockerfile-path: $(fn-builder-dockerfile-dockerfile-path "$APP")" + ) + + if [[ -z "$INFO_FLAG" ]]; then + dokku_log_info2_quiet "${APP} builder-dockerfile information" + for flag in "${flag_map[@]}"; do + key="$(echo "${flag#--}" | cut -f1 -d' ' | tr - ' ')" + dokku_log_verbose "$(printf "%-30s %-25s" "${key^}" "${flag#*: }")" + done + else + local match=false + local value_exists=false + for flag in "${flag_map[@]}"; do + valid_flags="${valid_flags} $(echo "$flag" | cut -d':' -f1)" + if [[ "$flag" == "${INFO_FLAG}:"* ]]; then + value=${flag#*: } + size="${#value}" + if [[ "$size" -ne 0 ]]; then + echo "$value" && match=true && value_exists=true + else + match=true + fi + fi + done + [[ "$match" == "true" ]] || dokku_log_fail "Invalid flag passed, valid flags:${valid_flags}" + [[ "$value_exists" == "true" ]] || dokku_log_fail "not deployed" + fi +} + +fn-builder-dockerfile-computed-dockerfile-path() { + declare APP="$1" + + file="$(fn-builder-dockerfile-dockerfile-path "$APP")" + if [[ "$file" == "" ]]; then + file="$(fn-builder-dockerfile-global-dockerfile-path "$APP")" + fi + + echo "$file" +} + +fn-builder-dockerfile-global-dockerfile-path() { + declare APP="$1" + + fn-plugin-property-get "builder-dockerfile" "--global" "dockerfile-path" "Dockerfile" +} + +fn-builder-dockerfile-dockerfile-path() { + declare APP="$1" + + fn-plugin-property-get "builder-dockerfile" "$APP" "dockerfile-path" "" +} diff --git a/plugins/builder-dockerfile/report b/plugins/builder-dockerfile/report new file mode 100755 index 000000000..1f809ffca --- /dev/null +++ b/plugins/builder-dockerfile/report @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +source "$PLUGIN_AVAILABLE_PATH/builder-dockerfile/internal-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-builder-dockerfile-report-single "$@" diff --git a/plugins/builder-dockerfile/subcommands/default b/plugins/builder-dockerfile/subcommands/default new file mode 100755 index 000000000..dc15e270c --- /dev/null +++ b/plugins/builder-dockerfile/subcommands/default @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +source "$PLUGIN_AVAILABLE_PATH/builder-dockerfile/help-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-builder-dockerfile-help "builder-dockerfile:help" diff --git a/plugins/builder-dockerfile/subcommands/report b/plugins/builder-dockerfile/subcommands/report new file mode 100755 index 000000000..15f0f1769 --- /dev/null +++ b/plugins/builder-dockerfile/subcommands/report @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +source "$PLUGIN_AVAILABLE_PATH/builder-dockerfile/internal-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-builder-dockerfile-report "$@" diff --git a/plugins/builder-dockerfile/subcommands/set b/plugins/builder-dockerfile/subcommands/set new file mode 100755 index 000000000..d31b4f150 --- /dev/null +++ b/plugins/builder-dockerfile/subcommands/set @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-builder-dockerfile-set() { + declare desc="set or clear a builder-dockerfile property for an app" + declare cmd="builder-dockerfile:set" + [[ "$1" == "$cmd" ]] && shift 1 + declare APP="$1" KEY="$2" VALUE="$3" + local VALID_KEYS=("dockerfile-path") + [[ "$APP" == "--global" ]] || verify_app_name "$APP" + + [[ -z "$KEY" ]] && dokku_log_fail "No key specified" + + if ! fn-in-array "$KEY" "${VALID_KEYS[@]}"; then + dokku_log_fail "Invalid key specified, valid keys include: dockerfile-path" + fi + + if [[ -n "$VALUE" ]]; then + dokku_log_info2_quiet "Setting ${KEY} to ${VALUE}" + fn-plugin-property-write "builder-dockerfile" "$APP" "$KEY" "$VALUE" + else + dokku_log_info2_quiet "Unsetting ${KEY}" + fn-plugin-property-delete "builder-dockerfile" "$APP" "$KEY" + fi +} + +cmd-builder-dockerfile-set "$@"