-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
74 lines (63 loc) · 2.22 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
#
# Start ${IMAGE_TAG} docker container
# author: Loan Lassalle
IMAGE_TAG="php/apache"
CONTAINER_NAME="php-apache"
IP_HOST="127.0.0.1"
PORT_HOST="8080"
# Open OS default browser
function open_browser {
local os_type="${1}"
local ip_host="${2}"
local port_host="${3}"
local browser=""
case "${os_type}" in
darwin*)
browser="open";;
linux*)
browser="xdg-open";;
msys*)
browser="start";;
*)
echo "unknown: ${os_type}"
exit 0;;
esac
echo "Open OS default browser"
"${browser}" "http://${ip_host}:${port_host}/"
}
# Start container
function start {
local imageTag="${1}"
local container_name="${2}"
local port_host="${3}"
echo "Starting ${container_name} docker container"
docker build \
--force-rm \
--quiet \
--tag "${imageTag}" .
docker run \
--detach \
--name "${container_name}" \
--rm \
--publish ${port_host}:80 \
"${imageTag}"
}
# Stop container
function stop {
local container_name="${1}"
echo "Stopping ${container_name} docker container"
docker stop "${container_name}" 2>/dev/null
docker kill "${container_name}" 2>/dev/null
docker rm \
--volumes "${container_name}" 2>/dev/null
#docker stop ${container_name} 2>/dev/null && docker kill ${container_name} 2>/dev/null && docker rm --volumes ${container_name} 2>/dev/null && docker volume rm --force ${volume_name} 2>/dev/null && docker image rm --force ${image_tag} 2>/dev/null
#docker stop $(docker ps --all --quiet) 2>/dev/null && docker kill $(docker ps --all --quiet) 2>/dev/null && docker rm --volumes $(docker ps --all --quiet) 2>/dev/null && docker volume rm --force $(docker volume ls --quiet) 2>/dev/null && docker image rm --force $(docker images --all --quiet) 2>/dev/null
#docker volume prune --force 2>/dev/null && docker image prune --force 2>/dev/null
#docker rmi $(docker images -f "dangling=true" -q)
}
# Main
start "${IMAGE_TAG}" "${CONTAINER_NAME}" "${PORT_HOST}"
open_browser "${OSTYPE}" "${IP_HOST}" "${PORT_HOST}"
read -p "Press enter key to stop ${CONTAINER_NAME} docker container ..."
stop "${CONTAINER_NAME}"