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?
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.
You can use docker run --env-file [path-toenv-file] to provide the environment variables to the container from a . env file.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With