0

I need to install docker and login to my repository in one script. I'm using ubuntu 16.04 but could also use 18.04.

docker_setup.sh:

sudo apt-get install -y docker.io
sudo service docker start
sudo usermod -a -G docker ubuntu
exec sudo su -l ubuntu
docker login -u $DOCKER_USER -p $DOCKER_PW

This does not work in a shell script because the sudo su command opens up a subshell. I cannot do newgrp docker for the same reason. Also, I cannot do something like

newgrp docker << DOCKERSUBHSELL
docker login -u $DOCKER_USER -p $DOCKER_PW
DOCKERSUBSHELL

because I am using the ray library, and a later part of the library (that works under the hood) also expects to be able to use docker commands. Is there a solution for this?

Preethi Vaidyanathan
  • 1,203
  • 1
  • 12
  • 32

2 Answers2

0

This is very easy. You can make a shell script exactly like this but you have to use cat and EOF. let me explain.

pi@raspberry$ sudo ./script.sh

This script will contain the following output

cat << EOF | sudo apt-get install -y docker.io
   sudo service docker start
   sudo usermod -a -G docker ubuntu
   exec sudo su -l ubuntu
   exit
   docker login -u $DOCKER_USER -p $DOCKER_PW
   EOF

if this command "docker login -u $DOCKER_USER -p $DOCKER_PW" is set to execute in ubuntu user then remove "exit" from my bash script and you are done.

Let me know if it works or the results so I can further modify it and help you. Thank you :)

Usman Khan
  • 121
  • 1
  • 12
0

This may be what's required :

sudo su -l ubuntu -c bash << DOCKERSUBSHELL
    docker login -u "$DOCKER_USER" -p "$DOCKER_PW"
DOCKERSUBSHELL
Philippe
  • 20,025
  • 2
  • 23
  • 32