-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-nix-docker.sh
More file actions
executable file
·233 lines (196 loc) · 5.81 KB
/
Copy pathbuild-nix-docker.sh
File metadata and controls
executable file
·233 lines (196 loc) · 5.81 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env bash
# Build script for creating Docker image with Nix
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Print colored message
print_msg() {
echo -e "${2:-$GREEN}$1${NC}"
}
print_error() {
echo -e "${RED}ERROR: $1${NC}" >&2
}
print_warning() {
echo -e "${YELLOW}WARNING: $1${NC}"
}
print_info() {
echo -e "${BLUE}INFO: $1${NC}"
}
# Check if Nix is installed
check_nix() {
if ! command -v nix &> /dev/null; then
print_error "Nix is not installed!"
print_info "Install Nix with:"
echo " curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install"
exit 1
fi
print_msg "✓ Nix is installed" "$GREEN"
}
# Check if Docker is installed
check_docker() {
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed!"
print_info "Install Docker from https://docs.docker.com/get-docker/"
exit 1
fi
print_msg "✓ Docker is installed" "$GREEN"
}
# Build using flake.nix (recommended)
build_with_flake() {
print_msg "Building Docker image using Nix flakes..." "$BLUE"
if [ ! -f "flake.nix" ]; then
print_error "flake.nix not found!"
exit 1
fi
# Build the Docker image using Nix
print_info "Building image (this may take a while on first run)..."
nix build .#dockerImage --impure
# Load the image into Docker
print_info "Loading image into Docker..."
docker load < result
print_msg "✓ Docker image built successfully!" "$GREEN"
print_info "Image name: manim-gpt:latest"
}
# Build using Dockerfile with Nix
build_with_dockerfile() {
print_msg "Building Docker image using Dockerfile with Nix..." "$BLUE"
local dockerfile="${1:-Dockerfile}"
if [ ! -f "$dockerfile" ]; then
print_error "$dockerfile not found!"
exit 1
fi
print_info "Building with $dockerfile..."
docker build -f "$dockerfile" -t manim-gpt:latest .
print_msg "✓ Docker image built successfully!" "$GREEN"
}
# Run the Docker container
run_container() {
print_msg "Starting manim-gpt container..." "$BLUE"
# Check if .env file exists
if [ ! -f ".env" ]; then
print_warning ".env file not found, copying from .env.example"
if [ -f ".env.example" ]; then
cp .env.example .env
print_info "Please edit .env and add your API keys"
fi
fi
# Stop existing container if running
if docker ps -a | grep -q manim-gpt; then
print_info "Stopping existing container..."
docker stop manim-gpt 2>/dev/null || true
docker rm manim-gpt 2>/dev/null || true
fi
# Run the container
docker run -d \
--name manim-gpt \
-p 8000:8000 \
-p 7860:7860 \
--env-file .env \
-v "$(pwd)/media:/app/media" \
-v "/tmp/manim_videos:/tmp/manim_videos" \
--restart unless-stopped \
manim-gpt:latest
print_msg "✓ Container started successfully!" "$GREEN"
print_info "API available at: http://localhost:8000"
print_info "Docs available at: http://localhost:8000/docs"
print_info "Gradio UI at: http://localhost:7860"
}
# Show usage
usage() {
cat << EOF
Usage: $0 [OPTIONS]
Build and run manim-gpt Docker container with Nix
OPTIONS:
-f, --flake Build using flake.nix (recommended)
-d, --dockerfile Build using Dockerfile (default)
-n, --nix-docker Build using Dockerfile.nix
-r, --run Run container after building
-s, --stop Stop and remove running container
-h, --help Show this help message
EXAMPLES:
$0 --flake --run Build with flake and run
$0 --dockerfile --run Build with Dockerfile and run
$0 --stop Stop running container
EOF
}
# Main
main() {
local build_method="dockerfile"
local should_run=false
local should_stop=false
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-f|--flake)
build_method="flake"
shift
;;
-d|--dockerfile)
build_method="dockerfile"
shift
;;
-n|--nix-docker)
build_method="nix-dockerfile"
shift
;;
-r|--run)
should_run=true
shift
;;
-s|--stop)
should_stop=true
shift
;;
-h|--help)
usage
exit 0
;;
*)
print_error "Unknown option: $1"
usage
exit 1
;;
esac
done
print_msg "=== Manim GPT Docker Build Script ===" "$BLUE"
echo
# Stop container if requested
if [ "$should_stop" = true ]; then
print_info "Stopping container..."
docker stop manim-gpt 2>/dev/null || true
docker rm manim-gpt 2>/dev/null || true
print_msg "✓ Container stopped" "$GREEN"
exit 0
fi
# Check prerequisites
check_docker
# Build based on method
case $build_method in
flake)
check_nix
build_with_flake
;;
dockerfile)
build_with_dockerfile "Dockerfile"
;;
nix-dockerfile)
check_nix
build_with_dockerfile "Dockerfile.nix"
;;
esac
# Run container if requested
if [ "$should_run" = true ]; then
run_container
else
print_info "To run the container, use: $0 --run"
print_info "Or use docker-compose: docker-compose up -d"
fi
echo
print_msg "=== Build Complete ===" "$GREEN"
}
# Run main function
main "$@"