如果在Dockerfile中作为入口点执行,则二进制文件不会获得env

w6lpcovy  于 12个月前  发布在  Docker
关注(0)|答案(1)|浏览(92)

所以,我在GitHub上对一个开源项目HFS by rejetto进行了dockerizing。基本上你可以通过环境变量来配置它,使用HFS_=的形式(例如HFS_PORT=80)。
我想做的是在运行时通过Docker传递这些envs,但是奇怪的事情发生了:这个二进制文件只有在从容器内手动执行时才能获得envs,如果由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"]

字符串

入口点是jsut

#!/bin/bash

env | grep HFS
./hfs


当我构建并运行它时,使用:

  • 第一个月
  • 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


现在,如果这是唯一的问题,我会认为是二进制文件没有正确读取env,但如果我在同一个运行容器中执行,并手动运行二进制文件:

~$ 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中直接运行二进制文件作为entrypoint或cmd,而不使用bash脚本,也遇到了同样的问题。像这样:

ENTRYPOINT ["./hfs"]


我也尝试在entrypoint脚本中源一个包含env的文件,但结果是一样的。有人有任何提示吗?这个问题让我抓狂。

vohkndzv

vohkndzv1#

我不是dockerMaven,但我可以建议作为一种变通方法,将设置作为参数而不是envs传递,如果你可以接受的话,因为HFS支持这一点,甚至优先于envs。
所以应该是3838端口

相关问题