Valensto
Valensto

Reputation: 127

Reflex golang with docker, docker-compose and makefile

hope you all are fine !

I read a lot about this problem but doesn't find something work for me ..

Explain context: I'm making an rest api with go but the code show here are fake code for readability. I'm using docker with docker-compose who build a dockerfile who CMD a makefile and I've a problem with reflex for dev env. Reflex start but doesn't reload after change. I probably miss something, I'm new to Go and Docker :)

Ok code time:

My docker-compose.yaml:

version: "3"
services:
  app:
    build: .
    ports:
      - "80:8000"
    volumes:
      - appsrc:/app
    environment:
      CODE_ENV: dev
    networks:
      - apbp-network

networks:
  apbp-network:
    driver: bridge
volumes:
  appsrc:

who call my Dockerfile:

FROM golang:latest

WORKDIR /app

COPY . .

RUN make --no-print-directory install

CMD make --no-print-directory run

who call my Makefile

GO_PROJECT_NAME := test

go_prep_build:
    @echo "\n....Preparing installation environment for $(GO_PROJECT_NAME)...."
    go get github.com/cespare/reflex

go_dep_install:
    @echo "\n....Installing dependencies for $(GO_PROJECT_NAME)...."
    go mod download

go_build:
    @echo "\n....Building $(GO_PROJECT_NAME)...."
    go build -o test ./cmd/test

go_run:
    @echo "\n....Running $(GO_PROJECT_NAME)...."
    ./test


# Project rules
install:
    $(MAKE) go_prep_build
    $(MAKE) go_dep_install
    $(MAKE) go_build

run:
    reflex --start-service -r '\.go$$' make restart


restart:
    @$(MAKE) go_build
    @$(MAKE) go_run

.PHONY: go_prep_build go_dep_install go_build go_run install run restart reflex

my ./cmd/test file:

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "hello world")
    })

    http.ListenAndServe(":8000", nil)
}

Everythings work well just my reflex command doesn't reload after change for exemple change hello world to hello world 2 doesn't rebuild.

my output:

app_1  | reflex --start-service -r '\.go$' make restart
app_1  | [00] Starting service
app_1  | [00] 
app_1  | [00] ....Building apbp....
app_1  | [00] go build -o test ./cmd/test
app_1  | [00] 
app_1  | [00] ....Running apbp....
app_1  | [00] ./test

I'm literaly stuck with it I think it's a little thing I missunderstand so if someone can help with explain that really welcome :)

Thank to read and if you need more informations ask me :)

Upvotes: 1

Views: 3063

Answers (1)

Valensto
Valensto

Reputation: 127

Ok, it was just my volume that was blocking the reload but I don't understand why so if anyone can explain it, it would be welcome !

I just change my docker-compose.yaml to :

version: "3"
services:
  app:
    build: .
    ports:
      - "80:8000"
    volumes:
      - .:/app
    environment:
      CODE_ENV: dev
    networks:
      - apbp-network

networks:
  apbp-network:
    driver: bridge

Thank for yours responses and time !

Upvotes: 2

Related Questions