From 8597b692eee6190a22b7c6d13e1de06cc541bb4c Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 2 May 2016 00:02:37 -0400 Subject: [PATCH 1/3] Add the ability to run containers in detached mode --- docs/deployment/one-off-processes.md | 7 +++++++ plugins/00_dokku-standard/subcommands/run | 11 +++++++++-- plugins/common/functions | 3 +++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/docs/deployment/one-off-processes.md b/docs/deployment/one-off-processes.md index 29c24a09e..1ec1dc469 100644 --- a/docs/deployment/one-off-processes.md +++ b/docs/deployment/one-off-processes.md @@ -32,6 +32,13 @@ dokku --rm-container run node-js-app ls -lah dokku --rm run node-js-app ls -lah ``` +Finally, you may wish to run a container in "detached" mode via the `--detach` dokku flag. Note that you may not use the `--rm-container` or `--rm` flags when running containers in detached mode, and attempting to do so will result in the `--detach` flag being ignored. + +```shell +dokku --detach run node-js-app ls -lah +# returns the ID of the new container +``` + ### Using `run` for cron tasks You can always use a one-off container to run an application task: diff --git a/plugins/00_dokku-standard/subcommands/run b/plugins/00_dokku-standard/subcommands/run index ff2ca1588..faebb4754 100755 --- a/plugins/00_dokku-standard/subcommands/run +++ b/plugins/00_dokku-standard/subcommands/run @@ -20,8 +20,15 @@ dokku_run_cmd() { local DOCKER_ARGS=$(: | plugn trigger docker-args-run "$APP" "$IMAGE_TAG") [[ "$DOKKU_TRACE" ]] && local DOCKER_ARGS+=" -e TRACE=true " - [[ "$DOKKU_RM_CONTAINER" ]] && local DOKKU_RUN_OPTS="--rm" - has_tty && local DOKKU_RUN_OPTS+=" -i -t" + + local DOKKU_RUN_OPTS="" + if [[ "$DOKKU_RM_CONTAINER" ]]; then + DOKKU_RUN_OPTS+=" --rm" + elif [[ "$DOKKU_DETACH_CONTAINER" ]]; then + DOKKU_RUN_OPTS+=" --detach" + fi + + has_tty && DOKKU_RUN_OPTS+=" -i -t" is_image_herokuish_based "$IMAGE" && local EXEC_CMD="/exec" # shellcheck disable=SC2086 docker run $DOKKU_GLOBAL_RUN_ARGS $DOKKU_RUN_OPTS $DOCKER_ARGS $IMAGE $EXEC_CMD "$@" diff --git a/plugins/common/functions b/plugins/common/functions index b1ebcee79..9852751d9 100755 --- a/plugins/common/functions +++ b/plugins/common/functions @@ -228,6 +228,9 @@ parse_args() { --quiet) export DOKKU_QUIET_OUTPUT=1 ;; + --detach) + export DOKKU_DETACH_CONTAINER=1 + ;; --trace) export DOKKU_TRACE=1 ;; From 565cd5c0e83c64b75396a5b450c495b1d6dce2c8 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 2 May 2016 12:37:49 -0400 Subject: [PATCH 2/3] Update one-off-processes.md [ci skip] --- docs/deployment/one-off-processes.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/deployment/one-off-processes.md b/docs/deployment/one-off-processes.md index 1ec1dc469..befe4a028 100644 --- a/docs/deployment/one-off-processes.md +++ b/docs/deployment/one-off-processes.md @@ -32,13 +32,15 @@ dokku --rm-container run node-js-app ls -lah dokku --rm run node-js-app ls -lah ``` -Finally, you may wish to run a container in "detached" mode via the `--detach` dokku flag. Note that you may not use the `--rm-container` or `--rm` flags when running containers in detached mode, and attempting to do so will result in the `--detach` flag being ignored. +Finally, you may wish to run a container in "detached" mode via the `--detach` dokku flag. Running a process in detached mode will immediately return a `CONTAINER_ID`. It is up to the user to then further manage this container in whatever manner they see fit, as dokku will *not* automatically terminate the container. ```shell dokku --detach run node-js-app ls -lah # returns the ID of the new container ``` +> Note that you may not use the `--rm-container` or `--rm` flags when running containers in detached mode, and attempting to do so will result in the `--detach` flag being ignored. + ### Using `run` for cron tasks You can always use a one-off container to run an application task: From 1bcd76ffe9be6187137bcc5be08cb8e191a11b0e Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 2 May 2016 21:11:06 -0400 Subject: [PATCH 3/3] Add detached run tests --- tests/unit/30_core_1.bats | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/unit/30_core_1.bats b/tests/unit/30_core_1.bats index e5b007afb..80d739025 100644 --- a/tests/unit/30_core_1.bats +++ b/tests/unit/30_core_1.bats @@ -112,6 +112,27 @@ build_nginx_config() { assert_success } +@test "(core) run (detached)" { + deploy_app + + RANDOM_RUN_CID="$(dokku --detach run $TEST_APP bash)" + run bash -c "docker inspect $RANDOM_RUN_CID" + echo "output: "$output + echo "status: "$status + assert_success + + run bash -c "docker stop $RANDOM_RUN_CID" + echo "output: "$output + echo "status: "$status + assert_success + + run dokku cleanup + echo "output: "$output + echo "status: "$status + assert_success + sleep 5 # wait for dokku cleanup to happen in the background +} + @test "(core) run (with tty)" { deploy_app run /bin/bash -c "dokku run $TEST_APP ls /app/package.json"