Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] When using docker to build an image, resources (eg: i18n) cannot be included #2874

Closed
shuqingzai opened this issue Aug 17, 2023 · 16 comments
Labels
bug It is confirmed a bug, but don't worry, we'll handle it. enhancement question

Comments

@shuqingzai
Copy link

shuqingzai commented Aug 17, 2023

1. What version of Go and system type/arch are you using?

#-> % go version
go version go1.21.0 darwin/amd64

2. What version of GoFrame are you using?

#-> % gf version
GoFrame CLI Tool v2.5.1, https://goframe.org
GoFrame Version: v2.5.1 in current go.mod
CLI Installed At: /Code/go/bin/gf
CLI Built Detail:
  Go Version:  go1.20.4
  GF Version:  v2.5.1
  Git Commit:  2023-07-26 21:27:58 e0e00434cc87d6edf64fc3df40ce7d3f40758794
  Build Time:  2023-07-26 21:32:56

3. Can this issue be re-produced with the latest release?

是的

4. What did you do?

在使用 docker 编译的时候,需要怎么样才能包含 i18n 资源???

编译配置
  # 项目编译
  # docs: https://goframe.org/pages/viewpage.action?pageId=1115788
  build:
    name: "app"
    #arch: "amd64"
    #system: "linux"
    mod: "none"
    cgo: 0
    packSrc: "resource"
#    packSrc: "resource/i18n,resource/public,resource/template"
    version: "0.0.1"
    output: "./bin/app"
    extra: ""
Dockerfile

使用下面的 Dockerfile 进行编译运行

ARG GOLANG_VERSION=1.20

# 时区 --build-arg TIMEZONE=Asia/Shanghai
ARG TIMEZONE=Asia/Shanghai


FROM golang:${GOLANG_VERSION} AS builder

# 时区
ENV TZ=${TIMEZONE}

# go env
RUN go env -w GOPROXY="https://goproxy.cn,https://goproxy.io,direct" \
    && go env -w GO111MODULE=on \
    && go env

WORKDIR /src

COPY go.mod go.sum ./

RUN go mod download -x

COPY . /src

RUN make cli-dependents && make wire && make build


FROM debian:stable-slim as final

# 时区
ENV TZ=${TIMEZONE}

WORKDIR /app

COPY --from=builder /src/bin /app/bin/

RUN set -eux \
    && cd /tmp/ \
    # ustc.edu
    # debian 12 之前的版本
    #&& ([ -f /etc/apt/sources.list ] && sed -i 's|deb.debian.org|mirrors.ustc.edu.cn|g' /etc/apt/sources.list) \
    # debian 12 改变了源文件
    && ([ -f /etc/apt/sources.list.d/debian.sources ] && sed -i 's|deb.debian.org|mirrors.ustc.edu.cn|g' /etc/apt/sources.list.d/debian.sources) \
    # aliyun
    # debian 12 之前的版本
    #&& ([ -f /etc/apt/sources.list ] && sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list) \
    # debian 12 改变了源文件
    # && ([ -f /etc/apt/sources.list.d/debian.sources ] && sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources) \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get update --fix-missing \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
        netbase \
		ca-certificates  \
        procps \
        curl \
        iputils-ping \
    && rm -rf /var/lib/apt/lists/* /var/cache/apt/* /tmp/* \
    && apt-get autoremove -y  \
    && apt-get autoclean -y \
    && apt-get clean -y \
    && chmod +x /app/bin/* \
    && mkdir -p /app/config /app/logs


# http 服务端口
EXPOSE 8000
# 配置文件目录
VOLUME /app/config

#ENTRYPOINT ["/app/bin/app", "http", "--with-task","--app-config", "/app/config/config.yaml"]
CMD ["/app/bin/app", "http", "--with-task","--app-config", "/app/config/config.yaml"]
构建命令
# 编译
docker build --file manifest/docker/Dockerfile  --tag=cstm:0.0.1  .

# 运行
docker run --name cstm -it --rm  -p 8888:8888 \
-v /customeow-goframe/manifest/config:/app/config \
cstm:0.0.1  /app/bin/app http --with-task --app-config=/app/config/config.yaml

运行后镜像后, 没有包含 resourcei18n 资源文件,编译过程也看到类似如下日志

2023-08-17 14:28:42.840 gf pack resource internal/packed/build_pack_data.go --keepPath=true
2023-08-17 14:28:43.222 done!
2023-08-17 14:28:43.242 start building...
2023-08-17 14:28:43.242 go build -o ./bin/app main.go
2023-08-17 14:28:44.610 done!
2023-08-17 14:28:44.610 remove the automatically generated resource go file: internal/packed/build_pack_data.go

编译命令主要是 make build ,使用的是 gf 内置的 Makefile ,但是无法加载资源

我不确定是否与

searchFolders = []string{"manifest/i18n", "manifest/config/i18n", "i18n"}
有关系,这里并没有指向 resource ,但是框架脚手架创建出来的 i18n 目录确实是在 resource 中, 而且我的编译配置当中也已经包含了 resource 目录( packSrc: "resource" )

5. What did you expect to see?

希望可以给出指导,正常在 docker 镜像中把资源编译成二进制文件

6. What did you see instead?

@Issues-translate-bot Issues-translate-bot changed the title [BUG] 使用 docker 构建镜像时,无法包含资源(如:i18n) [BUG] When using docker to build an image, resources (eg: i18n) cannot be included Aug 17, 2023
@hailaz
Copy link
Member

hailaz commented Aug 21, 2023

的确目前的代码没有包含这个目录,可以试试这几个方法,各方法无关联。

  1. 可以试试gi18n.SetPath("resource/i18n")
  2. 自己新建一个管理器
i18n := gi18n.New(gi18n.Options{
	Path: "resource/i18n",
})
  1. 改一下i18n的存放目录,同时需要修改gf build的配置。
  2. 改一下docker build的配置,直接复制resource的i18n到镜像对应目录中/app/bin/,这里只是提供思路,具体目录按实际修改。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


It is true that the current code does not include this directory, you can try these methods, each method is irrelevant.

  1. You can try gi18n.SetPath("resource")
  2. Create a new manager yourself
i18n := gi18n.New(gi18n.Options{
Path: "resource",
})
  1. Change the storage directory of i18n, and also need to modify the configuration of gf build.
  2. Change the configuration of the docker build, and directly copy the i18n of the resource to /app/bin/ in the corresponding directory of the image. This is just to provide ideas, and the specific directory should be modified according to the actual situation.

@gqcn gqcn added enhancement done This issue is done, which may be release in next version. labels Aug 21, 2023
@gqcn
Copy link
Member

gqcn commented Aug 21, 2023

@gqcn gqcn closed this as completed Aug 21, 2023
@hailaz
Copy link
Member

hailaz commented Aug 22, 2023

最新结果,将多语言文件放到目录manifest/i18n下面

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


For the latest results, put the multilingual files under the directory manifest/i18n

@shuqingzai
Copy link
Author

shuqingzai commented Sep 4, 2023

最新结果,将多语言文件放到目录manifest/i18n下面

@hailaz 我试了,似乎没效果,你使用我上面的 dockerfile 吗?还是说你有自己的 dockerfile ??可以参考一下?

我的迁移如下:

image

容器中运行还是只是显示默认 en

image

如果我在宿主机直接使用 gf run 运行,是可以翻译的,效果如下:
image

不理解这个问题没有解决,为啥就关闭了 issue ,也不给出提示可用方案 @gqcn ???

@shuqingzai
Copy link
Author

我在容器内部打印出打包的资源列表,是存在我的资源信息,但是没法转换?读取不到吗?

image

@gqcn @hailaz

@shuqingzai
Copy link
Author

shuqingzai commented Sep 4, 2023

我知道什么原因了 @hailaz @gqcn

  1. 如果项目根源文件目录路径与编译后的可执行文件路径不一致时,需要使用相对路径指向有效的资源路径
    因为一般项目编译后的可以执行文件都在 bin 目录下,就像我的这个 dockerfile 中,源码是在 /src 下,编译后放在 /app/bin 下,导致使用 g.I18n().SetPath("manifest/i18n") 没有效果,需要使用相对路径:../manifest/i18n 读取 I18N 资源

编译后目录结构参考:

image

/app/bin/app 直接读取是没法读取的,使用 gres.Dump() 打印出来的路径应该是相对于编译时的项目源码路径,相当于在 /app/manifest/i18n

这应该是在文档中有所提示,我觉得这个坑应该会有不少人遇到

  1. 需要在程序内部使用 g.I18n().SetPath("../manifest/i18n") 设置上诉描述中的相对路径

image

image

image

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


I know why @hailaz @gqcn

  1. If the project root file directory path is inconsistent with the compiled executable file path, you need to use a relative path to point to a valid resource path
    Because the compiled executable files of general projects are in the bin directory, just like in my dockerfile, the source code is under /src, and after compilation, it is placed under /app/bin, resulting in the use of g.I18n().SetPath("manifest/i18n") has no effect, you need to use a relative path: ../manifest/i18n to read I18N resources

Directory structure reference after compilation:

image

/app/bin/app cannot be read directly. The path printed by gres.Dump() should be relative to the project source path when compiling, which is equivalent to /app/manifest/ i18ndown

This should be hinted in the documentation, I think this pit will be encountered by many people

  1. You need to use g.I18n().SetPath("../manifest/i18n") inside the build to set the relative path in the appeal description

image

image

@hailaz
Copy link
Member

hailaz commented Sep 6, 2023

你的情况是资源文件在二进制外且不是同目录下,这种情况只能自己确定好路径然后配置即可。

可以试试将资源文件打包到二进制中,就不用指定了,简单很多。
https://goframe.org/pages/viewpage.action?pageId=1115788
packSrc参数

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


In your case, the resource file is outside the binary and not in the same directory. In this case, you can only determine the path and configure it yourself.

You can try to package the resource files into the binary, so you don’t need to specify it, it’s much simpler.
https://goframe.org/pages/viewpage.action?pageId=1115788

@hailaz
Copy link
Member

hailaz commented Sep 14, 2023

默认的gi18n.Manager有bug,使用gi18n.XXX无法读取到gres中的资源。

如果是使用gres打包的资源文件可以暂时使用如下方法

myi18n := gi18n.New(gi18n.Options{
    Path: "manifest/i18n",// 指定你在gres中的资源目录
})
myi18n.SetLanguage("zh-CN")
fmt.Println(myi18n.Translate(context.Background(), "{#hello}{#world}"))

@hailaz hailaz reopened this Sep 14, 2023
@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


The default gi18n.Manager has a bug, and resources in gres cannot be read using gi18n.XXX.

@hailaz hailaz added bug It is confirmed a bug, but don't worry, we'll handle it. and removed done This issue is done, which may be release in next version. labels Sep 14, 2023
@shuqingzai
Copy link
Author

你的情况是资源文件在二进制外且不是同目录下,这种情况只能自己确定好路径然后配置即可。

可以试试将资源文件打包到二进制中,就不用指定了,简单很多。 https://goframe.org/pages/viewpage.action?pageId=1115788 packSrc参数

@hailaz

我已经设置了,没用的,读不到

image

@gqcn
Copy link
Member

gqcn commented Oct 8, 2023

@shuqingzai @hailaz 这个i18n的目录不建议手动设置而是使用默认的,当使用docker打包的时候,请将i18n的目录使用Dockerfile拷贝到二进制的同目录下即可使用。 例如:
image

@hailaz 说的资源管理不支持i18n是另一个问题了。

@gqcn gqcn added the question label Oct 8, 2023
@hailaz
Copy link
Member

hailaz commented Nov 1, 2023

fix in #2961 @shuqingzai

@hailaz hailaz closed this as completed Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it. enhancement question
Projects
None yet
Development

No branches or pull requests

4 participants