How to install Docker | Adding local user to EC2 ubuntu Instance | Adding user to Sudoers file
Docker
An open-source project that automates the deployment of software applications inside containers by providing an additional layer of abstraction and automation of OS-level virtualization on Linux.
In simple, Docker is a tool that allows developers, sys-admins etc. to deploy applications in a sandbox (which in docker world we call it containers) to run over the host operating system.
For more information, read Docker Docs
Pre-requisites
* Ubuntu 18.04+ / AWS EC2 Ubuntu Instance
If you are on EC2 ubuntu Instance then lets first create a local user and add SSH priveleges to it and in order to get rid of use of user password again and again. We will also add that user to sudoers group. If you are not on AWS EC2 then ignore and continue from adding user to Sudoers.
Procedure
- Login to your EC2 ubuntu instance using your private key.
$ ssh -i "myprivatekey.pem" ubuntu@public-dns.compute.amazonaws.com
Once you are in, lets start with creating a user and assinging priveleges to it. In my case, I am creating user named ninja
$ sudo adduser ninja --disabled-password
In our case we will be using --disabled-password. Using --disabled-password, you can create a account without use of password. If you want to create user with password then follow below command.
$ sudo adduser ninja
Now lets switch to user ninja,
$ sudo su - ninja
Now its time to add SSH public key to the user account.
- start with creating a SSH directory
$ mkdir .ssh
- Assign owner direcoty, read, write and execute permission(700);
$ chmod 700 .ssh
- Adding SSH keys to Ninja
$ touch .ssh/authorized_keys
- Giving permission to authorized_keys Owner read and write permission.
$ chmod 600 .ssh/authorized_keys
Open the authorized_keys using your favorite editor and copy the public key for the key pair into the file auth_keys.
Remember your key pair which you downloaded while creating instance. Yes I am talking about that key only. Which you used for login to SSH
In order to open you key pair and copy its key. Use below command from your own system where you downloaded your key pair.
$ ssh-keygen -y -f myprivatekey.pem
Copy the key and paste it into authorized_keys file.
Note: You will not able to run nano or vim. So logout from ninja user and first install nano or vim using user named ubuntu. Once installed, switch to user ninja again using below command.
$ sudo su - ninja
Now paste your key which you will get after using the ssh-keygen command.
The key will look something like given in below image.
Copy it and paste under .ssh/authorized_keys
Use nano to open authorisez_keys file.
nano .ssh/authorised_keys
If somehow your command $ ssh-keygen -y -f myprivatekey.pem
fails. Make sure that you have given proper 400
permission to your private key "myprivatekey.pem"
if not given,
$ chmod 400 myprivatekey.pem
Now login using SSH but this time our user name is ninja.
$ ssh -i "myprivatekey.pem" ninja@public-dns.compute.amazonaws.com
Adding User ninja to sudoers group
Login to your EC2 ubuntu instance with default user ubuntu.
Now in order to add ninja to sudoers, follow below steps
$ sudo nano /etc/sudoers
I have highlighted User privilege specification
- Add
ninja ALL=(ALL) NOPASSWD:ALL
just belowroot ALL=(ALL:ALL) ALL
. Follow screenshot for better understanding.
Exit and login again with user ninja, now you will not be asked to enter user password again and again while installing Docker or while performing any other action.
Installing Docker
We will start by first updating existing list of packages.
$ sudo apt update
Now, lets install prerequisite packages so that apt can use packages over HTTPS.
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common
Adding GPG key for the official Docker repository
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Now add the Docker repo to APT sources
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable" && sudo apt update
Inorder to check Docker repo version table and installation candidate, use below command.
$ apt-cache policy docker-ce
Finally, lets install Docker
$ sudo apt install docker-ce
Inorder to check running status of Docker
$ sudo service docker status
Executing Docker commands without Sudo:
Typing sudo again and again becomes a problem because all commands in Docker need sudo access. But Docker understands its users and hence they allow users to execute Docker commands without use of sudo. Follow below steps to do so.
$ sudo usermod -aG docker ${USER}
Please reboot your machine inorder for changes to take effect.
You may also declare user mannualy by typing the name of user. For example:
$ sudo usermod -aG docker pankaj