0

I have python project and trying to dockerize it. The usecase is as below User create a parent directory eg titanic and checkout the github repo. The project structure becomes like this

  • titanic/
    • src/
    • Dockerfile
    • docker-compose.yml
    • .env

Now what I need is, to pass this parent directory name to docker's WORKDIR Command to create docker image.

In Dockerfile I have, WORKDIR $PROJ_WORKDIR

I tried many different ways to pass the directory name, but it always fails

1.    environment:
      - PROJ_WORKDIR=titanic
2. In .env, PROJ_WORKDIR=titanic
3. In docker-compose, args: 
                    - PROJ_WORKDIR=titanic
4. In Dockerfile, ENV PROJ_WORKDIR=some_default

Is there a way to configure and access it.

winter
  • 467
  • 2
  • 10
  • 1
    Try: `WORKDIR ${PROJ_WORKDIR}` – JonSG Apr 07 '23 at 17:01
  • 1
    Normally your image should only contain one application, and it can't be change after it's built. Using a fixed directory name like `/app` should be fine. I wouldn't try to set this via an environment variable or make it configurable at all. – David Maze Apr 07 '23 at 18:10
  • Hi David, Thanks. I agree with you but I needed it, in perspective of general user usage who have `.env` file to configure the project. – winter Apr 08 '23 at 17:22

1 Answers1

1

Almost there but just one syntax error:

Dockerfile will have entry as follows:

WORKDIR /app/${PROJ_WORKDIR}

Now this PROJ_WORKDIR can be set in numerous ways as pointed by you all the ways are valid.

More read about how to pass value to ARG variable: Pass argument to docker compose

Hope this helps, Thanks.

Suchandra T
  • 569
  • 4
  • 8