-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdocker_build_and_test.sh
50 lines (40 loc) · 1.15 KB
/
docker_build_and_test.sh
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
#!/bin/bash
# Constants
DOCKERFILE_PATH="./Dockerfile"
IMAGE_NAME="aws-fcj-management"
CONTAINER_NAME="aws-fcj-management-container"
PORT="5000"
# Build the Docker image
echo "Building Docker image..."
docker build -t "$IMAGE_NAME" -f "$DOCKERFILE_PATH" .
# Check if the build was successful
if [ $? -ne 0 ]; then
echo "Error: Docker build failed."
exit 1
fi
# Run a container from the built image
echo "Running Docker container..."
docker run -d -p "$PORT":5000 --name "$CONTAINER_NAME" "$IMAGE_NAME"
# Check if the container is running
if [ $? -ne 0 ]; then
echo "Error: Docker run failed."
exit 1
fi
# Wait for the container to start
echo "Waiting for the container to start..."
# Use curl to check if the service inside the container is ready
while true; do
if curl -s "http://localhost:$PORT/healthcheck" > /dev/null; then
echo "Container is up and running."
break
fi
sleep 1
done
# Clean up: stop and remove the container
echo "Stopping and removing the container..."
docker stop "$CONTAINER_NAME"
docker rm "$CONTAINER_NAME"
# Clean up: remove the image
echo "Removing the Docker image..."
docker rmi "$IMAGE_NAME"
echo "Script completed."