Docker Implementation

This commit is contained in:
Daniel Legt 2024-01-22 01:49:13 +02:00
parent 10bb300087
commit 63c92ac272
6 changed files with 144 additions and 7 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ snapshots.dat
dist dist
data.db data.db
data.sqlite data.sqlite
dev_data

52
Dockerfile Normal file
View File

@ -0,0 +1,52 @@
# Build Stage
FROM debian:bullseye
ENV IS_DOCKER TRUE
# Install build dependencies and runtime dependencies
RUN apt-get update && apt-get install -y \
gcc \
musl-dev \
libsqlite3-dev \
libsqlite3-0 \
wget \
&& rm -rf /var/lib/apt/lists/*
# Manually install Go 1.21
ENV GOLANG_VERSION 1.21.0
RUN wget https://golang.org/dl/go${GOLANG_VERSION}.linux-amd64.tar.gz -O go.tgz \
&& tar -C /usr/local -xzf go.tgz \
&& rm go.tgz
ENV PATH /usr/local/go/bin:$PATH
# Set the environment variable for Go
ENV GOPATH=/go
ENV PATH=$GOPATH/bin:$PATH
ENV GO111MODULE=on
ENV DIST_DIR=/app
# Create the directory and set it as the working directory
WORKDIR $DIST_DIR
# Copy the Go files and download dependencies
COPY go.mod go.sum ./
RUN go mod download
# Copy the source from the current directory to the Working Directory inside the container
COPY . .
# Build the Go app
RUN go build -o drive-health
# Cleanup build dependencies to reduce image size
RUN apt-get purge -y gcc musl-dev libsqlite3-dev wget \
&& apt-get autoremove -y \
&& apt-get clean
# Expose the necessary port
EXPOSE 8080
# Volume for external data
VOLUME [ "/data" ]
# Command to run the executable
CMD ["./drive-health"]

View File

@ -1,17 +1,19 @@
#!/bin/bash #!/bin/sh
set -o pipefail
set -u
APP_NAME="drive-health" APP_NAME="drive-health"
DIST_DIR="dist" DIST_DIR="${DIST_DIR:-dist}"
# Create the dist directory if it doesn't exist # Create the dist directory if it doesn't exist
mkdir -p $DIST_DIR mkdir -p $DIST_DIR
# Build the application # Build the application
echo "Building the application..." echo "Building the application..."
GOOS=linux GOARCH=amd64 go build -o $DIST_DIR/$APP_NAME GOOS=linux CGO_ENABLED=1 GOARCH=amd64 go build -o $DIST_DIR/$APP_NAME
# Copy additional resources (like .env, static files, templates) to the dist directory # echo "Copying additional resources..."
echo "Copying additional resources..." cp -r static templates $DIST_DIR/
cp -r .env static templates $DIST_DIR/
echo "Compilation and packaging completed." echo "Compilation and packaging completed."

56
deploy.sh Executable file
View File

@ -0,0 +1,56 @@
#!/bin/bash
# Function to display messages in color
echo_color() {
color=$1
text=$2
case $color in
"green") echo -e "\033[0;32m$text\033[0m" ;;
"yellow") echo -e "\033[0;33m$text\033[0m" ;;
"red") echo -e "\033[0;31m$text\033[0m" ;;
*) echo "$text" ;;
esac
}
# Getting GIT_VERSION from the most recent tag or commit hash
GIT_VERSION=$(git describe --tags --always)
if [ -z "$GIT_VERSION" ]; then
echo_color red "Error: Unable to determine GIT_VERSION."
exit 1
fi
# Run tests before proceeding
echo_color yellow "Running tests..."
if ! go test; then
echo_color red "Tests failed. Cancelling build process."
exit 1
fi
echo_color green "All tests passed successfully."
echo_color green "Starting the Docker build process with version $GIT_VERSION..."
IMAGE_NAME="ghcr.io/JustKato/drive-health:$GIT_VERSION"
echo_color yellow "Image to be built: $IMAGE_NAME"
# Confirmation to build
read -p "Are you sure you want to build an image? (y/N) " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
# Building the Docker image
echo "Building Docker image: $IMAGE_NAME"
docker build --no-cache -t $IMAGE_NAME .
else
echo_color red "Build cancelled."
exit 1
fi
# Prompt to push the image
read -p "Push image to repository? (y/N) " push_response
if [[ "$push_response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
# Pushing the image
echo "Pushing image: $IMAGE_NAME"
docker push $IMAGE_NAME
else
echo_color red "Push cancelled."
fi
echo_color green "Ending the Docker build process..."

14
docker-compose.dev.yml Normal file
View File

@ -0,0 +1,14 @@
version: "3.8"
services:
drive-health:
# Build the current image
build: .
# Read straight from the .env file, or use the environment path
env_file:
- .env
volumes:
- ./dev_data:/data
# Setup application ports
ports:
- 8080:8080

View File

@ -17,6 +17,8 @@ type DHConfig struct {
IdentityUsername string `json:"identityUsername"` IdentityUsername string `json:"identityUsername"`
IdentityPassword string `json:"identityPassword"` IdentityPassword string `json:"identityPassword"`
IsDocker bool `json:isDocker`
DebugMode bool `json:"debugMode"` DebugMode bool `json:"debugMode"`
} }
@ -36,6 +38,8 @@ func GetConfiguration() *DHConfig {
IdentityUsername: "admin", IdentityUsername: "admin",
IdentityPassword: "admin", IdentityPassword: "admin",
IsDocker: false,
Listen: ":8080", Listen: ":8080",
} }
@ -79,5 +83,13 @@ func GetConfiguration() *DHConfig {
} }
} }
if val, exists := os.LookupEnv("IS_DOCKER"); exists {
if isDocker, err := strconv.ParseBool(val); err == nil {
config.IsDocker = isDocker
config.DatabaseFilePath = "/data/data.sqlite"
}
}
return config return config
} }