I’ve been getting this error when I have main.go
in /cmd
folder rather than root:
| building...
| no Go files in /app
| failed to build, error: exit status 1
This is my Dockerfile
:
FROM golang:1.22.1-alpine
WORKDIR /app
COPY . .
RUN go mod download
RUN go build -o main .
CMD ["/app/main"]
This is my docker-compose.yaml
file:
version: "3"
services:
main:
container_name: myapp
build:
context: .
dockerfile: ./Dockerfile
ports:
- ${GRPC_PORT}:9090
- ${REST_PORT}:8080
volumes:
- .:/app
develop:
watch:
- action: rebuild
files:
- ./**/*.go
- ./go.mod
- ./go.sum
I’ve tried many things todo with volumes and working directories but cannot see how to build and execute from the cmd
folder.
2
Answers
I worked it out, here's an example repo and explanation article.
https://github.com/clintvidler/go-docker-live-reload
https://vidler.app/blog/golang/live-reloading-go-with-docker-compose/
I assume that by "I have
main.go
in/cmd
folder rather than root" you mean that your project is structured something like this:In that case this should work:
🗎
Dockerfile
🗎
docker-compose.yml
🗎
main.go
(A placeholder for yourmain.go
.)