How to format a DockerFile to look at a sub-directory where the app is

huangapple go评论77阅读模式
英文:

How to format a DockerFile to look at a sub-directory where the app is

问题

你可以在Dockerfile中的COPY命令中指定正确的目录路径,将*.go的复制路径修改为./app/*.go。这样Dockerfile就会在app子目录中查找.go文件了。

修改后的Dockerfile如下所示:

# syntax=docker/dockerfile:1

FROM golang:1.16-alpine

WORKDIR /app

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

COPY ./app/*.go ./

RUN go build -o /docker-gs-ping

EXPOSE 8080

CMD ["/docker-gs-ping"]

这样修改后,Dockerfile将会在app子目录中查找并复制.go文件。

英文:

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?

答案1

得分: 2

以下是一个构建轻量级 Docker 镜像的可能解决方案:

# 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"]

这个 Dockerfile 文件定义了两个阶段的构建过程。第一个阶段使用 golang:1.16-alpine 作为基础镜像,设置了一些环境变量,并在 /app 目录下复制了应用程序的文件。然后,它下载了依赖并构建了可执行文件 /docker-gs-pings。在第二个阶段,使用 scratch 作为基础镜像,将可执行文件复制到根目录,并暴露了端口 8080。最后,通过 CMD 指令运行了 /docker-gs-pings 可执行文件。

希望对你有帮助!

英文:

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" ]

答案2

得分: 1

根据我的理解,应该是这样的,基本上将所有内容复制到应用程序中,运行下载和构建命令。

# 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"]
英文:

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" ]

huangapple
  • 本文由 发表于 2021年12月9日 06:27:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/70282481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定