Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

docker

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?

like image 481
Dean Hiller Avatar asked Jun 19 '26 03:06

Dean Hiller


1 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
like image 85
Ahmed ElMetwally Avatar answered Jun 22 '26 02:06

Ahmed ElMetwally



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!