Home > Mobile >  Global environment variables in a shell script with Dockerfile
Global environment variables in a shell script with Dockerfile

Time:11-26

I'm trying to create a ENV VAR called SETTINGS in a Python image with Dockerfile. This env var must be dynamic, because I need to change the conf profile betweet production and preproduction.

I've created an script (entrypoint.sh) with two commands, the first one create the SETTINGS VAR and the second one is a python command.

The problem is that the python line works properly but the SETTINGS var doesn't.

Script

#!/bin/bash
profile=$profile
export SETTINGS=/acalls-service/${profile}.cfg
python -m "_acalls_clasiffier" 
exec

Dockerfile

ENTRYPOINT ["/bin/bash", "-c", ". entrypoint.sh"]

I've tried with ["./entrypoint.sh] also but I doesn't work.

CodePudding user response:

This would be a pretty typical use of an entrypoint wrapper script. Remember that a container only runs a single process. If your Dockerfile specifies an ENTRYPOINT, that's the process, and the CMD is passed as additional arguments to it. That lets you set up a script that does some first-time setup and then replaces itself with the real command:

#!/bin/sh

# Set the environment variable
export SETTINGS=/acalls-service/${profile}.cfg

# Run the CMD
exec "$@"

In your Dockerfile you'd specify this script as the ENTRYPOINT and then the actual command as CMD.

ENTRYPOINT ["./entrypoint.sh"]             # must be JSON-array syntax
CMD ["python", "-m", "_acalls_clasiffier"] # shell syntax allowed here too

Since you can easily provide a replacement CMD, you can check that this is working

docker run --rm myimage \
  sh -c 'echo $SETTINGS'  # <-- run in the container, single-quoted so the
                          #     host shell doesn't expand it

If you get a debugging shell in the container with docker exec, it won't see the variable set because it doesn't run through the entrypoint sequence. That's not usually a practical problem. You can always docker run a new container with an interactive shell instead of the Python script.

CodePudding user response:

There is a difference in passing a dynamic environment var on docker build or pass the variable on runtime.

Env vars on runtime

If you want to pass it on runtime, it doesn't need to be in your Dockerfile, just pass it when running your container:

docker run my-image -e MY_ENV=foo

Env vars on build

During build you have to:

  • define your env var in your Dockerfile
  • pass the env var into the build process

Example Dockerfile:

  ARG MY_BUILD_ARG=myDefaultArgument

  FROM node:16.9.0

  ARG MY_BUILD_ARG

  ENV MY_PUBLIC_ENV=${MY_BUILD_ARG}

  RUN echo ${MY_PUBLIC_ENV} // => myRandomEnvVar

And then pass it when you build as:

docker build --build-arg MY_BUILD_ARG=myRandomEnvVar
  • Related