Binary file not getting env if executed as entrypoint in Dockerfile

44 Views Asked by At

So, Im dockerizing an open source project, HFS by rejetto on GitHub. Basically you can configure it by environmental variables, using the form HFS_= (eg. HFS_PORT=80).
What Im trying to do is to pass these envs through Docker at run time, but something weird is happening: this binary is getting the envs only if manually executed from within the container, if automatically runed by docker in the entry point it ignores them, even if in the same entrypoint script I can see them properly setted.

A bit of context.

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

The entrypoint is jsut

#!/bin/bash

env | grep HFS
./hfs

When I build and run it, with:

  • 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 properly expose port 80, bound to 3838 internally, on all 0.0.0.0, but the binary file start using default port 80, even if I specified HFS_PORT=3838 as env in docker run (output below)

# 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

Now, if this was the only problem, I would have thought that it was the binary that didn't read the envs correctly, but if I exec in the same running container, and run the binary manually:
~$ 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

I got the same issue trying to run the binary directly in the Dockerfile as entrypoint or cmd, without using a bash script. Like this:

ENTRYPOINT ["./hfs"]

I also tryed to source a file containing the env in the entrypoint script, but the results is the same. does anyone have any hints? This issue is driving me crazy.

1

There are 1 best solutions below

0
On

I'm no docker expert, but I can suggest as a workaround to pass settings as parameters instead of envs, if that's acceptable for you, since HFS supports that and even gives priority over envs.

So it would be --port 3838