Home > Software design >  How to format a DockerFile to look at a sub-directory where the app is
How to format a DockerFile to look at a sub-directory where the app is

Time:12-09

My DockerFile is looking for .go files in the wrong directory, it needs to be looking at the sub-directory called app

Here is my folder set up, parent folder is my-app

my-app/
├─ app/
│  ├─ api
│  ├─ go.mod
│  ├─ go.sum
│  ├─ main.go
DockerFile

DockerFile:

# syntax=docker/dockerfile:1

FROM golang:1.16-alpine

WORKDIR /app

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

COPY *.go ./

RUN go build -o /docker-gs-ping

EXPOSE 8080

CMD [ "/docker-gs-ping" ]

As for now the DockerFile is looking for the go files in the directory where DockerFile is, not in the app sub-directory. Where can I fix that?

CodePudding user response:

From what I understand it should be like this, basically copy everything to the app, run the download and build commands.

# syntax=docker/dockerfile:1

FROM golang:1.16-alpine
WORKDIR /app
COPY app/ .
RUN go mod download    
RUN go build -o /docker-gs-ping
EXPOSE 8080
CMD [ "/docker-gs-ping" ]

CodePudding user response:

Here a possible solution building a lightweight docker image

# syntax=docker/dockerfile:1
FROM golang:1.16-alpine as builder
ENV GO111MODULE=on \
    CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64
WORKDIR /app
COPY app/* ./
RUN go mod download
RUN go build -o /docker-gs-pings *.go
EXPOSE 8080

FROM scratch
EXPOSE 8080
COPY --from=builder /docker-gs-pings /
CMD [ "/docker-gs-pings" ]
  • Related