This is our Tutorial page!
Watch our most recent
YouTube video
Today, I will be showing you how to set up a DigitalOcean Ubuntu droplet (Linux virtual machine), connect to it via SSH and then install Docker and Docker Compose.
Don't want to read this tutorial? Okay, fine. I have put this video together for you.
Okay, let's jump into it...
Syllabus:
This tutorial will focus on the following:
- Creating our virtual machine
- Connecting to our virtual machine with a password
- SSH to our virtual machine
- Install dependencies'
Prerequisites:
I will be using Visual Studio Code in this tutorial. Feel free to use an SSH client of your choice such as Mobaxterm. In fact, I have put this video together to help you setup Mobaxterm on your machine.
Creating our virtual machine:
There are numerous cloud service providers out there i.e. AWS EC2, Linode and Google Compute Engine. However, I find Digital Ocean easy to use and the most cost effective.
Let's get started!..
Visit DigitalOcean and set up an account. This only takes a few minutes.
Now, click on the green 'Create' button and select 'Droplets'
This will redirect you to a new page. The easiest way to set up a droplet is to add a password and click the 'Create droplet' button at the bottom of the page.
However, when setting up your production virtual machine you may want to spend more time configuring it to you needs. For instance, I would select the London data center as it is the closest to me and my users.
You will be redirected to this next page when you click the 'Create Droplet' button.
That's it! Your virtual machine is ready to go. Make a note of your virtual machines IP address as you will need it in a second.
Connect to our virtual machine:
Open up Visual Studio Code and open a new terminal.
We will need an SSH key pair to SSH to the new virtual machine. In the new terminal, add the following code:
ssh-keygen
This will generate a public/private rsa key pair and ask you to select the path. Take a note of the path you select.
You will also be asked for a passphrase. Feel free to add a passphrase but I won't for this tutorial.
Now, if you navigate to the path that you selected, you will see your SSH files.
Note: The file type 'File' is your private key. The file type 'Pub file' is your public key.
Open the public key file and copy its contents to your clipboard.
Now that we have a SSH key, use the following code to SSH to your new virtual machine:
Note: replace '**your.ip.address**' to the ip address of your new virtual machine.
ssh root@**your.ip.address**
You will be asked for a password. This is the password you used to create the virtual machine.
Great, you are now connected
We now need to save your SSH key as an authorized key.
Use the following code to open up the root user authorized_keys file.
cd .ssh && sudo nano authorized_keys
This will open up a nano and allow you to add text directly into the authorized_key file. Paste your public key into the nano. Press CTRL + S followed by CTRL + X to save and exit.
Adding an SSH key to a non-root users is also pretty easy.
Use the following code to:
- Create a non-root user
- Adding non-root to group sudo
- Set up the authorized_keys file
- Open a nano for you to add your SSH key
adduser **username**
usermod -aG sudo **username**
gpasswd -a **username** sudo
cd
cd /home/**username**
mkdir .ssh && cd .ssh
sudo nano authorized_keys
You now know how to connect to a virtual machine with a password. However, connecting via an SSH key is far more secure.
SSH to our virtual machine:
Like I said earlier, I am using Visual Studio Code so this next part is specifically focussed on how to configure VS Code to connect via SSH.
Firstly, you will need to install 2 extensions.
Open the extensions panel and search for 'remote'
Select 'Remote - SSH' and click install.
Note: This will also install 'Remote - SSH: Editing Configuration Files'.
When complete, click the green icon in the bottom left of your screen. Click on 'Open SSH configuration file...' and select the path to your SSH keys that we set up earlier.
# Read more about SSH config files: https://linux.die.net/man/5/ssh_config
Host **name**
HostName **your.ip.address**
User root
IdentityFile ~/.ssh/**ssh_key_name**
Call 'Host' something you will remember, change the 'HostName' to your virtual machines IP address and change IdentityFile to your private key name.
When saved, click the green icon in the bottom left of your screen again. This time, click on 'Connect to host' and select your new host name.
This will open a new Visual Studio Code instance.
Click on 'Linux on the dropdown list and add your password when prompted.
You have now connect to your virtual machine via SSH. Congratulations! keep this open as you need it in the next section.
Install dependencies:
Open up a new terminal and use the following code to install Docker and Docker Compose.
The first thing we need to do update and upgrade package information. Use the following commands to do this:
sudo apt update
sudo apt upgrade
We will be installing Docker Compose using curl and Docker using the Docker repository. Run the following command to make sure you have all the correct packages on your virtual machine:
sudo apt-get install ca-certificates
sudo apt-get install curl
sudo apt-get install gnupg
sudo apt-get install lsb-release
Add Docker’s official GPG key with the following:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable nightly test" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
The first thing we need to do update and upgrade package information. Use the following commands to do this:
sudo apt update
sudo apt-get install docker-ce docker-ce-cli containerd.io
Verify that Docker Engine is installed correctly by running the hello-world image:
sudo docker run hello-world
If everything went well you should see something similar to the following:
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
Great! We can now move onto docker-compose.
Run this command to download the current stable release of Docker Compose:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
We now need to apply executable permissions to the binary file. You can do this by using the following command:
sudo chmod +x /usr/local/bin/docker-compose
Create a symbolic link to /usr/bin path. You can do this by using the following command:
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
Let's make sure docker-compose is installed correctly.
docker-compose --version
You should see something similar to this on your screen:
docker-compose version 1.29.2, build 5becea4c
Finished :) You are now ready to upload a Docker project.
Conclusion
We have set up a virtual machine (Droplet) on Digital Ocean and connect to it via a secure SSH key. We the installed all the necessary dependencies to run a Docker project.
Start typing and press Enter to search