Skip to content

Commit dfe10c3

Browse files
committed
Add initial docker env taskfile
Signed-off-by: Evans Mungai <[email protected]>
1 parent dac6b1f commit dfe10c3

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

applications/taskfiles/docker.yml

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
version: "3"
2+
3+
# Development environment tasks
4+
tasks:
5+
build-image:
6+
desc: Build development Docker image
7+
vars:
8+
IMAGE_NAME: '{{.IMAGE_NAME}}'
9+
IMAGE_TAG: '{{.IMAGE_TAG | default "latest"}}'
10+
DOCKERFILE: '{{.DOCKERFILE | default "Dockerfile"}}'
11+
DOCKERFILE_PATH: '{{.DOCKERFILE_PATH | default "."}}'
12+
BUILD_ARGS: '{{.BUILD_ARGS | default ""}}'
13+
requires:
14+
vars: [IMAGE_NAME, IMAGE_TAG, DOCKERFILE, DOCKERFILE_PATH]
15+
16+
cmds:
17+
- docker build -t {{.IMAGE_NAME}} -f {{.DOCKERFILE}} {{.DOCKERFILE_PATH}}
18+
19+
start:
20+
desc: Start development container in background
21+
silent: true
22+
vars:
23+
IMAGE_NAME: '{{.IMAGE_NAME}}'
24+
CONTAINER_NAME: '{{.CONTAINER_NAME}}'
25+
IMAGE_TAG: '{{.IMAGE_TAG | default "latest"}}'
26+
requires:
27+
vars: [IMAGE_NAME, CONTAINER_NAME]
28+
29+
cmds:
30+
- echo "Starting {{.CONTAINER_NAME}} development container..."
31+
- |
32+
# Check if container is running
33+
if docker ps | grep -q "{{.CONTAINER_NAME}}"; then
34+
echo "Container {{.CONTAINER_NAME}} is already running"
35+
# Check if container exists but is not running
36+
elif docker ps -a | grep -q "{{.CONTAINER_NAME}}"; then
37+
echo "Container {{.CONTAINER_NAME}} exists but is not running. Starting it..."
38+
docker start {{.CONTAINER_NAME}}
39+
# Create and start new container
40+
else
41+
# Try ports 5000-5004, use the first available one
42+
PORT_FOUND=false
43+
for PORT in {5000..5004}; do
44+
if ! lsof -i :$PORT > /dev/null; then
45+
PORT_ARG="-p $PORT:$PORT"
46+
echo "Using port $PORT for MLflow UI"
47+
PORT_FOUND=true
48+
break
49+
fi
50+
done
51+
52+
# If all standard ports are in use, use a random port
53+
if [ "$PORT_FOUND" = "false" ]; then
54+
PORT_ARG="-P"
55+
echo "All standard ports are in use. Using a random port."
56+
fi
57+
58+
# Start container with host networking for kubectl port-forward compatibility
59+
CONTAINER_ID=$(docker run --name {{.CONTAINER_NAME}} --network host -d \
60+
-v $(pwd):/workspace \
61+
-v ~/.kube:/home/devuser/.kube \
62+
-v ~/.helm:/home/devuser/.helm \
63+
-v ~/.replicated:/home/devuser/.replicated \
64+
-e SHELL=/bin/bash \
65+
-e HOME=/home/devuser \
66+
-e USER=devuser \
67+
-e HOST_NETWORK=true \
68+
-w /workspace \
69+
{{.IMAGE_NAME}}:{{.IMAGE_TAG}} sleep infinity)
70+
71+
if [ $? -eq 0 ]; then
72+
echo "Development container started successfully with ID: $CONTAINER_ID"
73+
echo "Ports opened within the container will be directly accessible on your host machine."
74+
else
75+
echo "Failed to start development container"
76+
exit 1
77+
fi
78+
fi
79+
80+
ensure-running:
81+
desc: Ensure the development container is running
82+
internal: true
83+
silent: true
84+
cmds:
85+
- |
86+
if ! docker ps | grep -q "{{.CONTAINER_NAME}}"; then
87+
echo "Container {{.CONTAINER_NAME}} is not running. Starting it..."
88+
task start
89+
else
90+
echo "Container {{.CONTAINER_NAME}} is already running."
91+
fi
92+
status:
93+
- docker ps | grep -q "{{.CONTAINER_NAME}}"
94+
95+
shell:
96+
desc: Attach to development container shell
97+
deps: [ensure-running]
98+
silent: true
99+
cmds:
100+
- echo "Connecting to {{.CONTAINER_NAME}}..."
101+
- docker exec -it {{.CONTAINER_NAME}} /bin/bash
102+
103+
stop:
104+
desc: Stop development container
105+
silent: true
106+
cmds:
107+
- |
108+
if docker ps | grep -q "{{.CONTAINER_NAME}}"; then
109+
echo "Stopping {{.CONTAINER_NAME}} development container..."
110+
docker stop {{.CONTAINER_NAME}}
111+
else
112+
echo "Container {{.CONTAINER_NAME}} is not running"
113+
fi
114+
115+
restart:
116+
desc: Restart development container
117+
silent: true
118+
cmds:
119+
- task: stop
120+
- task: start

applications/wg-easy/Taskfile.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ version: "3"
22

33
includes:
44
utils: ./taskfiles/utils.yml
5+
dev: ./../taskfiles/docker.yml
56

67
vars:
78
# Application configuration
@@ -27,6 +28,11 @@ vars:
2728
GCP_ZONE: '{{.GCP_ZONE | default "us-central1-a"}}'
2829
VM_NAME: '{{.VM_NAME | default (printf "%s-dev" (or (env "GUSER") "user"))}}'
2930

31+
# Docker workflow configuration
32+
IMAGE_NAME: ttl.sh/wg-easy-dev
33+
CONTAINER_NAME: wg-easy-dev
34+
IMAGE_TAG: latest
35+
3036
tasks:
3137
default:
3238
desc: Show available tasks

0 commit comments

Comments
 (0)