add docker-compose example
[osm/vim-emu.git] / utils / docker / entrypoint_docker-compose.sh
1 #! /bin/bash -e
2
3 # docker stop trap signals
4 # https://medium.com/@gchudnov/trapping-signals-in-docker-containers-7a57fdda7d86#.5d6q01x7q
5
6 pid=0
7 command=""
8 term_recvd=0
9
10 # send SIGTERM also to the executed command in the docker container (the containernet topology)
11 # SIGTERM-handler
12 function term_handler() {
13 echo $command
14 pid=$(pgrep -f "$command" | sed -n 1p)
15
16 pid="$!"
17 # avoid that the process triggers its own handler by sending sigterm
18 if [ $pid -ne 0 ] && [ $term_recvd -eq 0 ]; then
19 echo "sigterm received"
20 echo $pid
21 term_recvd=1
22 kill -SIGTERM "$pid"
23 fi
24
25 wait "$pid"
26
27 # do some manual cleanup
28 # remove all containers started by son-emu
29 docker ps -a -q --filter="name=mn.*" | xargs -r docker rm -f
30 # cleanup remaining mininet
31 mn -c
32
33 sleep 5
34 exit 143; # 128 + 15 -- SIGTERM
35 }
36
37 # setup handlers
38 # on callback, kill the last background process, which is `tail -f /dev/null` and execute the specified handler
39 trap 'term_handler' SIGTERM
40
41
42 service openvswitch-switch start
43
44 if [ ! -S /var/run/docker.sock ]; then
45 echo 'Error: the Docker socket file "/var/run/docker.sock" was not found. It should be mounted as a volume.'
46 exit 1
47 fi
48
49 # this cannot be done from the Dockerfile since we have the socket not mounted during build
50 echo 'Pulling the "ubuntu:trusty" image ... please wait'
51 docker pull 'ubuntu:trusty'
52
53 echo "Welcome to Containernet running within a Docker container ..."
54
55 if [[ $# -eq 0 ]]; then
56 exec /bin/bash
57 else
58 #remember command to send it also the SIGTERM via the handler
59 command=$*
60 echo $command
61 exec $* &
62 # wait indefinetely
63 while true
64 do
65 sleep 1
66 done
67 echo "done"
68 fi