0

I use the AWS Lambda docker image to develop and do some test on my local host or during CI/CD. On my Docker file I added ENV PYTHONPATH "${PYTHONPATH}:/var/task" to bind /var/task where my python libraries are installed.

I would to do the same but without add ENV PYTHONPATH "${PYTHONPATH}:/var/task" in my Dockerfile.

I tried to add this line in my docker-compose but my python path wasn't updated.

    environment:
      - PYTHONPATH="${PYTHONPATH}:/var/task"

What did I do wrong?

Howins
  • 487
  • 1
  • 6
  • 18
  • Just changing a file won't change environment variables, but I don't think that's your problem. So, please elaborate a bit: What did you do? What did you observe? What did you expect to observe instead and why? Basically, extract and provide a [mcve]. – Ulrich Eckhardt Mar 10 '22 at 17:53
  • Why would you want to remove that setting from the Dockerfile? It seems like something you'd always need every time you run the container, no matter the context, and so it would be built into the image. – David Maze Mar 10 '22 at 18:18
  • This setting is just used during the CI/CD to test my code inside the container Then it will be deploy on AWS, I don't want to introduce bugs during the run of my lambda so I prefer to just append the python path during the test (with a `docker-compose` or `docker exec`) but maybe I will add the line to the dockerfile, it won't have a real impact on my lambda function I guess – Howins Mar 10 '22 at 18:23

2 Answers2

1

Solution 1 - You can do with .env file

Example:

  $ cat .env
    TAG=v1.5
  $ cat docker-compose.yml
    version: '3'
    services:
      web:
        image: "webapp:${TAG}"

Note 1: Starting with +v1.28, .env file is placed at the base of the project directory.

Note 2: Ideally .env file will be in the same dir where docker-compose at. But you can customize as per you need.

Testing before creating a Image - You can verify this with the config command,

$ docker-compose config

version: '3'
services:
  web:
    image: "webapp:v1.5"

Solution 2 - ARG directive in your Dockerfile

Follow https://stackoverflow.com/a/34600106/3098330

Gupta
  • 8,882
  • 4
  • 49
  • 59
  • Thank you for your answer but I would find a solution without create a new file. – Howins Mar 10 '22 at 17:32
  • 1
    ARG directive is interesting indeed but it is not the best way in my scenario I tried to add `docker exec generator export PYTHONPATH="$PYTHONPATH:/var/task"` to my CI script but I encounter this error: `OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "export": executable file not found in $PATH: unknown` – Howins Mar 10 '22 at 18:15
0

If you use ${VARIABLE} syntax, Compose will do variable substitution.

To prevent Compose interpolation, you can use the $$ to escape $

    environment:
      - PYTHONPATH="$${PYTHONPATH}:/var/task"

see: https://docs.docker.com/compose/compose-file/compose-file-v3/#variable-substitution

zion
  • 36
  • 3