├── LICENSE ├── README.md └── wait-for-healthy-container.sh /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Jordy Versmissen 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wait-for-healthy-container 2 | Simple bash script to wait for a Docker container until the health check returns `healthy` or the timeout exceeded. 3 | 4 | ## Usage 5 | 6 | Usage: `wait-for-healthy-container.sh [timeout]` 7 | 8 | ie. `wait-for-healthy-container.sh my_app_container 120` 9 | 10 | ## Description 11 | 12 | Script waits till the health check state of a container is 'healthy'. 13 | 14 | ## Prerequisites 15 | 16 | The docker container must have a **healthcheck** configured for this script to work. 17 | 18 | ### How to implement a docker container healthcheck 19 | 20 | - [Implementing in a docker-compose file](https://docs.docker.com/compose/compose-file/compose-file-v3/#healthcheck) 21 | - [Implementing in a Dockerfile](https://docs.docker.com/engine/reference/builder/#healthcheck) 22 | - [Implementing via the docker run command](https://docs.docker.com/engine/reference/run/#healthcheck) 23 | -------------------------------------------------------------------------------- /wait-for-healthy-container.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | container_name=$1 3 | shift 4 | timeout=$1 5 | 6 | default_timeout=120 7 | 8 | if [ -z ${timeout} ]; then 9 | timeout=${default_timeout} 10 | fi 11 | 12 | RETURN_HEALTHY=0 13 | RETURN_STARTING=1 14 | RETURN_UNHEALTHY=2 15 | RETURN_UNKNOWN=3 16 | RETURN_ERROR=99 17 | 18 | function usage() { 19 | echo " 20 | Usage: wait-for-healthy-container.sh [timeout] 21 | " 22 | return 23 | } 24 | 25 | function get_health_state { 26 | state=$(docker inspect -f '{{ .State.Health.Status }}' ${container_name}) 27 | return_code=$? 28 | if [ ! ${return_code} -eq 0 ]; then 29 | exit ${RETURN_ERROR} 30 | fi 31 | if [[ "${state}" == "healthy" ]]; then 32 | return ${RETURN_HEALTHY} 33 | elif [[ "${state}" == "unhealthy" ]]; then 34 | return ${RETURN_UNHEALTHY} 35 | elif [[ "${state}" == "starting" ]]; then 36 | return ${RETURN_STARTING} 37 | else 38 | return ${RETURN_UNKNOWN} 39 | fi 40 | } 41 | 42 | function wait_for() { 43 | echo "Wait for container '$container_name' to be healthy for max $timeout seconds..." 44 | for i in `seq ${timeout}`; do 45 | get_health_state 46 | state=$? 47 | if [ ${state} -eq 0 ]; then 48 | echo "Container is healthy after ${i} seconds." 49 | exit 0 50 | fi 51 | sleep 1 52 | done 53 | 54 | echo "Timeout exceeded. Health status returned: $(docker inspect -f '{{ .State.Health.Status }}' ${container_name})" 55 | exit 1 56 | } 57 | 58 | if [ -z ${container_name} ]; then 59 | usage 60 | exit 1 61 | else 62 | wait_for 63 | fi 64 | --------------------------------------------------------------------------------