#!/usr/bin/env bash

# This material is free software for demonstration purposes only.

# Copyright (c) 2018 Alec Clews
#
# Use of this source code is governed by an MIT license.
# See the project's LICENSE file for more information.

# Show the various states a container can be in.

# NOTE: The comands used below to start containers are slightly
# different to the examples given in the slides as a scripted
# process works a bit differently (instead of having two terminals
# open)

# Thanks to @vissree (https://github.com/vissree/) for some great ideas to improve this script



echo
echo This script will demostrate some of the lifecycle states a container can have
echo and some of the commands you can use to manage containers. Please make sure to
echo read the Docker manuals and the many tutorials for detailed and complete information.


docker container rm -f myAlpine > /dev/null 2>&1

echo
read -p 'Start a container ("docker container run -d --name myAlpine alpine")'
echo
echo '(there is some shell magic used so that the container prints digits every 5 seconds -- look at the script for details)'

docker container run -d --name myAlpine alpine  /bin/sh -c 'while echo $(( m += 1 )); do  sleep 5; done'

echo
read -p "Use the command \"docker container ls\" to see some basic information about all containers"
echo
docker container ls

echo

echo 'Look at the detailed state of our container called "myAlpine" with the command'
echo
read -p '    docker container inspect myAlpine'
echo

docker container inspect myAlpine 

echo
read -p 'and now more usefully run    "docker container inspect myAlpine|less"'
echo

docker container inspect myAlpine |less

echo

echo to make life a bit easier we can just extract the container status with the command
echo
echo "       docker container inspect --format {{.State.Status}} myAlpine"
echo
echo "which displays"
echo
docker container inspect --format {{.State.Status}} myAlpine
echo

echo "now let's stop the container with the stop command (will take some time to kill the blocked process)"

echo
read -p "     docker container stop myAlpine"
echo

docker container stop myAlpine

echo

echo the command \"docker container ls\" shows

echo

docker container ls

echo
echo the status from the inspect command is 
echo 

docker container inspect --format {{.State.Status}} myAlpine

echo
echo so why didn\'t \"docker container ls\" tell us that?
echo
read -p "try \"docker container ls -a\" instead"
echo
docker container ls -a
echo
echo We can start a \"exited\" container
echo
read -p "with the command \"docker container start myAlpine\""
echo

docker container start myAlpine

echo
read -p "and now the status is"
echo

docker container inspect --format {{.State.Status}} myAlpine

echo
echo Now the container is running again we can attach our terminal to it
echo
read -p "   docker container attach myAlpine"
echo
echo NOTE: Hit Ctrl-c when you get bored

docker container attach myAlpine

echo container status is
echo
docker container inspect --format {{.State.Status}} myAlpine

echo
echo So now the the myAlpine container is exited, but
echo how to really get rid of a container? It has to be removed
echo

read -p "    docker container rm myAlpine"
echo 

docker container rm myAlpine

echo
echo and now \"docker container ls -a\" and \"docker container inspect --format {{.State.Status}} myAlpine\" say the correct thing
echo

docker container ls -a

echo


docker container inspect --format {{.State.Status}} myAlpine

echo
echo and we are done!

echo 

echo sometimes you want to remove a container that us still running, without bothering to stop it first
echo use the \"-f\" option, as in \"docker container rm -f myAlpine\"
