简体   繁体   中英

Adding a local system variable, or the result of a command, to a dockerfile

I have seen some similar questions, but none of them appear to solve my problem. I want to add a user to a docker container and in my Dockerfile, I define the username with:

ARG USERNAME="some_user"

Instead, I want the username to be the current user's computer username, as obtained by running the command whoami in the local terminal.

So what I would like to have is something like

ARG USERNAME=$(whoami) .

This $(whoami) should be obtained from the local system environment, and not from the docker container. Is there a way to do this for dockerfiles? I have thought of .env and docker-compose solutions but these also require each user to set their own username according to my knowledge.

There is no integrated way to execute arbitrary commands on the host directly outside of a container using just docker build / docker-compose build .

So to execute an arbitrary command to get/generate the required information you'll need to provide a custom script / use another build system to call docker / docker-compose with the respective flags or maybe generate the .env file from a template / interactively.

If you only need the current user name you may want to use the $USER / $LOGNAME environment variables that are set by the system in many default configurations. But since these are just normal environment variables their values may be incorrect / empty / manually changed by the user, see this question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM