skip to Main Content

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



  1. I assume that by "I have main.go in /cmd folder rather than root" you mean that your project is structured something like this:

    ├── cmd
    │   ├── go.mod
    │   └── main.go
    ├── docker-compose.yml
    └── Dockerfile
    

    In that case this should work:

    🗎 Dockerfile

    FROM golang:1.22.1-alpine
    
    WORKDIR /app
    
    COPY cmd/ .
    
    RUN go mod download
    
    RUN go build -o main .
    
    CMD ["/app/main"]
    

    🗎 docker-compose.yml

    version: "3"
    
    services:
      main:
        container_name: myapp
        build:
          context: .
          dockerfile: ./Dockerfile
    

    🗎 main.go (A placeholder for your main.go.)

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        fmt.Println("Hello, World!")
    }
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search