-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcbash.bat
More file actions
53 lines (46 loc) · 1.65 KB
/
Copy pathcbash.bat
File metadata and controls
53 lines (46 loc) · 1.65 KB
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
@echo off
setlocal enabledelayedexpansion
:: Load container config from .env.container
if not exist .env.container (
echo .env.container file not found. Copy .env.container.example to .env.container and customize.
exit /b 1
)
for /f "tokens=1,2 delims==" %%a in (.env.container) do (
if "%%a"=="IMAGE_NAME" set IMAGE_NAME=%%b
if "%%a"=="CONTAINER_NAME" set CONTAINER_NAME=%%b
)
if not defined IMAGE_NAME (
echo IMAGE_NAME not set in .env.container.
exit /b 1
)
if not defined CONTAINER_NAME (
echo CONTAINER_NAME not set in .env.container.
exit /b 1
)
:: Check if container exists
docker inspect %CONTAINER_NAME% >nul 2>&1
if errorlevel 1 (
echo Container %CONTAINER_NAME% does not exist. Run rebuild.bat first.
exit /b 1
)
:: Check if running
for /f %%i in ('docker inspect -f "{{.State.Running}}" %CONTAINER_NAME% 2^>nul') do set RUNNING=%%i
if not "%RUNNING%"=="true" (
echo Starting container %CONTAINER_NAME%...
docker start %CONTAINER_NAME%
)
:: Exec into interactive bash as devuser
echo Opening shell in %CONTAINER_NAME%...
docker exec -it -u devuser %CONTAINER_NAME% bash
:: After shell exit, count remaining interactive bash processes (default to 0 if fails)
:: pgrep -x matches the process name exactly, so bs serve and other non-shell processes don't count
set COUNT=0
for /f %%a in ('docker exec %CONTAINER_NAME% pgrep -c -x bash 2^>nul') do set COUNT=%%a
:: If no more bash processes, stop the container
if "%COUNT%"=="0" (
echo No more active shells. Stopping container %CONTAINER_NAME%...
docker stop %CONTAINER_NAME%
) else (
echo %COUNT% active shell^(s^) remaining. Container stays running.
)
endlocal