Skip to content
Advertisement

Why docker exec command ingore bashrc inside docker container?

So. I have docker image with ubuntu (also creared container from this image). On my docker container I see this output:

root@61ff2a8dbf2d:/# uname -a
Linux 61ff2a8dbf2d 5.3.0-51-generic #44~18.04.2-Ubuntu SMP Thu Apr 23 14:27:18 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

On my container on file ~/.bashrc (it is /root/.bashrc) I’m export some variables:

export VARIABLE1="value1"
export VARIABLE2="value2"

When I try to execute command on docker container using “docker exec” I don’t see my variables from .bashrc file

root@HUAWEI-MateBook-X:~/Documents/# docker exec -it sly_compilator /bin/bash -c 'export'
declare -x HOME="/root"
declare -x HOSTNAME="61ff2a8dbf2d"
declare -x OLDPWD
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
declare -x PWD="/"
declare -x SHLVL="1"
declare -x TERM="xterm"

Why I don’t see my variables? How to fix it?

But if I logging my container this works fine.

root@HUAWEI-MateBook-X:~/Documents/# docker exec -it sly_compilator bash
root@61ff2a8dbf2d:/# export
declare -x VARIABLE1="value1"
declare -x VARIABLE2="value2"
declare -x HOME="/root"
declare -x HOSTNAME="61ff2a8dbf2d"
declare -x LS_COLORS="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:"
declare -x OLDPWD
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
declare -x PWD="/"
declare -x SHLVL="1"
declare -x TERM="xterm"

Tell me please where I must but by variables export or what must to do to get success result (variables from .bashrc shows when I call try to execute command inline using docker exec)?

Best regards, Sergei.

Advertisement

Answer

~/.bashrc is only sourced if the shell is interactive. It’s complicated:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the –login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The –noprofile option may be used when the shell is started to inhibit this behavior.

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the –norc option. The –rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.

In your case, bash is started as neither a login shell nor an interactive shell, so none of these files are sourced. When using docker exec -it, you do get an interactive shell, hence the difference.

You could put the initialization in ~/.profile instead, and pass --login to make sure it’s getting used.

A better solution might be to keep the variables in a file on the host system, for example variables.env, and pass --env-file=variables.env to the docker run command used to start your container. That should make sure they are available to every process started inside the container, including other shells and non-shell programs.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement