Installing docker requires restarting, which pauses the script

Hey guys,

I’m writing a bash script on Linux for the user to run.

The script installs docker software on the laptop by itself:

It downloads the repository, the GPG key, containerd, awscli, etc and other stuff.

Before using AWSCLI, the script enables and starts the docker. Then adds it to the group. See the following:

# Enable docker
sudo systemctl enable docker

# Start docker
sudo systemctl start docker

# Add current user to Docker group
sudo usermod -aG docker $USER

# The rest of the script, including commands of awscli
# ...

It turns out that for the “docker” to be updated in the “group”, there’s a requirement to do a linux restart, or at least log-out (then log in again).

But the problem is, I’d like this script to run completely without intervention of the user.

I tried to use:
“newgrp docker” to reset the group settings, which would add “docker” into it without restarting, but it still doesn’t work well.

How can I solve the problem of needing to restart in the middle in order to keep on with the script’s other commands?

Thanks

What it really just needs is a new terminal, as the current terminal is not aware of the changes on the user profile. A new terminal is.

You’re right. But a new terminal would terminate the current run of the script. Can it keep running only the rest of the script?

There is a length discussion on the superuser forum:

I assume you want to login to ECR and pull some images during the initial setup.

I see a couple of options for this:

  • configure the aws cli profile for root as well and do your initial setup as root?
  • extract the initial setup to its own script and call that script with something like sudo -u $USER bash -i -c "yourscript.sh"
  • Wrap the initial setup into a command like the previous one but replace yourscript.sh with the actual commands. Looks like a here-doc string is a good way to still keep it readable for the human eye.

Note: The authorization tokens for ECR are valid 12hrs, as such the ECR login must be repeated at least once per day. See:
https://docs.aws.amazon.com/cli/latest/reference/ecr/get-login-password.html

1 Like

Thank you so much for your input!
You’re right, I wanted to login to ECR and pull some images.
For now, I’ve splitted the script into two separated files, so that the second script would be run after the reboot.
I’ll look further into your suggestions. Thanks!

I’ve used your suggestions and got the solution that I aimed for.
Thanks!

Btw, do you know if it’s possible to extend the duration of the authorization tokens?

As the API has no parameter for it, I doubt it can be modified. At the end the awscli is just a client for that api: