I want to correctly share directories between host and docker's container where the same UID and GID are used on both systems. For this, I wanted to pass them as variables MY_UID=$(id -u) to the docker.
We are considering case where .env has row with reference to some variable like MY_UID=$(id -u), not row value like MY_UID=1000
Variables are correctly declared, exported as an environment variables and it works on the docker-compose.yml level, but docker-compose do not pass them further to Dockerfile(s).
I've tried so far in docker-compose.yml:
- env_file field
- environment field
together with in bash:
- exporting variables, like:
- cat .env | envsubst | docker-compose -f - ...
- VAR=123 docker-compose ...
or options for docker-compose -e.
The entire route for the given variables is export > docker-compose > dockerfile.
for testing purpose Dockerfile is simple like: (on this level variables doesn't works!)
FROM heroku/heroku:18 AS production RUN useradd -ms /usr/bin/fish -p $(openssl passwd -1 django) --uid "$MY_UID" --gid "$MY_GID" -r and $MY_UID is empty while using docker-compose.
docker-compose.yml (on this level variables works!)
version: '3.7' networks: {} services: django: build: context: ${MY_DIR} dockerfile: ${COMPOSE_DIR}/django/Dockerfile env_file: - .env environment: - MY_UID: ${MY_UID} volumes: - ${MY_DIR}:/app:rw docker-compose config returns: MY_GID: $$(id -u) for .env: MY_GID=$(id -u)
I wanted to avoid the workaround like this:
source .env && cat template_Dockerfile | envsubst > Dockerfile
or
source .env && cat .env | envsubst > .env_for_dockercompose
Dockerfile. On my systemid -utells me my uid is 501; does this mean I won't be able to use your image if you built it on a system where your host uid is 1000?envsubstrun on the target machine will do a job, except it's complicating a process of building containers.