Issue
When using bash to execute "docker exec" commands, I am trying to install nvm inside the Docker container. Those are the following commands inside the bash script:
Update: I've added the whole script.
#!/bin/bash
ubuntu_version="ubuntu:$1"
node_version=$2
container_name="ubuntu_container"
green=`tput setaf 2`
reset=`tput sgr0`
red=`tput setaf 1`
echo "${green}Pulling Ubuntu version: $ubuntu_version ${reset}"
docker pull $ubuntu_version
echo "${green}Running Ubuntu in background...${reset}"
# Stop and remove an existing docker container
docker stop $container_name
docker rm $container_name
docker run -d --name $container_name --rm $ubuntu_version sleep inf
echo "${green}Updating Ubuntu...${reset}"
docker exec $container_name apt update
docker exec $container_name apt upgrade
echo "${green}Installing curl${reset}"
docker exec $container_name apt install -y curl
echo "${green}Installing nvm: ${red}$node_version${reset}"
docker exec -it $container_name bash -c "curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash"
docker exec $container_name bash -c ". ~/.bashrc; nvm"
This leads to the following output:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 15037 100 15037 0 0 54481 0 --:--:-- --:--:-- --:--:-- 54481
=> Downloading nvm as script to '/root/.nvm'
=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "nvm": executable file not found in $PATH: unknown
Seems like nvm is either not in the path or not installed. What am I missing here?
Solution
The problem is with the third docker exec
.
You have two options
- Run :
docker exec -it test_container bash
in bash session, you can run nvm
Or
- Run :
docker exec test_container bash -c '. ~/.bashrc; nvm'
Update
Regarding 1), I thought(wrongly) you were running your docker exec
manually.
Regarding 2), save follwoing in test.sh
:
#!/bin/bash
docker run -d --name test_container2 --rm node sleep inf
docker exec test_container2 apt install -y curl
docker exec -it test_container2 bash -c "curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash"
docker exec test_container2 bash -c '. ~/.bashrc; nvm'
and run bash test.sh
When I run it, I don't have nvm: command not found
Update 2
Your version of ~/.bashrc
contains a test on $PS1, so a workaround, change the last line to :
docker exec $container_name bash -c "PS1=x; . ~/.bashrc; nvm"
Answered By - Philippe Answer Checked By - Candace Johnson (WPSolving Volunteer)