The script docker-shell-helpers.sh provides a list of helper functions for making even simpler the creation, usage, and destruction of Docker containers inside a shell script.
The docker-shell-helpers library currently provides the following public functions:
- container_id()
- desc: return the Docker Id of a container
- args: container name
- container_exists()
- desc: return true if the container exists, and false otherwise
- args: container name
- container_is_running()
- desc: return true if the container is running, and false otherwise
- args: container name
- container_stop()
- desc: stop a container, if it's running
- args: container name
- container_remove()
- desc: stop and remove a container from the host node
- args: container name
- container_exec_command()
- desc: run a command (or a sequence of commands) inside a container
- args: container name
- container_property()
- desc: get a container property
- args: container name and one of the options: --id, --ipaddr, --os
- container_create()
- desc: create a container and return its name
- args: --name (or --random-name), --os and a host folder (--disk) to map
- example: container_create --random-name --os "centos:latest" --disk ~/docker-datadisk:/shared:rw
- container_start()
- desc: start a container if not running
- args: container name
- container_status_table()
- desc: return the status of a container
- args: container name of no args to list all the containers
- container_list()
- desc: return the available container name
- args: none
Here's is a simple example of how the library functions can be used.
. docker-shell-helpers.sh
cname="$(container_create --random-name --os "centos:latest")"
container_list | while read elem; do echo " - $elem"; done
#--> - centos.latest_kw05yg2Y
container_start $cname
echo "The running OS is: $(container_property --os $cname)"
#--> The running OS is: centos-7.2.1511
container_exec_command $cname "\
yum install -y autoconf automake gcc git make
cd /root
git clone https://github.com/madrisan/nagios-plugins-linux
cd nagios-plugins-linux
autoreconf
./configure && make && ./plugins/check_uptime --clock-monotonic
"
#--> ...
# uptime OK: 19 hours 18 min | uptime=1158
container_remove $cname