Docker compose Invalid volume destination path: '.' mount path must be absolute

I had the same error message, it was a silly mistake. I forgot to put the leading forward slash on container directory /app

Before

volumes:
      - ./api/app:app

After

volumes:
      - ./api/app:/app

This is just not allowed in the Compose file, since you do not have a template engine there.

You will not need to define

volumes: 
        - /opt/h2-data

Since that will be done automatically (anonymous volume). If you want to have a named volume use

volumes: 
        - myname:/opt/h2-data

or a host mount

volumes: 
        - /path/on/the/host:/opt/h2-data

So ${DATA_DIR} is not expanded in the volumes ( from the ENV ) in a compose file. There are dialects like rancher-compose providing this, but in general that is not possible

UPDATED: Updated my answer since I somehow mixed the Dockerfile/docker-compose.yml file. It makes sense in the Dockerfile, since it is just used as a variable. Thank you for hinting me on that @Bmitch (once again)


Sometimes just an extra space can cause the problem (my case): Shouldn't be like that:

volumes:
      - ./api/app: /app

but :

volumes:
      - ./api/app:/app