Skip to content

Commit

Permalink
fix(install-script): add DOCKER_HOST to top of bashrc
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Nov 10, 2023
1 parent 94ba306 commit d6e354e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
24 changes: 19 additions & 5 deletions scripts/setup/docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -167,29 +167,43 @@ EOF
}

add_vars_to_bashrc() {
# DOCKER_HOST must be added to the top of bashrc, as running non-interactively
# Most distros exit .bashrc execution is non-interactive

heading_echo "Adding rootless DOCKER_HOST to bashrc"

user_id=$(id -u)
docker_host_var="export DOCKER_HOST=unix:///run/user/$user_id//docker.sock"
docker_host_var="export DOCKER_HOST=unix:///run/user/$user_id/docker.sock"
dc_alias_cmd="alias dc='docker compose'"

# Check if DOCKER_HOST is already defined
# Create a temporary file
tmpfile=$(mktemp)

# Check if DOCKER_HOST is already defined in user's .bashrc
if ! grep -q "$docker_host_var" ~/.bashrc; then
echo "Adding rootless DOCKER_HOST var to ~/.bashrc."
echo "$docker_host_var" >> ~/.bashrc
echo "$docker_host_var" >> "$tmpfile"
fi

echo "Done"
echo

heading_echo "Adding dc='docker compose' alias"

# Check if the alias already exists
# Check if the alias already exists in user's .bashrc
if ! grep -q "$dc_alias_cmd" ~/.bashrc; then
echo "Adding 'dc' alias to ~/.bashrc."
echo "$dc_alias_cmd" >> ~/.bashrc
echo "$dc_alias_cmd" >> "$tmpfile"
fi

# Append the rest of the original .bashrc to the temporary file
if [ -e ~/.bashrc ]; then
grep -v -e "$docker_host_var" -e "$dc_alias_cmd" ~/.bashrc >> "$tmpfile"
fi

# Replace the original .bashrc with the modified file
mv "$tmpfile" ~/.bashrc

echo "Done"
}

Expand Down
33 changes: 29 additions & 4 deletions src/frontend/public/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -299,42 +299,67 @@ echo "Done"


add_vars_to_bashrc() {
# DOCKER_HOST must be added to the top of bashrc, as running non-interactively
# Most distros exit .bashrc execution is non-interactive

heading_echo "Adding DOCKER_HOST and 'dc' alias to bashrc"

user_id=$(id -u)
docker_host_var="export DOCKER_HOST=unix:///run/user/$user_id/docker.sock"
dc_alias_cmd="alias dc='docker compose'"

# Create temporary files for root and user bashrc
tmpfile_root=$(mktemp)
tmpfile_user=$(mktemp)

if [ "$RUN_AS_ROOT" = true ]; then
# Check if DOCKER_HOST is already defined in /root/.bashrc
if ! sudo grep -q "$docker_host_var" /root/.bashrc; then
echo "Adding DOCKER_HOST var to /root/.bashrc."
echo "$docker_host_var" | sudo tee -a /root/.bashrc > /dev/null
echo "$docker_host_var" | sudo tee -a "$tmpfile_root" > /dev/null
echo
fi

# Check if the 'dc' alias already exists in /root/.bashrc
if ! sudo grep -q "$dc_alias_cmd" /root/.bashrc; then
echo "Adding 'dc' alias to /root/.bashrc."
echo "$dc_alias_cmd" | sudo tee -a /root/.bashrc > /dev/null
echo "$dc_alias_cmd" | sudo tee -a "$tmpfile_root" > /dev/null
echo
fi
fi

# Check if DOCKER_HOST is already defined in ~/.bashrc
if ! grep -q "$docker_host_var" ~/.bashrc; then
echo "Adding DOCKER_HOST var to ~/.bashrc."
echo "$docker_host_var" >> ~/.bashrc
echo "$docker_host_var" | tee -a "$tmpfile_user" > /dev/null
echo
fi

# Check if the 'dc' alias already exists in ~/.bashrc
if ! grep -q "$dc_alias_cmd" ~/.bashrc; then
echo "Adding 'dc' alias to ~/.bashrc."
echo "$dc_alias_cmd" >> ~/.bashrc
echo "$dc_alias_cmd" | tee -a "$tmpfile_user" > /dev/null
echo
fi

# Append the rest of the original .bashrc to the temporary file
if [ -e ~/.bashrc ]; then
grep -v -e "$docker_host_var" -e "$dc_alias_cmd" ~/.bashrc >> "$tmpfile_user"
fi
# Replace the original .bashrc with the modified file
mv "$tmpfile_user" ~/.bashrc

# If RUN_AS_ROOT is true, replace /root/.bashrc with the modified file
if [ "$RUN_AS_ROOT" = true ]; then
# Append the rest of the original /root/.bashrc to the temporary file
if [ -e /root/.bashrc ]; then
grep -v -e "$docker_host_var" -e "$dc_alias_cmd" /root/.bashrc >> "$tmpfile_root"
fi

# Replace the original /root/.bashrc with the modified file
sudo mv "$tmpfile_root" /root/.bashrc
fi

echo "Setting DOCKER_HOST for the current session."
export DOCKER_HOST=unix:///run/user/$(id -u)/docker.sock

Expand Down

0 comments on commit d6e354e

Please sign in to comment.