This repository was archived by the owner on Nov 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-tool.ps1
More file actions
87 lines (74 loc) · 2.99 KB
/
Copy pathdocker-tool.ps1
File metadata and controls
87 lines (74 loc) · 2.99 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
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
# Docker tool
# PowerShell script for building, running, and stopping a docker container
# version: 1.0
# run from the command line
# .\docker-tool.ps1 [command]
# command: build, run, stop, check, rm (remove dockers)
# example: .\docker-tool.ps1 build
# ----------------- Functions -----------------
# function for building
function dockerBuild {
Write-Host "Building docker image..."
#check if image exists
$imageExists = docker images -q larpex-backend-app
if ($imageExists) {
Write-Host "Docker image already exists. Removing the image..." -ForegroundColor Yellow
# stop dockers
docker stop (docker ps -a -q --filter ancestor=larpex-backend-app)
# remove dockers
docker rmi larpex-backend-app
}
docker build -t larpex-backend-app .
Write-Host "Docker image built. Run the container with the 'r' command." -ForegroundColor Magenta
}
# function for running
function dockerRun {
Write-Host "Running docker container..."
#check if container exists than run it
$containerExists = docker ps -a --filter ancestor=larpex-backend-app
if ($containerExists) {
Write-Host "Docker container already exists. Removing the container..." -ForegroundColor Yellow
# stop dockers
docker stop (docker ps -a -q --filter ancestor=larpex-backend-app)
# remove dockers
docker rm (docker ps -a -q --filter ancestor=larpex-backend-app)
}
docker run -d -p 8000:8000 larpex-backend-app
Write-Host "Check if the container is running with the 'c' command." -ForegroundColor Magenta
}
# function for stopping
function dockerStop {
Write-Host "Stopping docker container..."
docker stop (docker ps -a -q --filter ancestor=larpex-backend-app)
Write-Host "Docker container stopped." -ForegroundColor Magenta
}
function removeDockers {
Write-Host "Removing docker containers of larpex-backend-app..."
#stop dockers
docker stop (docker ps -a -q --filter ancestor=larpex-backend-app)
docker rm (docker ps -a -q --filter ancestor=larpex-backend-app)
#remove images
docker rmi larpex-backend-app
Write-Host "Docker containers removed." -ForegroundColor Magenta
}
function checkIfRunning {
Write-Host "Checking if container is running..."
docker ps -a --filter ancestor=larpex-backend-app
}
# ----------------- Main -----------------
$command = $args[0]
$usageStart = "Usage: docker-tool.ps1 [command]"
$usageExplanation = "commands: build, run, stop, check, rm (remove dockers)"
$usageExample = "example: docker-tool.ps1 build"
$warning = "Warning: Make sure you have Docker installed and running."
switch ($command) {
"build" { dockerBuild }
"run" { dockerRun }
"stop" { dockerStop }
"check" { checkIfRunning }
"rm" { removeDockers }
default { Write-Host $usageStart -ForegroundColor Cyan;
Write-Host $usageExplanation -ForegroundColor Cyan;
Write-Host $usageExample -ForegroundColor Magenta;
Write-Host $warning -ForegroundColor Yellow; break }
}