Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass through environment variables in docker run through an env file?

Given this Dockerfile:

FROM alpine:3.7
ENV LAST_UPDATED=2018-02-22
ARG XDG_CACHE_HOME=/tmp/cache/
RUN apk update && \
    apk add libxslt && \
    apk add sed && \
    apk add py-pip && \
    apk add mariadb-client && \
    apk add bash bash-doc bash-completion && \
    pip install httpie && \
    rm -rf /var/cache/apk/*
WORKDIR /usr/deleter/
COPY delete.sh ./
ENTRYPOINT ["/usr/deleter/delete.sh"]

I expected to be able to pass multiple variables through an .env file with the key=value format.

$ cat stage.env 
MYSQL_DATABASE=database
MYSQL_HOST=127.0.0.1:3306
MYSQL_PASSWORD=password
MYSQL_PORT=3306
MYSQL_USER=a_user

My delete.sh only looks like this:

#!/bin/bash
set -e
set -o pipefail
echo "hello world"
echo ${MYSQL_DATABASE} ${MYSQL_HOST} ${MYSQL_PASSWORD} ${MYSQL_PORT} ${MYSQL_USER}
echo "ALL VARIABLES"
env

I expected to see the env variables, yet they all are empty. The --env-file options seems to be not working. The output of the script is:

hello world

ALL VARIABLES
HOSTNAME=f52c5c2aa22b
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/usr/deleter
LAST_UPDATED=2018-02-22
SHLVL=1
HOME=/root
_=/usr/bin/env

I create and run the docker container via:

docker build -t deleter:local
docker run deleter:local --env-file stage.env

I tried --env-file stage.env, --env-file=stage.env, --env-file ./stage.env, yet I don't see anything being included nor any thrown error. I also tried it with the absolute path.

The stage.env is on the same level as my Dockerfile.

The env file is valid, I can source it on my local machine an access the variables there.

Where is my mistake?

like image 580
k0pernikus Avatar asked Feb 22 '18 15:02

k0pernikus


People also ask

How do I pass an environment variable in Docker run?

Using –env, -e When we launch our Docker container, we can pass environment variables as key-value pairs directly into the command line using the parameter –env (or its short form -e). As can be seen, the Docker container correctly interprets the variable VARIABLE1.

Can you use an env file in a Dockerfile?

You can use docker run --env-file [path-toenv-file] to provide the environment variables to the container from a . env file.

How do I pass a variable in Dockerfile?

Predominantly, there are three different ways through which we can pass environment variables to our Docker containers. These are by using the -e, --env-file, and the ENV instruction inside the Dockerfile.

How do you pass an environment variable?

Environment variables can be used to pass configuration to an application when it is run. This is done by adding the definition of the environment variable to the deployment configuration for the application. To add a new environment variable use the oc set env command.


1 Answers

Keep in mind that the docker run argument order is mandatory:

$ docker help run 
Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

The environment setting fall under options:

 -e, --env list                       Set environment variables
     --env-file list                  Read in a file of environment 

Hence:

docker run --env-file stage.env deleter:local

will import the environment variables as expected.

like image 191
k0pernikus Avatar answered Oct 22 '22 21:10

k0pernikus