go-blueprint icon indicating copy to clipboard operation
go-blueprint copied to clipboard

[Feature Request] Add make target to start a development env with database and air hot loading

Open mingan666 opened this issue 1 year ago • 1 comments

Tell us about your feature request

Currently there is no make target that enables me start the database together with air hot loading. Either I can use make docker-run which does not support air hot loading, or I can use make watch which does not start the database. I'd like to be able to start a development environment that supports both a database (as a test container like used for the tests or a regular docker image) together with the server with air hot loading support.

Disclaimer

  • [X] I agree

mingan666 avatar Dec 13 '24 10:12 mingan666

We can approach the solution in two different ways. One would be to simply add a target to the Makefile so that when make watch is executed, the database is also spun up:

watch:  docker-run
       @if command -v air > /dev/null; then \
	    air; \
	    echo "Watching...";\
        ...

Alternatively, we can achieve this using a Dockerfile and Docker Compose: (This is just a code snippet to demonstrate the concept, the database part is not included.)

FROM golang:1.22-alpine AS base

RUN apk add --no-cache make

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .

FROM base AS dev
RUN make build
EXPOSE 5000
CMD [ "sh", "-c", "echo 'y' | make watch" ]

FROM base AS build
RUN make build

FROM alpine:3.20.1 AS prod
WORKDIR /app
COPY --from=build /app/main /app/main
EXPOSE 5000
CMD ["./main"]
services:
  web-dev:
    build:
      context: .
      dockerfile: Dockerfile
      target: dev
    ports:
      - 5000:5000
    volumes:
      - ./:/app

  web-prod:
    build:
      context: .
      dockerfile: Dockerfile
      target: prod
    ports:
      - 5000:5000

# docker compose up  web-dev --build
# docker compose up  web-prod --build

There is also a compose watch option that could be considered as an alternative.

https://docs.docker.com/compose/how-tos/file-watch/

It is a nice issue to solve, so any PRs are welcome.

Ujstor avatar Dec 15 '24 12:12 Ujstor