This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
_dockerTaskGeneric.cmd
99 lines (85 loc) · 2.25 KB
/
_dockerTaskGeneric.cmd
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
@echo off
set imageName="<%= imageName %>"
set containerPort=<%= portNumber %>
set publicPort=<%= portNumber %>
if /I "%1" == "compose" (
call :compose
goto :eof
)
if /I "%1" == "build" (
call :buildImage
goto :eof
)
if /I "%1" == "run" (
call :runContainer
goto :eof
)
if /I "%1" == "clean" (
call :cleanAll
goto :eof
)
if /I "%1" == "buildrun" (
call :buildImage
call :runContainer
goto :eof
)
REM By default, show usage.
call :showUsage
goto :eof
REM Kills all running containers of an image and then removes them.
:cleanAll
REM List all running containers that use %imageName%, kill them and then remove them.
FOR /F "tokens=1,2" %%a IN ('docker ps -a') DO (
if "%%b" == %imageName% (
docker kill %%a
docker rm %%a
)
)
goto :eof
REM Builds the Docker image.
:buildImage
echo Building the image %imageName%.
docker build -t %imageName% .
goto :eof
REM Runs docker-compose.
:compose
echo Composing.
docker-compose up -d
goto :eof
REM Runs the container.
:runContainer
REM Check if container is already running, stop it and run a new one.
FOR /F "tokens=1,2" %%a IN ('docker ps') DO (
if "%%b" == %imageName% (
docker kill %%a
)
)
REM Create a container from the image.
<%= containerRunCommand %>
REM Get the active machine name.
FOR /F %%i IN (' "docker-machine active" ') do (
set dockerHostName=%%i
)
REM Open the site.
FOR /F %%i IN (' "docker-machine ip %dockerHostName%" ') do (
set ipValue=%%i
)
start http://%ipValue%:%publicPort%
goto :eof
REM Shows the usage for the script.
:showUsage
echo Description:
echo Builds and runs a Docker image.
echo.
echo Options:
echo build: Builds a Docker image (%imageName%).
echo run: Runs a container based on an existing Docker image (%imageName%).
echo buildrun: Builds a Docker image and runs the container.
echo compose: Runs docker-compose.
echo clean: Removes the image %imageName% and kills all containers based on that image.
echo Example:
echo dockerTask.cmd build
echo
echo This will:
echo Build a Docker image named %imageName%.
:eof