Skip to content

Instantly share code, notes, and snippets.

@goliatone
Forked from the-frey/monit_block_for_docker_container
Last active December 6, 2017 18:58
Show Gist options
  • Select an option

  • Save goliatone/d65d9e42fd64fb939d92 to your computer and use it in GitHub Desktop.

Select an option

Save goliatone/d65d9e42fd64fb939d92 to your computer and use it in GitHub Desktop.
Monit Service Monitor Block for Docker Container

Tun run a command against a running app inside docker, run the docker exec command non-interactively under bash:

docker exec \
  --tty \
  --interactive \
  ${container_ID} \
  bash -c dspmq

Reaching out into your container to verify is running ok:

docker exec ${container_ID} bash -c "/opt/your_app/bin/docker_agent status"

Convert an existing docker container into a "docker run" command line.

This is useful when trying to debug containers that have been created

by orchestration tools.

Install jq: stedolan.github.io/jq/

function format_run() { cid=$1

json=$(docker inspect $cid 2>&1)

# parse container info
entrypoint=$(  echo $json | jq -r '.[0].Config.Entrypoint | join(" ")'             )
envvars=$(     echo $json | jq -r '(.[0].Config.Env | [" -e " + .[]] | join(""))'  )
image=$(       echo $json | jq -r .[0].Image                                       )
cmd=$(         echo $json | jq -r '.[0].Config.Cmd | join(" ")'                    )

echo "docker run --entrypoint $entrypoint $envvars $image $cmd"

}

# Inside main monitrc
# Check that services are running and also define start and stop commands for them.
# You will need one of these for each container
# as well as working out a restarting and/or red
eploying workflow.
check process <container-name> with pidfile /var/run/monit/<container-name>.pid
start = "/path/to/pid/script start <container-name> '<docker-arguments>'"
stop = "/path/to/pid/script stop <container-name>"
if does not exist then alert
if cpu equal 0%
and memory equal 0kB
for 5 cycles then alert
#!/bin/bash
# First, create a folder: /var/run/monit
# Then, make sure your user or group owns it: chown user:user /var/run/monit
# USAGE:
# use option one to start or stop
# use option two as the docker name
# use option three as the docker run command it follows '-d' f you are starting a container
#
# Example:
# $ ./monit_docker_wrapper.sh start my-name "-p 80:80 ubuntu true"
#
# I would then expect to find /var/run/monit/my-name-docker-container.pid
case $1 in
start)
sudo rm /var/run/monit/$2-docker-container.pid;
sudo echo $$ > /var/run/monit/$2-docker-container.pid;
sudo docker run --name $2 -d $3
;;
stop)
sudo docker stop $2
sudo rm /var/run/monit/$2-docker-container.pid;
;;
*)
echo "usage: {start|stop} [name] [docker run command]" ;;
esac
exit 0
# This is adapted from the Monit examples. License: MIT.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment