5

I'm having trouble with Docker creating a container that does not have environment variables set that I know I set in the image definition.

I have created a Dockerfile that generates an image of OpenSuse 42.3. I need to have some environment variables set up in the image so that anyone that starts a container from the image can use a code that I've compiled and placed in the image.

I have created a shell file called "image_env_setup.sh" that contains the necessary environment variable definitions. I also manually added those environment variable definitions to the Dockerfile.

USER codeUser
COPY ./docker/image_env_setup.sh /opt/MyCode

ENV PATH="$PATH":"/opt/MyCode/bin:/usr/lib64/mpi/gcc/openmpi/bin"
ENV LD_LIBRARY_PATH="/usr/lib64:/opt/MyCode/lib:"
ENV PS1="[\u@docker: \w]\$ "
ENV TERM="xterm-256color"
ENV GREP_OPTIONS="--color=auto"
ENV EDITOR=/usr/bin/vim

USER root
RUN chmod +x  /opt/MyCode/image_env_setup.sh
USER codeUser
RUN /opt/MyCode/image_env_setup.sh
RUN /bin/bash -c "source /opt/MyCode/image_env_setup.sh"

The command that I use to create the container is:

docker run  -it -d --name ${containerName}  -u $userID:$groupID         \
            -e USER=$USER --workdir="/home/codeUser"            \
            --volume="${home}:/home/codeUser" ${imageName} /bin/bash  \

The only thing that works is to pass the shell file to be run again when the container starts up.

docker start $MyImageTag
docker exec -it $MyImageTag /bin/bash --rcfile /opt/MyCode/image_env_setup.sh

I didn't think it would be that difficult to just have the shell variables setup within the container so that any entry into it would provide a user with them already defined.

wandadars
  • 1,113
  • 4
  • 19
  • 37
  • 1
    `alias` is a shell command; it has nothing to do with the environment which `ENV` modifies. – chepner Oct 19 '17 at 16:29
  • Thanks @chepner. I edited it out. The most important part for me is to have the PATH and LD_LIBRARY_PATH environment variables properly set. The other stuff is just aesthetic stuff about the display that the user sees when entering the container. – wandadars Oct 19 '17 at 16:45

2 Answers2

8

RUN entries cannot modify environment variables (I assume you want to set more variables in image_env_setup.sh). Only ENV entries in the Dockerfile (and docker options like --rcfile can change the environment).

You can also decide to source image_env_setup.sh from the .bashrc, of course.

For example, you could either pre-fabricate a .bashrc and pull it in with COPY, or do

RUN echo '. /opt/MyCode/image_env_setup.sh' >> ~/.bashrc
AnoE
  • 8,048
  • 1
  • 21
  • 36
  • I see. Most of the variables that are set in the image_env_setup.sh file are in the Dockerfile example. How would I go about sourcing image_env_setup.sh from the .bashrc? Where would this happen (Dockerfile, docker run command, etc)? – wandadars Oct 19 '17 at 16:44
  • I see @AnoE, but what directory is implicit in the >> .bashrc command? Looks like it assumes that whatever directory that I am in there will be the .bashrc file. That usually goes in the "/home/codeUser" directory? I'm mounting my host's home directory to the home directory of the container, which means that the .bashrc file is the one that is on the host. Should I actually mount the host's home directory in a sub-directory of the container's home? – wandadars Oct 19 '17 at 21:10
  • 1
    @wandadars, first, I've added "~/" so it's clear - it is the home directory inside the container. As to the rest of your comment I cannot really say. If you do mount your host home directory in your container, then, well, you can just add that line to your host .bashrc... but this is all getting very specific and has little to do with Docker - it's just what your preferences are. – AnoE Oct 19 '17 at 21:18
0

you can put /opt/MyCode/image_env_setup.sh in ~/.bash_profile or ~/.bashrc of the container so that everytime you get into the container you have the env's set

Vignajeth
  • 658
  • 1
  • 7
  • 10