From ebd72fabdf60851a516d1de48093859e94e7b6bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Omar=20Vergara=20P=C3=A9rez?= Date: Fri, 3 Nov 2023 12:05:41 -0600 Subject: [PATCH] feat: add dockerfile (#10) * refactor(main): remove the localhost from the listenandserve function * build(docker): add a dockerfile to build the container image * build(makefile): add target commands to the makefile to build and run the container --- .dockerignore | 7 +++++++ Dockerfile | 27 +++++++++++++++++++++++++++ Makefile | 16 ++++++++++++++++ main.go | 2 +- 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e600295 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +.git +LICENSE +README.md +tmp/ +.dockerignore +.gitignore +docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d75212e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +# syntax=docker/dockerfile:1 + +# Build the application from source +FROM golang:1.21 AS builder + +WORKDIR /app + +COPY go.* ./ +RUN go mod download + +COPY . . + +ARG TARGETOS +ARG TARGETARCH + +RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o morphos . + +# Deploy the application binary into a lean image +FROM debian:bookworm-slim AS release + +WORKDIR / + +COPY --from=builder /app/morphos /bin/morphos + +EXPOSE 8080 + +ENTRYPOINT ["/bin/morphos"] diff --git a/Makefile b/Makefile index e8de64c..874a614 100644 --- a/Makefile +++ b/Makefile @@ -16,3 +16,19 @@ download-htmx: download-bootstrap: curl -o static/bootstrap.min.css https://cdn.jsdelivr.net/npm/bootstrap@${BOOTSTRAP_VERSION}/dist/css/bootstrap.min.css curl -o static/bootstrap.min.js https://cdn.jsdelivr.net/npm/bootstrap@${BOOTSTRAP_VERSION}/dist/js/bootstrap.bundle.min.js + +.PHONY: build +## build: Builds the container image +build: + docker build -t morphos . + +.PHONY: docker-run +## docker-run: Runs the container +docker-run: + docker run -d -p 8080:8080 morphos + +.PHONY: help +## help: Prints this help message +help: + @echo "Usage:" + @sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /' diff --git a/main.go b/main.go index 00215cd..a415e58 100644 --- a/main.go +++ b/main.go @@ -206,7 +206,7 @@ func main() { r.Post("/format", handleFileFormat) r.Get("/modal", handleModal) - http.ListenAndServe("localhost:8080", r) + http.ListenAndServe(":8080", r) } func renderError(w http.ResponseWriter, message string, statusCode int) {