diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ca37414 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,30 @@ +services: + db: + build: + context: ./database + dockerfile: Dockerfile + ports: + - 55432:5432 + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 1s + timeout: 5s + retries: 10 + machines-app: + build: + context: ./machines + dockerfile: Dockerfile + environment: + - DBHOST=db + - DBPORT=5432 + - DBNAME=komponiranje + - DBUSER=pero + - DBPASSWORD=pero.000 + ports: + - 3000:3000 + depends_on: + db: + condition: service_healthy diff --git a/machines/.dockerignore b/machines/.dockerignore new file mode 100644 index 0000000..16e89a9 --- /dev/null +++ b/machines/.dockerignore @@ -0,0 +1,3 @@ +**/build +**/tmp +**/.env* diff --git a/machines/Dockerfile b/machines/Dockerfile new file mode 100644 index 0000000..199f4b8 --- /dev/null +++ b/machines/Dockerfile @@ -0,0 +1,20 @@ +# stage 2: build golang backend +FROM golang:1.21-alpine3.19 as go-builder + +WORKDIR /go-builder +COPY . . + +RUN \ + go mod download && \ + go mod verify && \ + go build -v -ldflags "-s -w" -o machines-app ./app/main.go + + +# stage 2: build final container +FROM alpine:3.19 + +USER $USER +WORKDIR /app +COPY --from=go-builder /go-builder/machines-app /app + +ENTRYPOINT ["/app/machines-app"] diff --git a/machines/Makefile b/machines/Makefile index 6f8dde0..3765ea6 100644 --- a/machines/Makefile +++ b/machines/Makefile @@ -2,13 +2,38 @@ EXEC=machines-app CONTAINER_NAME=machines-app IMAGE_NAME=komponiranje-machines-app + run: @air + .PHONY: build build: @go build -ldflags "-s -w" -o ./build/${EXEC} ./app/. + upgrade-packages: @go get -u ./... + +docker-build: clean + @docker build \ + --progress=plain \ + --tag $(IMAGE_NAME) \ + . + + +docker-run: + @docker run \ + --name $(CONTAINER_NAME) \ + --publish 3000:3000 \ + --env CONTAINER_NAME="Awesome API server" \ + --env DBPORT=55432 \ + --detach \ + $(IMAGE_NAME) + + +clean: + - @docker stop $(CONTAINER_NAME) + - @docker rm $(CONTAINER_NAME) + - @docker rmi $(IMAGE_NAME)