BettaFish icon indicating copy to clipboard operation
BettaFish copied to clipboard

[Bug][docker] .env file PORT unused in docker image settings

Open edwardzcn-decade opened this issue 1 month ago • 12 comments

Description

Following the quick start steps in README, copy the .env file and modify PORT=5060 (since local port 5000 is already occupied).

However, after running docker compose up -d, BettaFish container still exposes port 5000. Does the .env file work for docker (I guess so because it cant set up without touch this file)? Here is the log:

2025-11-10 02:40:43.292 | INFO     | __main__:<module>:1036 - Flask服务器已启动,访问地址: [http://0.0.0.0:5000⁠](http://0.0.0.0:5000/)

Expected behavior

Container should respect .env → PORT value and expose the configured port (e.g., 5060).

Actual behavior

Container continues to expose default port 5000, ignoring .env override PORT=5060.

I will try building it locally.

问题描述

复制 .env 文件并修改其中的 PORT=5060(因为本地的 5000 端口常被占用)。

执行 docker compose up -d 后,BettaFish 容器仍然暴露的是 5000 端口。.env 文件是否对 Docker 生效?(我感觉是的,缺失它容器无法正常启动)。以下是日志输出:

2025-11-10 02:40:43.292 | INFO     | __main__:<module>:1036 - Flask服务器已启动,访问地址: [http://0.0.0.0:5000⁠](http://0.0.0.0:5000/)

预期行为

容器应当读取 .env 文件中的 PORT 参数,并按照配置的端口(例如 5060)进行暴露。

实际行为

容器仍然暴露默认的 5000 端口,忽略了 .env 中的 PORT=5060 配置。

edwardzcn-decade avatar Nov 10 '25 02:11 edwardzcn-decade

Hi @edwardzcn-decade! I'm Dosu and I’m helping the BettaFish team.

The reason changing PORT in your .env file doesn't affect the port exposed by Docker is because the docker-compose.yml file hardcodes the port mapping as 5000:5000 for the BettaFish service, and does not reference the PORT variable from .env. This means Docker will always map host port 5000 to container port 5000, regardless of your .env settings. The Flask app inside the container does read the PORT from .env and will bind to it, but unless the Docker Compose file also uses that variable, the external mapping won't change (source).

To make the container respect your .env PORT value, update the ports section in docker-compose.yml like this:

ports:
  - "${PORT:-5000}:5000"

This way, when you set PORT=5060 in your .env, Docker will map host port 5060 to container port 5000. If you want the container to also listen on a different internal port, make sure both sides of the mapping use the variable:

ports:
  - "${PORT:-5000}:${PORT:-5000}"

And ensure your Flask app is configured to bind to that port (which it already does via .env).

For reference, the database service in the same file already uses this pattern for its port mapping (source).

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

dosubot[bot] avatar Nov 10 '25 02:11 dosubot[bot]

Ofc I manually change the ports forwarding as 5060:5000 and it works. But that means we dont need a .env file with hardcoded environment settings.

edwardzcn-decade avatar Nov 10 '25 03:11 edwardzcn-decade

容器的端口暴露行为是docker配置控制的,env控制的是容器内如何暴露端口。

@dosu 是否如此。英文回复。

DoiiarX avatar Nov 10 '25 03:11 DoiiarX

Yes, that's correct. Docker configuration (such as the ports section in docker-compose.yml or the -p flag in docker run) controls which host port is exposed and mapped to the container. Environment variables (like PORT in a .env file) control which port the application inside the container listens on. Changing the .env PORT only affects the internal application, not the Docker host port mapping, unless the Docker configuration is also updated to use that variable. This separation is confirmed by both user experience and maintainer comments in BettaFish discussions (example).

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

dosubot[bot] avatar Nov 10 '25 03:11 dosubot[bot]

But the .env file does not work as expected in docker. E.g. change the PORT=5000 (by default) to PORT=15080, you can change the ports forwading in docker-compose.yml at the same time btw. Use docker compose up -d to regenerate containers. The log inside container shows the PORT remains 5000.

666ghj/bettafish:latest
15060:15080⁠
...

The log in bettafish:

2025-11-10 03:19:07.161 | INFO     | __main__:<module>:1036 - Flask服务器已启动,访问地址: [http://0.0.0.0:5000⁠](http://0.0.0.0:5000/)

edwardzcn-decade avatar Nov 10 '25 03:11 edwardzcn-decade

但是该.env文件在 Docker 中无法正常工作。例如,将PORT=5000(默认值)更改为PORT=15080,同时也在 docker-compose.yml 中更改端口转发。使用docker compose up -d重新生成容器。容器内部日志显示端口仍然是 5000。

666ghj/bettafish:latest 15060:15080⁠ ... 斗鱼日志:

2025-11-10 03:19:07.161 | INFO | main::1036 - Flask服务器已启动,访问地址: http://0.0.0.0:5000⁠

我也遇到了类似问题@dosu

tommywangf-sketch avatar Nov 10 '25 12:11 tommywangf-sketch

同样的问题。除了端口,在env文件中配置的大模型API、搜索API也都没有生效。感觉容器并未加载这个env文件.

gqchen-dz avatar Nov 11 '25 02:11 gqchen-dz

同 API配置没有生效

CaptainUnhappy avatar Nov 11 '25 02:11 CaptainUnhappy

修改docker-compose.yml 为 bettafish 服务添加 env_file 指令

...
env_file:
  - .env
...

已修复 #304

CaptainUnhappy avatar Nov 11 '25 03:11 CaptainUnhappy

Hi guys, anything new about this?

edwardzcn-decade avatar Nov 13 '25 02:11 edwardzcn-decade

Hi guys, anything new about this?

you can try this one to fix 322 Please run: docker-compose up -d --force-recreate to apply it.

CaptainUnhappy avatar Nov 13 '25 02:11 CaptainUnhappy

#322 add mapping ./.env:/app/.env, and it indeed solved the issue of initializing env configurations using the image.

There are two remaining concerns — correct me if I’m wrong:

  1. Any changes made to the .env file (e.g. from the website) inside the container will be exposed to (and overwritten into) the .env file located in the same directory as docker-compose.yml on the host machine. This feels odd, and I would prefer a one-time environment parameter, then the container can modify and persist its own configuration internally. The db section in docker-compose.yml is using env_file to pass such parameters.
  2. The container image does not support MySQL (see image: postgres:15).

edwardzcn-decade avatar Nov 13 '25 09:11 edwardzcn-decade

Fixed by #340

edwardzcn-decade avatar Nov 15 '25 15:11 edwardzcn-decade