skip to Main Content

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?

2

Answers


  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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search