docker run using config-map variables

9/6/2019

I have a .bat file right now that runs a docker command with

docker run --rm -it --env-file .env --name my-name my-name /bin/sh -c "mvn test -Dtest=my.runner.Runner"

I have a k8s config-map.yml and a deployment.yml with a configMapRef to replace the .env file, but I don’t know how to reference it in my docker run command.

It should work that every time I deploy the image from my docker registry , it picks up the config-map and uses that for the repos envs.

-- user6754289
docker
environment-variables
kubernetes
maven

1 Answer

9/11/2019

If you try to set the value of an environment variable from inside a RUN statement like RUN export VARI=5 && ..., you won’t have access to it in any of the next RUN statements. The reason for this, is that for each RUN statement, a new container is launched from an intermediate image. An image is saved by the end of the command, but environment variables do not persist that way.

So firstly you should make changes in your image, build it and then change its reference in deployment.yaml. After wole process just execute configmap and deployment. Now everything should works fine. Trying to combine this process (docker run)with Kubernetes components it is not good approach.

Useful article: docker-arg-env.

Overall documentation: docker-run.

-- MaggieO
Source: StackOverflow