├── .gitignore ├── LICENSE ├── README.md ├── commands ├── install ├── post-delete ├── pre-build-buildpack ├── pre-build-dockerfile ├── pre-deploy ├── pre-release-buildpack └── pre-release-dockerfile /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Michael Yoo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | The better Redis plugin for Dokku 2 | --------------------------------- 3 | 4 | This is a Redis plugin for the Dokku Project, a self hosted PaaS in around 100 lines of code. You can find the project here: https://github.com/progrium/dokku 5 | 6 | This plugin is a fork of another Dokku Redis plugin by **Vlorent Viel**, which you can find here: https://github.com/luxifer/dokku-redis-plugin 7 | 8 | Installation 9 | ------------ 10 | The current latest version of Dokku Redis Plugin is v1.0.2, which was tested on Dokku v0.3.26. 11 | 12 | You can also see the Release tab for current and previous releases. Please be aware that while I strive to provide maximum reliability, I do not always have time to test releases so use at your own caution :) 13 | 14 | ``` 15 | $ cd /var/lib/dokku/plugins 16 | $ sudo git clone --branch v1.0.2 --depth 1 https://github.com/sekjun9878/dokku-redis-plugin redis 17 | $ sudo dokku plugins-install 18 | ``` 19 | 20 | Note that you might need to use su (not sudo) in some cases due to permissions. 21 | 22 | Commands 23 | -------- 24 | ``` 25 | $ sudo dokku help 26 | redis:enable Enable Redis for an app for next build 27 | redis:destroy Destroy Redis container and volume of an app 28 | ``` 29 | 30 | Simple usage 31 | ------------ 32 | This Redis plugin is a bit different than other Redis plugins in that this uses pure Docker links, instead of publicising the port and using internal IPs therefore it is a lot more reliable (e.g. no internal IP changes after a redeploy.) 33 | 34 | Also with this plugin, the Redis instance is managed for you. Unlike other plugins, you do not have to start your Redis server before you deploy. You simply have to enable Redis for an app, and the plugin will handle the start-up of the instance automatically. 35 | 36 | Usage of Docker Links also means that the details of the Redis instance is provided directly inside environment variables. 37 | 38 | **How to connect to Redis inside your app:** 39 | The following environment variables are provided for you to connect to the Redis instance: 40 | ``` 41 | # Default Docker Link variables 42 | REDIS_NAME=//redis 43 | REDIS_PORT=tcp://:6379 44 | REDIS_PORT_6379_TCP=tcp://:6379 45 | REDIS_PORT_6379_TCP_PROTO=tcp 46 | REDIS_PORT_6379_TCP_PORT=6379 47 | REDIS_PORT_6379_TCP_ADDR= 48 | 49 | # Injected by this plugin 50 | REDIS_URL=redis://redis:6379 # For devs: This is hardcoded but does not matter anyway. 51 | ``` 52 | Here is an example code for PHP, but for other languages accessing your environment variable should be straight forward. 53 | 54 | Example code for PHP using https://github.com/phpredis/phpredis: 55 | ``` 56 | $app->redis = new Redis; 57 | $app->redis->connect($_ENV["REDIS_PORT_6379_TCP_ADDR"], $_ENV["REDIS_PORT_6379_TCP_PORT"], 2); 58 | ``` 59 | 60 | `REDIS_URL` is also provided for when your library supports a simpler syntax such as ones used on Heroku. 61 | 62 | Example simplified code for Ruby using https://github.com/redis/redis-rb using `REDIS_URL`: 63 | ``` 64 | require "redis" 65 | 66 | redis = Redis.new 67 | ``` 68 | 69 | You can find more information about Docker Links here: https://docs.docker.com/userguide/dockerlinks/#environment-variables 70 | 71 | **Enable Redis:** 72 | To enable redis, an app must already exist with the same name. If you have not deployed yet, you can create a new blank app by doing: 73 | ``` 74 | # dokku apps:create example-org # Server side 75 | $ ssh dokku@server apps:create example-org # Client side 76 | 77 | Creating example-org... done 78 | ``` 79 | Now enable Redis: 80 | ``` 81 | # dokku redis:enable example-org # Server side 82 | $ ssh dokku@server redis:enable example-org # Client side 83 | 84 | -----> Enabling Redis for example-org... 85 | =====> Redis enabled for example-org 86 | Redis will be started on next build / deploy 87 | ``` 88 | 89 | Deploy your app with the same name (client side): 90 | Note: Output may be different depending on plugin version and environment. 91 | ``` 92 | $ git remote add dokku git@server:example-org 93 | $ git push dokku master 94 | Counting objects: 5, done. 95 | Delta compression using up to 8 threads. 96 | Compressing objects: 100% (3/3), done. 97 | Writing objects: 100% (5/5), 422 bytes | 0 bytes/s, done. 98 | Total 5 (delta 0), reused 0 (delta 0) 99 | remote: -----> Cleaning up... 100 | remote: -----> Building test from buildstep... 101 | remote: -----> Adding BUILD_ENV to build environment... 102 | remote: -----> PHP app detected 103 | 104 | ... blah blah blah ... 105 | 106 | remote: -----> Releasing example-org... 107 | remote: -----> Starting Redis for example-org... 108 | remote: -----> Creating new volume 109 | remote: -----> Starting Redis container... 110 | remote: -----> Creating /home/dokku/example-org/LINK 111 | remote: -----> Linking redis-example-org:redis with example-org 112 | remote: -----> Redis container linked: example-org -> redis-example-org 113 | remote: =====> Redis started: 114 | remote: Application: example-org 115 | remote: Redis: redis-example-org 116 | remote: -----> Deploying example-org... 117 | remote: -----> Running pre-flight checks 118 | 119 | ... blah blah blah ... 120 | 121 | remote: -----> Running nginx-pre-reload 122 | remote: Reloading nginx 123 | remote: =====> Application deployed: 124 | remote: http://example-org.server 125 | ``` 126 | 127 | 128 | Advanced usage 129 | -------------- 130 | 131 | **Enable Redis for a application.** This will not take effect until the next rebuild / redeploy. Example is provided above in Simple usage. 132 | ``` 133 | dokku redis:enable example-org 134 | ``` 135 | 136 | **Destroy Redis for an application.** This completely destroys Redis for an application, **including all data**. Docker Links and Redis container will all be removed. This will prompt you for a verification. Please use carefully. 137 | ``` 138 | dokku redis:destroy example-org 139 | ``` 140 | 141 | License 142 | -------------- 143 | ``` 144 | The MIT License (MIT) 145 | 146 | Copyright (c) 2015 Michael Yoo 147 | 148 | Permission is hereby granted, free of charge, to any person obtaining a copy 149 | of this software and associated documentation files (the "Software"), to deal 150 | in the Software without restriction, including without limitation the rights 151 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 152 | copies of the Software, and to permit persons to whom the Software is 153 | furnished to do so, subject to the following conditions: 154 | 155 | The above copyright notice and this permission notice shall be included in 156 | all copies or substantial portions of the Software. 157 | 158 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 159 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 160 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 161 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 162 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 163 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 164 | THE SOFTWARE. 165 | ``` 166 | -------------------------------------------------------------------------------- /commands: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | source "$PLUGIN_PATH/common/functions" 3 | # Reset bash mode from importing the functions 4 | set +exo pipefail 5 | # Fail on equation failure 6 | set -e 7 | 8 | # Check if name is specified 9 | if [[ $1 == redis:* ]]; then 10 | if [[ -z $2 ]]; then 11 | dokku_log_fail "You must specify an app name" 12 | else 13 | APP="$2" 14 | fi 15 | fi 16 | 17 | REDIS_CONTAINER_NAME="redis-$APP" 18 | REDIS_FLAG_PERSISTENCE_FILE="$DOKKU_ROOT/$APP/REDIS" 19 | REDIS_FLAG_LINK_FILE="$DOKKU_ROOT/$APP/LINK" 20 | REDIS_VOLUME_DIR="$DOKKU_ROOT/.redis/volume-$APP" 21 | REDIS_CONTAINER_ID=$(docker ps | grep "$REDIS_CONTAINER_NAME" | awk '{print $1}') 22 | 23 | case "$1" in 24 | 25 | redis:enable) 26 | verify_app_name $APP 27 | 28 | dokku_log_info1 "Enabling Redis for $APP..." 29 | 30 | touch $REDIS_FLAG_PERSISTENCE_FILE 31 | 32 | dokku_log_info2 "Redis enabled for $APP" 33 | dokku_log_verbose "Redis will be started on next build / deploy" 34 | ;; 35 | 36 | redis:destroy) 37 | if [[ ! -f $REDIS_FLAG_PERSISTENCE_FILE ]] && [[ ! -f $REDIS_FLAG_LINK_FILE ]] && [[ ! -d $REDIS_VOLUME_DIR ]] && [[ -z $REDIS_CONTAINER_ID ]] && [[ -z $(dokku config:get $APP REDIS_URL) ]]; then 38 | dokku_log_info2 "No Redis instance / leftovers found for $APP" 39 | exit 0 40 | fi 41 | 42 | if [[ -z "$2" ]]; then # <-- If not called from event 43 | confirmed=0 # If this is called from event, cannot get input 44 | while [[ $confirmed -eq 0 ]] 45 | do 46 | dokku_log_warn "WARNING: Potentially Destructive Action" 47 | dokku_log_warn "This command will destroy the Redis instance for" 48 | dokku_log_warn "$APP (including all volumes and data)." 49 | dokku_log_warn "To proceed, type \"$APP\"" 50 | echo 51 | 52 | read -p "> " app_name_verification 53 | if [[ "$app_name_verification" != "$APP" ]]; then 54 | dokku_log_warn "Confirmation did not match." 55 | echo 56 | else 57 | confirmed=1 58 | fi 59 | done 60 | fi 61 | 62 | dokku_log_info1 "Destroying Redis for $APP..." 63 | 64 | # Disable Redis autostart 65 | [[ -f "$REDIS_FLAG_PERSISTENCE_FILE" ]] && rm "$REDIS_FLAG_PERSISTENCE_FILE" 66 | 67 | # Stop the container 68 | if [[ ! -z $REDIS_CONTAINER_ID ]]; then 69 | docker stop $REDIS_CONTAINER_ID > /dev/null 70 | fi 71 | 72 | # Remove docker link 73 | if [[ -f $REDIS_FLAG_LINK_FILE ]]; then 74 | dokku link:delete $APP $REDIS_CONTAINER_NAME redis 75 | fi 76 | 77 | # Remove the container 78 | if [[ ! -z $REDIS_CONTAINER_ID ]]; then 79 | docker rm $REDIS_CONTAINER_ID > /dev/null 80 | fi 81 | 82 | # Remove persistent volume 83 | if [[ -d $REDIS_VOLUME_DIR ]]; then 84 | rm -rf $REDIS_VOLUME_DIR 85 | fi 86 | 87 | # Remove Redis autoconfiguration variable 88 | dokku config:unset-norestart "$APP" "REDIS_URL" 89 | 90 | dokku_log_info2 "Redis instance / volume destroyed:" 91 | dokku_log_verbose "Application: $APP" 92 | ;; 93 | 94 | help) 95 | cat && cat<, Enable Redis for an app for next build 97 | redis:destroy , Destroy Redis container and volume of an app 98 | EOF 99 | ;; 100 | 101 | *) 102 | exit $DOKKU_NOT_IMPLEMENTED_EXIT 103 | ;; 104 | 105 | esac 106 | -------------------------------------------------------------------------------- /install: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | source "$PLUGIN_PATH/common/functions" 3 | # Reset bash mode from importing the functions 4 | set +exo pipefail 5 | # Fail on equation failure 6 | set -e 7 | 8 | # Build redis-server docker image 9 | docker pull redis:latest 10 | -------------------------------------------------------------------------------- /post-delete: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | source "$PLUGIN_PATH/common/functions" 3 | # Reset bash mode from importing the functions 4 | set +exo pipefail 5 | # Fail on equation failure 6 | set -e 7 | 8 | APP="$1" 9 | 10 | dokku redis:destroy $APP 1 # This indicates that this is being called from event 11 | -------------------------------------------------------------------------------- /pre-build-buildpack: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | source "$PLUGIN_PATH/common/functions" 3 | # Reset bash mode from importing the functions 4 | set +exo pipefail 5 | # Fail on equation failure 6 | set -e 7 | 8 | APP="$1" 9 | REDIS_FLAG_PERSISTENCE_FILE="$DOKKU_ROOT/$APP/REDIS" 10 | REDIS_CONTAINER_NAME="redis-$APP" 11 | 12 | dokku docker-options:remove "$APP" deploy,run "--link $REDIS_CONTAINER_NAME:redis" 13 | -------------------------------------------------------------------------------- /pre-build-dockerfile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | source "$PLUGIN_PATH/common/functions" 3 | # Reset bash mode from importing the functions 4 | set +exo pipefail 5 | # Fail on equation failure 6 | set -e 7 | 8 | APP="$1" 9 | REDIS_FLAG_PERSISTENCE_FILE="$DOKKU_ROOT/$APP/REDIS" 10 | REDIS_CONTAINER_NAME="redis-$APP" 11 | 12 | dokku docker-options:remove "$APP" deploy,run "--link $REDIS_CONTAINER_NAME:redis" 13 | -------------------------------------------------------------------------------- /pre-deploy: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | source "$PLUGIN_PATH/common/functions" 3 | # Reset bash mode from importing the functions 4 | set +exo pipefail 5 | # Fail on equation failure 6 | set -e 7 | 8 | APP="$1" 9 | REDIS_FLAG_PERSISTENCE_FILE="$DOKKU_ROOT/$APP/REDIS" 10 | REDIS_CONTAINER_NAME="redis-$APP" 11 | 12 | # Only start Redis if it is enabled 13 | if [[ -f $REDIS_FLAG_PERSISTENCE_FILE ]]; then 14 | dokku_log_info1 "Starting Redis for $APP..." 15 | 16 | # Check if Redis container is installed 17 | IMAGE=$(docker images | grep "redis" | awk '{print $3}') 18 | if [[ -z $IMAGE ]]; then 19 | dokku_log_fail "Redis image not found! Did you run 'dokku plugins-install'?" 20 | fi 21 | 22 | # Check if Redis is already running 23 | ID=$(docker ps | grep "$REDIS_CONTAINER_NAME" | awk '{print $1}') 24 | if [[ ! -z $ID ]]; then 25 | dokku_log_verbose "Redis container already running: $REDIS_CONTAINER_NAME" 26 | exit 0 27 | fi 28 | 29 | # Remove old stopped container with the same name, if it exists 30 | ID=$(docker ps -a | grep "$REDIS_CONTAINER_NAME" | awk '{print $1}') 31 | if [[ ! -z $ID ]]; then 32 | ID=$(docker rm $ID) 33 | dokku_log_info1 "Removed stale Redis container $ID" 34 | fi 35 | 36 | # Check if an existing DB volume exists 37 | HOST_DIR="$DOKKU_ROOT/.redis/volume-$APP" 38 | if [[ -d $HOST_DIR ]]; then 39 | dokku_log_info1 "Reusing volume-$APP volume" 40 | else 41 | dokku_log_info1 "Creating new volume" 42 | mkdir -p $HOST_DIR 43 | fi 44 | VOLUME="$HOST_DIR:/data" 45 | 46 | # Launch container 47 | dokku_log_info1 "Starting Redis container..." 48 | ID=$(docker run -v $VOLUME --name $REDIS_CONTAINER_NAME -d redis redis-server --appendonly yes) 49 | 50 | dokku_log_info2 "Redis started:" 51 | dokku_log_verbose "Application: $APP" 52 | dokku_log_verbose "Redis: $REDIS_CONTAINER_NAME" 53 | fi 54 | -------------------------------------------------------------------------------- /pre-release-buildpack: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | source "$PLUGIN_PATH/common/functions" 3 | # Reset bash mode from importing the functions 4 | set +exo pipefail 5 | # Fail on equation failure 6 | set -e 7 | 8 | APP="$1" 9 | REDIS_FLAG_PERSISTENCE_FILE="$DOKKU_ROOT/$APP/REDIS" 10 | REDIS_CONTAINER_NAME="redis-$APP" 11 | 12 | # Only start Redis if it is enabled 13 | if [[ -f $REDIS_FLAG_PERSISTENCE_FILE ]]; then 14 | dokku_log_info1 "Preparing Redis for $APP..." 15 | 16 | # Link to app 17 | dokku docker-options:add "$APP" deploy,run "--link $REDIS_CONTAINER_NAME:redis" 18 | dokku_log_info1 "Redis container linked: $APP -> $REDIS_CONTAINER_NAME" 19 | 20 | # Create REDIS_URL env variable for library / connector autoconfiguration 21 | dokku_log_info1 "Creating Redis autoconfiguration environment variable" 22 | dokku config:set-norestart "$APP" "REDIS_URL=redis://redis:6379" 23 | fi 24 | -------------------------------------------------------------------------------- /pre-release-dockerfile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | source "$PLUGIN_PATH/common/functions" 3 | # Reset bash mode from importing the functions 4 | set +exo pipefail 5 | # Fail on equation failure 6 | set -e 7 | 8 | APP="$1" 9 | REDIS_FLAG_PERSISTENCE_FILE="$DOKKU_ROOT/$APP/REDIS" 10 | REDIS_CONTAINER_NAME="redis-$APP" 11 | 12 | # Only start Redis if it is enabled 13 | if [[ -f $REDIS_FLAG_PERSISTENCE_FILE ]]; then 14 | dokku_log_info1 "Preparing Redis for $APP..." 15 | 16 | # Link to app 17 | dokku docker-options:add "$APP" deploy,run "--link $REDIS_CONTAINER_NAME:redis" 18 | dokku_log_info1 "Redis container linked: $APP -> $REDIS_CONTAINER_NAME" 19 | 20 | # Create REDIS_URL env variable for library / connector autoconfiguration 21 | dokku_log_info1 "Creating Redis autoconfiguration environment variable" 22 | dokku config:set-norestart "$APP" "REDIS_URL=redis://redis:6379" 23 | fi 24 | --------------------------------------------------------------------------------