Imad
Imad

Reputation: 2751

How to define and use environment variables within strings in dockerfile?

Our directory contains dags and plugins subdirectories. We have been trying for an hour to make this Dockerfile work :

...
# Set AIRFLOW_HOME
ENV AIRFLOW_HOME="/root/airflow"

# Create airflow directory to populate it with dags
RUN mkdir -vp $AIRFLOW_HOME
## Import dags
ADD dags '${AIRFLOW_HOME}/dags'
ADD plugins '${AIRFLOW_HOME}/plugins'
...

No matter what we tried, the dags and plugins subdirectories would just not be created under the specified path. Until we did this :

ADD dags ${AIRFLOW_HOME}'/dags'
ADD plugins ${AIRFLOW_HOME}'/plugins'

I.E. Removed the ${AIRFLOW_HOME} from under the string brackets... Why is this behaving this way? Could you provide an explanation of what we're doing wrong?

Upvotes: 0

Views: 533

Answers (1)

Antonio Petricca
Antonio Petricca

Reputation: 11050

The main error is that the ADD source folder must be relative to docker file context path, so you cannot use absolute paths:

The second error is that you cannot pass environment variables to some docker commands, so you have to use ARG.

Here an example:

ARG AIRFLOW_HOME /root/airflow
WORKDIR ${AIRFLOW_HOME}

## Import dags

ADD dags {{dags target folder under WORKDIR}}
ADD plugins {{plugins target folder under WORKDIR}}

ANd remember to place dags and plugins source folder under the docker context path.

Upvotes: 1

Related Questions