@@ -2095,8 +2095,27 @@ func startNerdctld(options *run.CommandOptions) {
20952095
20962096 // set up environment variable on remote machine. docker client uses 'non-login & non-interactive shell' therefore the only way is to modify .bashrc file of user 'docker'
20972097 // insert this at 4th line
2098- envSetupCommand := exec .Command ("/bin/bash" , "-c" , "sed -i '4i export DOCKER_HOST=unix:///var/run/nerdctl.sock' .bashrc" )
2099- if rest , err := runner .RunCmd (envSetupCommand ); err != nil {
2100- exit .Error (reason .StartNerdctld , fmt .Sprintf ("Failed to set up DOCKER_HOST: %s" , rest .Output ()), err )
2098+ checkDockerHostCmd := exec .Command ("/bin/bash" , "-c" , "echo $DOCKER_HOST" )
2099+ dockerHostResult , err := runner .RunCmd (checkDockerHostCmd )
2100+ if err != nil {
2101+ exit .Error (reason .StartNerdctld , fmt .Sprintf ("Failed to check DOCKER_HOST: %s" , dockerHostResult .Output ()), err )
2102+ }
2103+ if ! strings .Contains (dockerHostResult .Output (), "unix:///var/run/nerdctl.sock" ) {
2104+ envSetupCommand := exec .Command ("/bin/bash" , "-c" , "sed -i '4i export DOCKER_HOST=unix:///var/run/nerdctl.sock' .bashrc" )
2105+ if rest , err := runner .RunCmd (envSetupCommand ); err != nil {
2106+ exit .Error (reason .StartNerdctld , fmt .Sprintf ("Failed to set up DOCKER_HOST: %s in /home/docker/.bashrc" , rest .Output ()), err )
2107+ }
2108+ }
2109+
2110+ checkDockerHostCmd = exec .Command ("sudo" , "-i" , "bash" , "-c" , "echo $DOCKER_HOST" )
2111+ rootDockerHostResult , err := runner .RunCmd (checkDockerHostCmd )
2112+ if err != nil {
2113+ exit .Error (reason .StartNerdctld , fmt .Sprintf ("Failed to check root DOCKER_HOST: %s" , rootDockerHostResult .Output ()), err )
2114+ }
2115+ if ! strings .Contains (dockerHostResult .Output (), "unix:///var/run/nerdctl.sock" ) {
2116+ envSetupCommand := exec .Command ("/bin/bash" , "-c" , "sudo sed -i '4i export DOCKER_HOST=unix:///var/run/nerdctl.sock' /root/.bashrc" )
2117+ if rest , err := runner .RunCmd (envSetupCommand ); err != nil {
2118+ exit .Error (reason .StartNerdctld , fmt .Sprintf ("Failed to set up DOCKER_HOST: %s in /root/.bashrc" , rest .Output ()), err )
2119+ }
21012120 }
21022121}
0 commit comments