Install Docker on a Google Cloud virtual machine

I set up a new virtual machine using the Google Cloud Platform and wanted to install Docker. Although I couldn’t get the official instructions to work, I found this tutorial for installing Docker using Debian 9, which worked since GCP virtual machines use Debian as their Linux distribution.

Here’s how to do it.

Start up a new virtual machine using GCP. Then ssh into the box (with gcloud compute ssh xxxx or something similar) and copy paste this into the terminal prompt

sudo apt update
sudo apt install --yes apt-transport-https ca-certificates curl gnupg2 software-properties-common
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
sudo apt update
sudo apt install --yes docker-ce

At this point you have Docker installed and you can run docker commands in the shell. Problem is that you’ll have to use sudo every time you want to use docker.

Let’s fix this by adding your user to the docker group. For the changes to take effect you need to log out and log in again. Run the commands below:

sudo usermod -aG docker $USER
logout 

Then relogin again (gcloud compute ssh xxx, replacing xxx), test it works with docker run hello-world, and presto! you should have Docker.