TransWikia.com

Docker environment variable not making it through on run command to shell script

Stack Overflow Asked by Dean Hiller on December 18, 2021

I am trying to do exactly as the answers here

How do I use Docker environment variable in ENTRYPOINT array?

but for some reason, it’s not working and here is my deploy.sh script..

#!/bin/bash
#IF ANY command fails, fail the script
set -e
echo "Deploying $@"

This is my Docker file

FROM gcr.io/google.com/cloudsdktool/cloud-sdk:alpine
ENV SERVICE="default"
RUN mkdir -p ./monobuild
COPY . ./monobuild/
WORKDIR "/monobuild"
ENTRYPOINT ./deploy.sh "${SERVICE}"

This is my docker run command where I try to feed in SERVICE. ( I would prefer to fail if there is no SERVICE supplied as well)

docker run gcr.io/orderly-gcp/prod-deploy -e SERVICE=blah

My output however when I run that command is simply

Deploying default

I am unsure why I follow that sample SO post and this is still not working?

3 Answers

There are three ways to pass environment variables to docker


First way

Using -e flag like -e ENV_NAME='ENV_VALUE'

Example with one environment variable

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD='secret' -d mysql:tag

Example with two environment variables

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD='secret' -e MYSQL_DATABASE='mySchema' -d mysql:tag

Example with two environment variables and many options

docker run --name some-mysql -d -t -i -e MYSQL_ROOT_PASSWORD='secret' -e MYSQL_DATABASE='mySchema' mysql:tag

NOTE: You should pass image name mysql:tag after options like -e MYSQL_ROOT_PASSWORD='secret' -e MYSQL_DATABASE='mySchema'


Second way

Using .env file. basicly you will add environment variables to .env file then pass this name to docker run command like docker run --env-file ./.env

Example with one environment variable

Create .env file

MYSQL_ROOT_PASSWORD=secret

Then use it in docker command

docker run --name some-mysql --env-file ./.env -d mysql:tag

Example with two environment variables

Create .env file

MYSQL_ROOT_PASSWORD=secret
MYSQL_DATABASE=mySchema

Then use it in docker command

docker run --name some-mysql --env-file ./.env -d mysql:tag

Example with two environment variables and many options Create .env file

MYSQL_ROOT_PASSWORD=secret
MYSQL_DATABASE=mySchema

Then use it in docker command

docker run --name some-mysql -d -t -i --env-file ./.env mysql:tag

NOTE: You shouldn't add single quote or double quote to the value

NOTE: You should pass image name mysql:tag after options like --env-file ./.env


Third way

Using linux environment variables so first we need to explain how to add linux environment variables. there are two type of it (local, global). for example -e ENV_NAME.

  • Local (per terminal)

To add local environment variables just use $ export MY_NAME='ahmed'. then try to retrive it $ printenv MY_NAME the result will be ahmed.

NOTE: When you use $ export MY_NAME='ahmed' you can use MY_NAME in any command in current terminal. so if you try to use it in anther terminal it will not work.

  • Local (per command)

To add environment variables to work in current command only just use $ MY_NAME='ahmed' my_command. for example $ MY_NAME='ahmed' printenv MY_NAME the result will be ahmed. so if you try to print MY_NAME again it will not work.

  • Global (for all terminals)

To add environment variables to work in all terminals just open ~/.bashrc then add your environment variables like

MY_NAME='ahmed'
ENV_NAME='ENV_VALUE'

Then try to print it using printenv MY_NAME the result will be ahmed.

Let's follow the examples.

Example with one environment variable

export MYSQL_ROOT_PASSWORD='secret'
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD -d mysql:tag

Example with two environment variables

export MYSQL_ROOT_PASSWORD='secret'
export MYSQL_DATABASE='mySchema'
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD -e MYSQL_DATABASE -d mysql:tag

Example with two environment variables and many options

export MYSQL_ROOT_PASSWORD='secret'
export MYSQL_DATABASE='mySchema'
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD -e MYSQL_DATABASE -d -t -i mysql:tag

NOTE: You should pass image name mysql:tag after options like -e MYSQL_ROOT_PASSWORD -e MYSQL_DATABASE.


Demo

Dockerfile

FROM debian

ENTRYPOINT ["printenv", "ENV_NAME"]

Try to use it

$ docker build --tag demo .

$ ENV_NAME='Hello World' docker run -e ENV_NAME demo:latest
$ Hello World

$ docker run -e ENV_NAME='Hello World' demo:latest
$ Hello World

Answered by Ahmed ElMetwally on December 18, 2021

I think problem is with your docker run invocation. It should be like this:

docker run -e SERVICE=blah gcr.io/orderly-gcp/prod-deploy 

From documentation

Command line arguments to docker run will be appended after all elements in an exec form ENTRYPOINT, and will override all elements specified using CMD. This allows arguments to be passed to the entry point, i.e., docker run -d will pass the -d argument to the entry point. You can override the ENTRYPOINT instruction using the docker run --entrypoint flag.

Answered by piotrekkr on December 18, 2021

The way you are using run command is causing the trouble.

-e is an option and the way the command should be used is

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

meaning that the -e comes before the image name

Answered by Matus Dubrava on December 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP