创建用于 Golang Web 应用程序的 Dockerfile

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

Creating dockerfile for golang web application

问题

我尝试按照教程的步骤操作,但由于我的目录结构设置的原因,我似乎无法创建一个Dockerfile来为我的Web应用程序创建一个Docker容器,以便我可以使用Docker在多个设备上运行它。

这是我的项目结构:

MyApp
    /cmd
        /web
            - main.go
            - middleware.go
            - routes.go
    /pkg
        /somepackage
            - somegolangfile.go 
        /anotherpackageIuse
            - somegolangfile.go
    /templates
        -HTML files 
    go.mod
    go.sum
    .gitignore 
    Dockerfile 

这是我正在使用的Dockerfile:

FROM golang:latest as build

WORKDIR /build
COPY . .
RUN go build  -o app .
 
FROM alpine:latest as run
WORKDIR /app
COPY --from=build /build/app .
ENTRYPOINT ["/app/app"]

我得到的错误是/build目录中没有go文件。这是正确的,错误也是有道理的,因为go文件在cmd/web/目录中。

然而,我似乎无法解决这个问题。我尝试过以下操作:

FROM golang:latest as build

WORKDIR /build
COPY . .
RUN go build  cmd/web/ -o app .
 
FROM alpine:latest as run
WORKDIR /app
COPY --from=build /build/app .
ENTRYPOINT ["/app/app"]

这里的关键区别是我用RUN go build -o app .替换为RUN go build cmd/web/ -o app .,但是我仍然得到错误,说我的sum.go和mod.go在目录中不存在,这也是正确的。

如何为我的Web应用程序创建一个Docker容器,并使其在我的go文件位于不同目录的情况下构建?此外,当我正常运行我的Web应用程序时,我使用go run cmd/web/*.go,其中cmd/web目录中的所有golang文件需要同时运行。因此,我还需要我的Dockerfile来构建和编译所有的golang文件。

英文:

I've tried following the tutorials out there, but for some reason, because of how my directories are set up, I can't seem to make a dockerfile to create a docker container for my web app so I can run it on multiple devices using docker.

Here is my project structure:

MyApp
    /cmd
        /web
            - main.go
            - middleware.go
            - routes.go
    /pkg
        /somepackage
            - somegolangfile.go 
        /anotherpackageIuse
            - somegolangfile.go
    /templates
        -HTML files 
    go.mod
    go.sum
    .gitignore 
    Dockerfile 

Here is the dockerfile I'm using:

FROM golang:latest as build

WORKDIR /build
COPY . .
RUN go build  -o app .
 
FROM alpine:latest as run
WORKDIR /app
COPY --from=build /build/app .
ENTRYPOINT ["/app/app"]

I get the error that there are no go files in /build. This is correct and the error makes sense since the go files are in cmd/web/

However, I can't seem to fix it. I've tried doing:

FROM golang:latest as build

WORKDIR /build
COPY . .
RUN go build  cmd/web/ -o app .
 
FROM alpine:latest as run
WORKDIR /app
COPY --from=build /build/app .
ENTRYPOINT ["/app/app"]

Key difference here is that I've swapped out RUN go build -o app .with RUN go build cmd/web/ -o app ., and yet I get the error that my sum.go and mod.go are not present in the directory, which is also true

How do I make a docker container for my web app, and have it build in which my go files are in a different directory? Also, when I run my web app normally, I used go run cmd/web/*.go in which all golang files in the cmd/web directory need to be run at the same time. Therefore, I also need my dockerfile to build and compile all the golang files together

=============================================

答案1

得分: 1

你的构建命令中有一个多余的点,并且参数的顺序错误:

RUN go build -o app ./cmd/web/...

正确的命令应该是:

RUN go build ./cmd/web/... -o app
英文:

There is an extra dot in your build command and wrong arguments order:

RUN go build -o app ./cmd/web/...

答案2

得分: 0

嗨 @Radin Khosraviani,

copy . . 只会复制源代码。你还需要复制 Go 模块文件

下面是我修改后的 Dockerfile,并添加了 COPY go.mod .COPY go.sum .RUN go mod download

FROM golang:latest as build

WORKDIR /app

# 复制 Go 模块文件
COPY go.mod .
COPY go.sum .

# 下载 Go 模块依赖
RUN go mod download

COPY . .

RUN go build -o /myapp ./cmd/web

FROM alpine:latest as run

# 从构建镜像中复制应用可执行文件
COPY --from=build /myapp /myapp

WORKDIR /app
EXPOSE 8080
CMD ["/myapp"]

希望对你有帮助!

英文:

Hey @Radin Khosraviani,

copy . . will only copy the source code. You need to copy the Go module files as well.

Below I have modified your docker file and have added COPY go.mod . COPY go.sum . RUN go mod download

FROM golang:latest as build

WORKDIR /app

# Copy the Go module files
COPY go.mod .
COPY go.sum .

# Download the Go module dependencies
RUN go mod download

COPY . .

RUN go build -o /myapp ./cmd/web
 
FROM alpine:latest as run

# Copy the application executable from the build image
COPY --from=build /myapp /myapp

WORKDIR /app
EXPOSE 8080
CMD ["/myapp"]

huangapple
  • 本文由 发表于 2023年4月10日 09:55:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75973805.html
匿名

发表评论

匿名网友

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

确定