Skip to content

Commit

Permalink
refactor: Standardize entry point script in Dockerfiles (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-zehnder committed May 30, 2024
1 parent a4d297d commit 8172b08
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
15 changes: 11 additions & 4 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@ RUN swag init && CGO_ENABLED=0 go build -o main .
RUN test -f docs/swagger.json && test -f docs/swagger.yaml

# Stage 2: Setup the application in a smaller container
FROM alpine:latest
FROM debian:latest
WORKDIR /root/

# Copy the built application binary from the build stage
# Install MySQL client for database initialization
RUN apt-get update && apt-get install -y default-mysql-client

# Copy the built application binary and entrypoint script from the build stage
COPY --from=build /app/main .
COPY --from=build /app/bin/entrypoint.sh .

# Ensure entrypoint.sh is executable
RUN chmod +x /root/entrypoint.sh

# Expose the application port
EXPOSE 8080

# Run the application binary
CMD ["./main"]
# Run the entrypoint script
ENTRYPOINT ["/root/entrypoint.sh"]
14 changes: 7 additions & 7 deletions backend/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Use the official Go image as the base image
FROM golang:latest

# Install necessary packages
RUN apt-get update && apt-get install -y default-mysql-client

# Set the working directory for the application
WORKDIR /app

# Install MySQL client using apt-get
RUN apt-get update && apt-get install -y default-mysql-client

# Install swag CLI for generating Swagger docs
RUN go install github.com/swaggo/swag/cmd/swag@latest

Expand All @@ -22,11 +22,11 @@ COPY . .
# Generate Swagger docs
RUN swag init

# Ensure init-db.sh is executable
RUN chmod +x /app/bin/init-db.sh
# Ensure entrypoint.sh is executable
RUN chmod +x /app/bin/entrypoint.sh

# Expose port 8080
EXPOSE 8080

# Run the initialization script and start the Go application
CMD ["/app/bin/init-db.sh"]
# Run the entrypoint script
ENTRYPOINT ["/app/bin/entrypoint.sh"]
7 changes: 6 additions & 1 deletion backend/bin/init-db.sh → backend/bin/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ echo "Database initialization completed."

# Start the main application
echo "Starting Go application..."
exec go run main.go

if [ "$ENVIRONMENT" = "development" ]; then
exec go run main.go
else
exec ./main
fi

0 comments on commit 8172b08

Please sign in to comment.