Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing a command inside docker shows wrong $PATH

Tags:

linux

bash

docker

I am trying to run a bash command inside docker from host:

$ docker exec -it -u weiss apollo_dev /bin/bash -c "rosbag"

/bin/bash: rosbag: command not found

So I tried:

$ docker exec -it -u weiss apollo_dev /bin/bash -c "echo \$PATH"

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

But when I run docker interactively:

$ docker exec -it -u weiss apollo_dev /bin/bash
weiss@docker$ echo $PATH

/usr/local/cuda-8.0/bin:/home/tmp/ros/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Any reason why I am getting different results for $PATH?

like image 862
Elad Weiss Avatar asked Oct 21 '25 03:10

Elad Weiss


2 Answers

This path is most likely changed in your .bashrc file, and this file is not loaded when the shell is non interactive (see https://www.gnu.org/software/bash/manual/bash.html#Bash-Startup-Files)

So /bin/bash will load it, /bin/bash -c will not

like image 58
Wee Avatar answered Oct 22 '25 19:10

Wee


Here you are getting de $PATH of your Host. Before you run the container the variable is replace for the host's $PATH.

$ docker exec -it -u weiss apollo_dev /bin/bash -c "echo \$PATH"

You need to pass the command without replace the variable, so when run the command in the container just invoke the $PATH variable.

$ docker exec -it -u weiss apollo_dev /bin/bash -c 'echo \$PATH'

Te 'apostrophe' is the key. Bye

like image 35
Pablo Anaquín Avatar answered Oct 22 '25 18:10

Pablo Anaquín