Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unset environment variable in compose

I have a Dockerfile that requires two environment variables:

ARG AWS_ACCESS_KEY_ID
ARG AWS_SECRET_ACCESS_KEY

I'm passing them from the host through my compose file using:

build:
  # ...
  args:
    - AWS_ACCESS_KEY_ID
    - AWS_SECRET_ACCESS_KEY

My problem is that in some flows there is an IAM role set (and no env vars) and I don't want to use the environment variables. But even when they don't exist on the host they seem to be set to empty strings during the build process.

I've tried this:

run if [ -z "$AWS_ACCESS_KEY_ID" ]; then unset AWS_ACCESS_KEY_ID; fi
run if [ -z "$AWS_SECRET_ACCESS_KEY" ]; then unset AWS_SECRET_ACCESS_KEY; fi

run env # see if set

But it doesn't work (the variables are still set even if not set in host env).

I'd welcome another solution on mixing env vars and IAM roles when building dockers.

like image 250
Reut Sharabani Avatar asked Oct 09 '18 07:10

Reut Sharabani


People also ask

How do I unset an environment variable?

Delete Environment Variables You need to just use the unset command with the variable name to delete it. This command will remove the variable permanently.

Can I use environment variables in Docker compose?

Set environment variables with 'docker compose run' When you set the same environment variable in multiple files, here's the priority used by Compose to choose which value to use: Compose file. Shell environment variables. Environment file.

How pass env variable in Docker compose command?

Set environment variable with the --env-file flag If you want to pass a file containing all of your environment variables to a Docker container, you can use the --env-file flag. The --env-file flag allows you to pass a file containing environment variables to a Docker container.


1 Answers

Different run in Dockerfile not impact each other, to make your aims, suggest to combine them to one run, something likes follows, FYI:

run if [ -z "$AWS_ACCESS_KEY_ID" ]; then unset AWS_ACCESS_KEY_ID; fi && \
  if [ -z "$AWS_SECRET_ACCESS_KEY" ]; then unset AWS_SECRET_ACCESS_KEY; fi && \
  env

Then, you will find no AWS_ACCESS_KEY_ID was set to empty value in env.

And, when try, suggest use docker-compose build --no-cache to test.

Finally, why you see empty value?

I made a experiment, seems if no ENV set in Dockerfile, meanwhile, no env for this variable set in HOST, the ARG in Dockerfile will automatically be changed to one ENV variable when docker build, as the ARG did not set a value, so it's empty.

like image 69
atline Avatar answered Oct 01 '22 02:10

atline