finch icon indicating copy to clipboard operation
finch copied to clipboard

The environment variables are not getting pick up in finch compose

Open htquach opened this issue 1 year ago • 5 comments

Describe the bug When define environment variable in docker-compose.yaml file and run with finch compose up, the result shows warning about the variable is not set and defaulting to blank string.

Steps to reproduce Create a docker-compose.yaml with the following content:

version: '3'

services:
  finch_envs:
    image: alpine
    user: "${CUSTOM_UID}:${CUSTOM_GID}"
    entrypoint: sh
    command:
      - -c
      - |
        id -u
        id -u -n
        id -g
        id -g -n

Run CUSTOM_UID=405 CUSTOM_GID=100 finch compose up

Output:

WARN[0000] The "CUSTOM_UID" variable is not set. Defaulting to a blank string.
WARN[0000] The "CUSTOM_GID" variable is not set. Defaulting to a blank string.
INFO[0000] Ensuring image alpine
INFO[0000] Re-creating container finch_sandbox_finch_envs_1
INFO[0000] Attaching to logs
finch_envs_1 |0
finch_envs_1 |root
finch_envs_1 |0
finch_envs_1 |root
INFO[0000] Container "finch_sandbox_finch_envs_1" exited
INFO[0000] All the containers have exited
INFO[0000] Stopping containers (forcibly)
INFO[0000] Stopping container finch_sandbox_finch_envs_1

Expected behavior All environments variables are respected.

INFO[0000] Ensuring image alpine
INFO[0000] Re-creating container finch_sandbox_finch_envs_1
INFO[0000] Attaching to logs
finch_envs_1 |405
finch_envs_1 |guest
finch_envs_1 |100
finch_envs_1 |users
INFO[0000] Container "finch_sandbox_finch_envs_1" exited
INFO[0000] All the containers have exited
INFO[0000] Stopping containers (forcibly)
INFO[0000] Stopping container finch_sandbox_finch_envs_1

htquach avatar Mar 10 '23 23:03 htquach

Can you try to inject env variables is via a .env file in the same path as the docker-compose.yaml file.

$ cat .env                         
CUSTOM_UID=405
CUSTOM_GID=100

I tried a test with the compose file you provided.

$ finch compose  up                
INFO[0000] Ensuring image alpine                        
INFO[0000] Re-creating container compose-tests_finch_envs_1 
INFO[0000] Attaching to logs                            
finch_envs_1 |405
finch_envs_1 |guest
finch_envs_1 |100
finch_envs_1 |users
INFO[0000] Container "compose-tests_finch_envs_1" exited 
INFO[0000] All the containers have exited               
INFO[0000] Stopping containers (forcibly)               
INFO[0000] Stopping container compose-tests_finch_envs_1 

vsiravar avatar Mar 13 '23 01:03 vsiravar

I tried nerdctl, and nerdctl works in this way: CUSTOM_UID=405 CUSTOM_GID=100 nerdctl compose up

KevinLiAWS avatar Mar 13 '23 17:03 KevinLiAWS

Can you try to inject env variables is via a .env file in the same path as the docker-compose.yaml file.

Confirmed that specifying the hard coded environment variables in the .env file works. I'll use this as a work-around. I am expecting setting these system-wide environment variables should be accessible within the docker-compose.yaml file.

The end goal is to have the CUSTOM_UID and CUSTOM_GID to match with the current user UID and GID.

export CUSTOM_UID=$(id -u); export CUSTOM_GID=$(id -g); finch compose up

htquach avatar Mar 14 '23 02:03 htquach

This is related to https://github.com/lima-vm/lima/issues/412 on Lima. Basically the finch vm doesn't have access to the local environment variables from your mac, we'd have to specifically pass the local environment through.

Maybe we can parse docker-compose.yaml and expand the variables as a part of the command that we pass through to the host, e.g.

bernings:~ » export SAM_TEST=foo
bernings:~ » LIMA_HOME=/Applications/Finch/lima/data /Applications/Finch/lima/bin/limactl shell finch SAM_TEST=$SAM_TEST env
SAM_TEST=foo
...

This is similar to the approach that we use for the -e/--env flag for other nerdctl commands: https://github.com/runfinch/finch/pull/158/files

sam-berning avatar Apr 04 '23 23:04 sam-berning

I am also encountering this issue. I can use the .env as a temp workaround - is there any update on whether Finch will be able to support direct env variables via the command line as above?

094459 avatar Jun 28 '24 10:06 094459