-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.sh
More file actions
executable file
·141 lines (127 loc) · 3.33 KB
/
Copy pathdocker.sh
File metadata and controls
executable file
·141 lines (127 loc) · 3.33 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env bash
set -euo pipefail
# docker.sh - build and run FunGame Docker image
# Usage: ./docker.sh [build|run|stop|logs|rm|rebuild|help]
IMAGE_NAME="fungame:latest"
CONTAINER_NAME="fungame_app"
ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
FRONTEND_DIR="$ROOT_DIR/frontend"
DOCKERFILE_PATH="$ROOT_DIR/Dockerfile"
print_help(){
cat <<EOF
Usage: ./docker.sh <command>
Commands:
build Build the frontend (npm) then build the Docker image (${IMAGE_NAME})
run Run the container (detached) with port 5000 published
stop Stop the running container
logs Show container logs (follow)
rm Remove the stopped container
rebuild Stop, remove, build image again and run
shell Start a shell inside a new container (for debugging)
help Show this help
Notes:
- Dockerfile at project root is used. It performs a multi-stage build that builds the frontend then copies artifacts into the Python image.
- Ensure Docker is running and you have permission to run docker commands.
EOF
}
check_docker(){
if ! command -v docker >/dev/null 2>&1; then
echo "docker is not installed or not in PATH" >&2
exit 2
fi
}
build_frontend(){
echo "Building frontend..."
if [ ! -d "$FRONTEND_DIR" ]; then
echo "Frontend directory not found: $FRONTEND_DIR" >&2
exit 1
fi
pushd "$FRONTEND_DIR" >/dev/null
if [ -f package-lock.json ]; then
npm ci --silent
else
npm install --silent
fi
npm run build
popd >/dev/null
}
docker_build(){
check_docker
if [ ! -f "$DOCKERFILE_PATH" ]; then
echo "Dockerfile not found at $DOCKERFILE_PATH" >&2
exit 1
fi
echo "Building Docker image ${IMAGE_NAME}..."
docker build -t "$IMAGE_NAME" "$ROOT_DIR"
}
docker_run(){
check_docker
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
echo "Container ${CONTAINER_NAME} already exists. Stopping and removing..."
docker rm -f "$CONTAINER_NAME" || true
fi
echo "Running container ${CONTAINER_NAME} (port 5000 -> 5000)..."
docker run -d --name "$CONTAINER_NAME" -p 5000:5000 "$IMAGE_NAME"
echo "Container started. Use './docker.sh logs' to follow logs."
}
docker_stop(){
check_docker
if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
echo "Stopping ${CONTAINER_NAME}..."
docker stop "$CONTAINER_NAME"
else
echo "No running container named ${CONTAINER_NAME}"
fi
}
docker_logs(){
check_docker
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
docker logs -f "${CONTAINER_NAME}"
else
echo "No container named ${CONTAINER_NAME}"
fi
}
docker_rm(){
check_docker
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
docker rm -f "${CONTAINER_NAME}"
echo "Removed container ${CONTAINER_NAME}"
else
echo "No container named ${CONTAINER_NAME} to remove"
fi
}
docker_shell(){
check_docker
docker run --rm -it --name "${CONTAINER_NAME}_debug" -p 5000:5000 "$IMAGE_NAME" /bin/bash
}
case ${1-""} in
build)
build_frontend
docker_build
;;
run)
docker_run
;;
stop)
docker_stop
;;
logs)
docker_logs
;;
rm)
docker_rm
;;
rebuild)
docker_stop || true
docker_rm || true
build_frontend
docker_build
docker_run
;;
shell)
docker_shell
;;
help|""|*)
print_help
;;
esac