Home > Enterprise >  need help looking for environment variables generated by Dockerfile
need help looking for environment variables generated by Dockerfile

Time:07-23

I am running a Docker source codes which has a Dockerfile that looks like this:

FROM node:14-slim as build

# install node-gyp dependencies
RUN apt-get update && apt-get upgrade -y && apt-get install -y --no-install-recommends apt-utils cron g   make python

# add pin script
WORKDIR /
ADD scripts/pinVersions.js scripts/cleanup.sh ./
RUN chmod  x /cleanup.sh

# build server
WORKDIR /app
ADD packages/server .
RUN node /pinVersions.js && yarn && yarn build && /cleanup.sh

# build worker
WORKDIR /worker
ADD packages/worker .
RUN node /pinVersions.js && yarn && yarn build && /cleanup.sh

FROM couchdb:3.2.1
# TARGETARCH can be amd64 or arm e.g. docker build --build-arg TARGETARCH=amd64
ARG TARGETARCH amd64
#TARGETBUILD can be set to single (for single docker image) or aas (for azure app service)
# e.g. docker build --build-arg TARGETBUILD=aas ....
ARG TARGETBUILD single
ENV TARGETBUILD $TARGETBUILD

COPY --from=build /app /app
COPY --from=build /worker /worker

ENV \
  APP_PORT=4001 \
  ARCHITECTURE=amd \
  BUDIBASE_ENVIRONMENT=PRODUCTION \
  CLUSTER_PORT=80 \
  # CUSTOM_DOMAIN=budi001.custom.com \
  DEPLOYMENT_ENVIRONMENT=docker \
  MINIO_URL=http://localhost:9000 \
  POSTHOG_TOKEN=phc_fg5I3nDOf6oJVMHSaycEhpPdlgS8rzXG2r6F2IpxCHS \
  REDIS_URL=localhost:6379 \
  SELF_HOSTED=1 \
  TARGETBUILD=$TARGETBUILD \
  WORKER_PORT=4002 \
  WORKER_URL=http://localhost:4002 \
  APPS_URL=http://localhost:4001

# These secret env variables are generated by the runner at startup
# their values can be overriden by the user, they will be written
# to the .env file in the /data directory for use later on
#  REDIS_PASSWORD=something\
#  COUCHDB_PASSWORD=something \
#  COUCHDB_USER=something \
#  COUCH_DB_URL=http://something@localhost:5984 \
#  INTERNAL_API_KEY=something \
#  JWT_SECRET=something \
#  MINIO_ACCESS_KEY=something \
#  MINIO_SECRET_KEY=something \

There is a runner.sh that looks like this:

#!/bin/bash
declare -a ENV_VARS=("COUCHDB_USER" "COUCHDB_PASSWORD" "MINIO_ACCESS_KEY" "MINIO_SECRET_KEY" "INTERNAL_API_KEY" "JWT_SECRET" "REDIS_PASSWORD")
if [ -f "/data/.env" ]; then
    export $(cat /data/.env | xargs)
fi
# first randomise any unset environment variables
for ENV_VAR in "${ENV_VARS[@]}"
do
    temp=$(eval "echo \$$ENV_VAR")
    if [[ -z "${temp}" ]]; then
        eval "export $ENV_VAR=$(uuidgen | sed -e 's/-//g')"
    fi
done
if [[ -z "${COUCH_DB_URL}" ]]; then
    export COUCH_DB_URL=http://$COUCHDB_USER:$COUCHDB_PASSWORD@localhost:5984
fi
if [ ! -f "/data/.env" ]; then
    touch /data/.env
    for ENV_VAR in "${ENV_VARS[@]}"
    do
        temp=$(eval "echo \$$ENV_VAR")
        echo "$ENV_VAR=$temp" >> /data/.env
    done
fi

# make these directories in runner, incase of mount
mkdir -p /data/couch/dbs /data/couch/views
chown couchdb:couchdb /data/couch /data/couch/dbs /data/couch/views
redis-server --requirepass $REDIS_PASSWORD &
/opt/clouseau/bin/clouseau &
/minio/minio server /data/minio &
/docker-entrypoint.sh /opt/couchdb/bin/couchdb &
/etc/init.d/nginx restart
if [[ ! -z "${CUSTOM_DOMAIN}" ]]; then
    # Add monthly cron job to renew certbot certificate
    echo -n "* * 2 * * root exec /app/letsencrypt/certificate-renew.sh ${CUSTOM_DOMAIN}" >> /etc/cron.d/certificate-renew
    chmod  x /etc/cron.d/certificate-renew
    # Request the certbot certificate
    /app/letsencrypt/certificate-request.sh ${CUSTOM_DOMAIN}
fi

/etc/init.d/nginx restart
pushd app
pm2 start --name app "yarn run:docker"
popd
pushd worker
pm2 start --name worker "yarn run:docker"
popd
sleep 10
curl -X PUT ${COUCH_DB_URL}/_users
curl -X PUT ${COUCH_DB_URL}/_replicator
sleep infinity

Now I want to know what is the value of the COUCHDB_USER and COUCHDB_PASSWORD which is supposed to be randomly generated.

  • I tried docker exec -it containername /bin/bash and printenv, it is not there.
  • I tried docker exec -it containername /bin/bash and look for the /data folder, couldnt see any .env file

Can any docker expert advise where will I find the environment variables?

CodePudding user response:

This isn't really a Docker question: nothing that is happening here is docker-specific.


The environment variables are set in runner.sh. There are two places those variables are set:

  1. If /data/.env exists at the beginning of the script, those variables are exported to the environment:

    if [ -f "/data/.env" ]; then
        export $(cat /data/.env | xargs)
    fi
    
  2. Variables in the list ENV_VARS that weren't previously set are set to random values, which are then exported to the environment:

    for ENV_VAR in "${ENV_VARS[@]}"
    do
       temp=$(eval "echo \$$ENV_VAR")
       if [[ -z "${temp}" ]]; then
           eval "export $ENV_VAR=$(uuidgen | sed -e 's/-//g')"
       fi
    done
    

You won't see them in docker exec -it containername /bin/bash because that shell isn't a child of runner.sh, so of course it won't see environment variables set by that script.

It looks as if runner.sh should create /data/.env unconditionally, so it's not clear why you're not seeing it. Note that you won't see .env with a simple ls; you would need to run ls -A /.data, for example.


Update

If I follow the image build instructions and then start the image (docker run --name budibase budibase:latest), then when I docker exec into the resulting container I see /data/.env:

$ docker exec budibase ls -A /data
.env
couch
minio
  • Related