所以,我通过 GitHub 上的 rejetto 对一个开源项目 HFS 进行了 docker 化。基本上,您可以通过环境变量进行配置,使用形式 HFS_=(例如 HFS_PORT=80)。
我想做的是在运行时通过 Docker 传递这些环境变量,但是发生了一些奇怪的事情:只有在容器内手动执行时,该二进制文件才会获取环境变量,如果由 docker 在入口点自动运行,则会忽略它们,即使在同一个入口点脚本中,我也可以看到它们已正确设置。
一些背景知识。
Dockerfile
# syntax=docker/dockerfile:1
ARG hfs_version=latest
FROM alpine AS build
WORKDIR src
ARG hfs_version
ENV HFS_VERSION=${hfs_version}
RUN apk --update --no-cache add \
wget \
zip
RUN wget https://github.com/rejetto/hfs/releases/download/${HFS_VERSION}/hfs-linux.zip \
&& unzip hfs-linux.zip
FROM node:20
WORKDIR hfs
COPY --from=build src/hfs ./hfs
COPY ./entrypoint.sh ./entrypoint.sh
RUN chmod +x ./hfs
ENTRYPOINT ["/bin/bash", "entrypoint.sh"]
入口点就是这样
#!/bin/bash
env | grep HFS
./hfs
当我构建并运行它时,使用:
docker build --build-arg hfs_version=v0.50.5 -t gbrlfrc/hfs:1.0.0 -f hfs.Dockerfile .
docker run -it --rm -e HFS_PORT=3838 -p 0.0.0.0:80:3838 --name hfs gbrlfrc/hfs:1.0.0
Docker 在所有 0.0.0.0 上正确公开端口 80,在内部绑定到 3838,但二进制文件开始使用默认端口 80,即使我在 docker run 中指定 HFS_PORT=3838 作为 env(输出如下)
# this is coming from my 'env | grep HFS' in the entrypoint
HFS_PORT=3838
HFS ~ HTTP File Server - Copyright 2021-2023, Massimo Melina <[email protected]>
License https://www.gnu.org/licenses/gpl-3.0.txt
started 12/30/2023, 3:06:31 PM
version 0.50.5
build 2023-12-28T14:52:48.139Z
cwd /root/.hfs
node v18.5.0
platform linux
pid 9
config config.yaml
No config file, using defaults
HINT: type "help" for help
# This is the output from the binary telling me that the port is the default one
http serving on any network : 80
serving on http://172.17.0.2
cannot launch browser on this machine >PLEASE< open your browser and reach one of these (you may need a different address)
- http://172.17.0.2/~/admin/
HINT: you can enter command: create-admin YOUR_PASSWORD
~$ d exec -it hfs bash
root@be778aabb5c9:/hfs# ./hfs
HFS ~ HTTP File Server - Copyright 2021-2023, Massimo Melina <[email protected]>
License https://www.gnu.org/licenses/gpl-3.0.txt
started 12/30/2023, 3:10:42 PM
version 0.50.5
build 2023-12-28T14:52:48.139Z
cwd /root/.hfs
node v18.5.0
platform linux
pid 33
config config.yaml
HINT: type "help" for help
# As you can see now the port is the correct one
http serving on any network : 3838
serving on http://172.17.0.2:3838
cannot launch browser on this machine >PLEASE< open your browser and reach one of these (you may need a different address)
- http://172.17.0.2:3838/~/admin/
HINT: you can enter command: create-admin YOUR_PASSWORD
我在尝试直接在 Dockerfile 中作为入口点或 cmd 运行二进制文件时遇到了同样的问题,而不使用 bash 脚本。像这样:
ENTRYPOINT ["./hfs"]
我还尝试在入口点脚本中获取包含 env 的文件,但结果是相同的。 有人有任何提示吗?这个问题让我发疯。
我不是 Docker 专家,但我可以建议作为一种解决方法,将设置作为参数而不是 envs 传递,如果您可以接受的话,因为 HFS 支持这一点,甚至优先于 envs。
所以它会是--端口3838