简体   繁体   中英

docker-compose can't set env values automatically

There is a start.sh script under the application:

scripts/start.sh

#!/bin/bash

source /env/set.sh

env/set.sh content:

#!/bin/bash
export DB_USERNAME=a
export DB_PASSWORD=b

docker-compose.yml

version: '3.4'
services:
  web:
    build: .
    expose:
      - "5000"
    command: scripts/start.sh
    tty: true
    stdin_open: true

After run docker-compose build && docker-compose up , login into the container, the env values had not been set. But should run source /env/set.sh manually. Why didn't command: scripts/start.sh work?

Let's assume you need to set it after the container has started and can't use the docker-compose environment variables.

You are sourcing the script that exports all the variables needed but then you close this bash session, what you need is to make the exports permanent. I described a way to do it in a similar question here .

What you need is an entrypoint:

#!/bin/bash

# if env variable is not set, set it
if [ -z $DB_USERNAME ];
then
    # env variable is not set
    export DB_USERNAME=username;
fi

# pass the arguments received by the entrypoint.sh
# to /bin/bash with command (-c) option
/bin/bash -c $@

Then add this script as entrypoint in your docker-compose file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM