简体   繁体   中英

Setting Docker ENV using python -c “command”

The python modules that I downloaded are inside the user's home directory. I need to set the python path to the user's bin profile. I tried two approaches as shown below in my dockerfile but to no avail. When I check the environment variable in the running container for the first case the PY_USER BIN is $(python -c 'import site; print(site.USER_BASE + "/bin")') and for the second case the PY_USER_BIN is blank. However, when I manually try to export the PY_USER_BIN variable, it works.

ENV PY_USER_BIN $(python -c 'import site; print(site.USER_BASE + "/bin")')

ENV PATH $PY_USER_BIN:$PATH

and

RUN export PY_USER_BIN=$(python -c 'import site; print(site.USER_BASE + "/bin")')

ENV PATH $PY_USER_BIN:$PATH

To me you mix different context of execution.

The ENV command that you use is a Dockerfile command, it is for env variable in the context of docker that would be forwarded to the container.

The RUN command execute a command inside the container, here, export . Whatever is done inside the container stay inside the container and docker will not have access to it.

For me there no point to give as docker ENV variable where python is on the host has they don't share the same file system. If you need to do it on the container context, then run these command inside the container context with standard shell commands.

Try that first by connecting to your container and running a shell inside it, once the commands works, put them in your Dockerfile. That's as simple as that. To do that run it like:

docker run -ti [your container name/tag] [your shell]

if you use sh as shell:

docker run -ti [your container name/tag] sh

Then try your commands.

To me it seems the commands you want would look like that:

RUN export PY_USER_BIN=$(python -c 'import site; print(site.USER_BASE + "/bin")')
RUN export PATH=$PY_USER_BIN:$PATH

Anyway the point of a container is to have a fixed file system, fixed user names and all. So the USER_BIN shall be always in the same path inside the container in 99% case you could has well hardcode it.

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