This set of scripts provide a framework for creating simple and reliable healthcheck scripts for Docker containers.
- mikenye/docker-healthchecks-framework
 
Clone the repository, and if desired delete files/directories not required, as-per the example below.
    # Deploy healthchecks framework
    git clone \
      --depth=1 \
      https://github.com/mikenye/docker-healthchecks-framework.git \
      /opt/healthchecks-framework \
      && \
    rm -rf \
      /opt/healthchecks-framework/.git* \
      /opt/healthchecks-framework/*.md \
      /opt/healthchecks-framework/tests \
      && \In your healthcheck script (which should use bash as an interpreter), add the following:
# Import healthchecks-framework
source /opt/healthchecks-framework/healthchecks.shYou can then call the functions outlined below.
For example, to ensure a web server is listening in your container:
#!/usr/bin/env bash
# Import healthchecks-framework
source /opt/healthchecks-framework/healthchecks.sh
# Ensure web server listening
if ! check_tcp4_socket_listening ANY 80; then
    exit 1
fi
exit 0You can then set this script in your HEALTHCHECK argument in your project's Dockerfile.
Checks that an IPv4 TCP connection is established for a specific PID.
Dependencies:
netstat- Provided by the 
net-toolspackage on Debian/Ubuntu 
- Provided by the 
 
Syntax:
check_tcp4_connection_established_pid local_ip local_port remote_ip remote_port pidArguments:
local_ip: The IPv4 address of the local side of the connection, orANY.local_port: The TCP port of the local side of the connection, orANY.remote_ip: The IPv4 address of the remote side of the connection, orANY.remote_port: The TCP port of the remote side of the connection, orANY.pid: The PID of the process, orANY.
Example 1:
Checks to ensure a connection to an external MariaDB database server is always established from a specific PID:
check_tcp4_connection_established_pid ANY ANY 1.2.3.4 3306 "$pid_of_process"Checks that an IPv4 TCP connection is established.
Dependencies:
netstat- Provided by the 
net-toolspackage on Debian/Ubuntu 
- Provided by the 
 
Syntax:
check_tcp4_connection_established local_ip local_port remote_ip remote_portArguments:
local_ip: The IPv4 address of the local side of the connection, orANY.local_port: The TCP port of the local side of the connection, orANY.remote_ip: The IPv4 address of the remote side of the connection, orANY.remote_port: The TCP port of the remote side of the connection, orANY.
Example 1:
Checks to ensure a connection to an external MariaDB database server is always established:
check_tcp4_connection_established ANY ANY 1.2.3.4 3306Example 2:
Check to ensure at least one inbound OpenVPN connecton is always established:
check_tcp4_connection_established ANY 443 ANY ANYExample 3:
Combined usage with get_ipv4 to resolve a linked container name (in the example below, the container is named "mariadb") to an IP:
check_tcp4_connection_established ANY ANY $(get_ipv4 mariadb) 3306Checks that an IPv4 UDP connection is established for a specific PID.
Dependencies:
netstat- Provided by the 
net-toolspackage on Debian/Ubuntu 
- Provided by the 
 
Syntax:
check_udp4_connection_established_pid local_ip local_port remote_ip remote_port pidArguments:
local_ip: The IPv4 address of the local side of the connection, orANY.local_port: The UDP port of the local side of the connection, orANY.remote_ip: The IPv4 address of the remote side of the connection, orANY.remote_port: The UDP port of the remote side of the connection, orANY.pid: The PID of the process, orANY.
Example 1:
Checks to ensure a connection to an external RTP server is always established for a specific PID:
check_udp4_connection_established_pid ANY ANY 1.2.3.4 5234 "$process_pid"Checks that an IPv4 UDP connection is established.
Dependencies:
netstat- Provided by the 
net-toolspackage on Debian/Ubuntu 
- Provided by the 
 
Syntax:
check_udp4_connection_established local_ip local_port remote_ip remote_portArguments:
local_ip: The IPv4 address of the local side of the connection, orANY.local_port: The UDP port of the local side of the connection, orANY.remote_ip: The IPv4 address of the remote side of the connection, orANY.remote_port: The UDP port of the remote side of the connection, orANY.
Example 1:
Checks to ensure a connection to an external RTP server is always established:
check_udp4_connection_established ANY ANY 1.2.3.4 5234Example 2:
Combined usage with get_ipv4 to resolve a linked container name (in the example below, the container is named "rtmpserver") to an IP:
check_udp4_connection_established ANY ANY $(get_ipv4 rtmpserver) 5234Checks that an IPv4 TCP socket is listening for a specific PID.
Dependencies:
netstat- Provided by the 
net-toolspackage on Debian/Ubuntu 
- Provided by the 
 
Syntax:
check_tcp4_socket_listening_pid local_ip local_port pidArguments:
local_ip: The local IPv4 address the service is listening on, orANY.local_port: The local TCP port the service is listening on, orANY.pid: The PID of the process, orANY.
Example 1:
Checks to ensure a web server is always listening on 0.0.0.0:80:
check_tcp4_socket_listening_pid 0.0.0.0 80 "$process_pid"Checks that an IPv4 TCP socket is listening.
Dependencies:
netstat- Provided by the 
net-toolspackage on Debian/Ubuntu 
- Provided by the 
 
Syntax:
check_tcp4_socket_listening local_ip local_portArguments:
local_ip: The local IPv4 address the service is listening on, orANY.local_port: The local TCP port the service is listening on, orANY.
Example 1:
Checks to ensure a web server is always listening on 0.0.0.0:80:
check_tcp4_socket_listening 0.0.0.0 80Example 2:
Check to ensure a database server is always listening on 127.0.0.1:3306:
check_tcp4_socket_listening 127.0.0.1 3306Checks that an IPv4 UDP socket is listening.
Dependencies:
netstat- Provided by the 
net-toolspackage on Debian/Ubuntu 
- Provided by the 
 
Syntax:
check_udp4_socket_listening local_ip local_portArguments:
local_ip: The local IPv4 address the service is listening on, orANY.local_port: The local UDP port the service is listening on, orANY.
Example 1:
Checks to ensure an RTP server is always listening on 0.0.0.0:5234:
check_udp4_socket_listening 0.0.0.0 5234Checks for abnormal service deaths for s6-overlay services.
Dependencies:
s6-overlay
Syntax:
check_s6_service_abnormal_death_tally service_name service_rootArguments:
service_name: The name of the s6 service, orALLservice_root: If the root directory is something other than/run/s6/services, otherwise leave unset
Example 1:
Check for abnormal death count for all services in a container:
check_s6_service_abnormal_death_tally ALLResolves a host/container to an IPv4 address.
Dependencies:
One of the following:
dig- Provided by the 
dnsutilspackage on Debian/Ubuntu 
- Provided by the 
 busybox- Provided by the 
busyboxpackage on Debian/Ubuntu 
- Provided by the 
 s6-dnsip4- Provided by s6-overlay
 
Syntax:
get_ipv4 hostArguments:
host: Hostname, container name, FQDN or IPv4 address.
Example 1:
Combined usage with check_tcp4_connection_established to resolve a linked container name (in the example below, the container is named "mariadb") to an IP:
check_tcp4_connection_established ANY ANY $(get_ipv4 mariadb) 3306