Workaround to docker run “--env-file” supplied file not being evaluated as expected

后端 未结 6 1312
你的背包
你的背包 2021-02-05 23:58

My current setup for running a docker container is on the lines of this:

  1. I\'ve got a main.env file:
# Main
export PRIVATE_IP=\\`echo          


        
相关标签:
6条回答
  • 2021-02-06 00:03

    I had a very similar problem to this. If I passed the contents of the env file to docker as separate -e directives then everything ran fine however if I passed the file using --env-file the container failed to run properly.

    Turns out there were some spurious line endings in the file (I had copied from windows and ran docker in Ubuntu). When I removed them the container ran the same with --env or --env-file.

    0 讨论(0)
  • 2021-02-06 00:06

    Both --env and --env-file setup variables as is and do not replace nested variables.

    Solomon Hykes talks about configuring containers at run time and the the various approaches. The one that should work for you is to volume mounting the main.env from host into the container and sourcing it.

    0 讨论(0)
  • 2021-02-06 00:06
    • Ceate a .env file
     example: test=123 val=Guru
    
    • Execute command

    docker run -it --env-file=.env bash

    • Inside the bash verify using

    echo $test (should print 123)

    0 讨论(0)
  • 2021-02-06 00:13

    creating an ENV file that is nothing more than key/value pairs can be processed in normal shell commands and appended to the environment. Look at the bash -a pragma.

    0 讨论(0)
  • 2021-02-06 00:20

    What you can do is create a startup script that can be run when the container starts. So if your current docker file looks something like this

    From ...
    ...
    CMD command
    

    Change it to

    From ...
    ...
    ADD start.sh start.sh
    CMD ["start.sh"]
    

    In your start.sh script do the following:

    export PRIVATE_IP=\`echo localhost\`
    export MONGODB_HOST="$PRIVATE_IP"
    export MONGODB_URL="mongodb://$MONGODB_HOST:27017/development"
    command
    
    0 讨论(0)
  • 2021-02-06 00:23

    I had this issue when using docker run in a separate run script run.sh file, since I wanted the credentials ADMIN_USER and ADMIN_PASSWORD to be accessible in the container, but not show up in the command.

    Following the other answers and passing a separate environment file with --env or --env-file didn't work for my image (though it worked for the Bash image). What worked was creating a separate env file...

    # env.list
    ADMIN_USER='username'
    ADMIN_PASSWORD='password'
    

    ...and sourcing it in the run script when launching the container:

    # run.sh
    source env.list
    docker run -d \
        -e ADMIN_USER=$INFLUXDB_ADMIN_USER \
        -e ADMIN_PASSWORD=$INFLUXDB_ADMIN_PASSWORD \
        image_repo/name:tag
    
    0 讨论(0)
提交回复
热议问题