How do I dynamically set the working directory at runtime by passing an env variable?

2/4/2020

Here's what my Dockerfile looks like for an image I'm creating.

FROM python:3.7-alpine
COPY requirements.txt /
RUN pip install -r /requirements.txt
ENV U_PATH="a"
WORKDIR $U_PATH

I override the env variable U_PATH when I call it using docker run -it -e U_PATH=/mnt temp:v1 /bin/sh but the WORKDIR is set during build time and I cannot change that during runtime.

Is there any way to dynamically set the working directory at runtime by passing an env variable?

-- him229
docker
kubernetes

1 Answer

2/4/2020

While not an environment variable, don't forget you can alter the working directory of a Pod's container via the workingDir: PodSpec field

containers:
- name: foo
  image: 'temp:v1'
  workingDir: /mnt
-- mdaniel
Source: StackOverflow