'Append python path environment variable in docker-compose instead of dockerfile

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?



Solution 1:[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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1