The Easiest Docker & Docker-compose Setup on Compute Engine
Tags: docker, googlecloud, devops
Published: 2020-07-20 21:28:49

# Inspiration

Whenever I created a new Compute Engine and have to set up Docker again, I usually go to multiple web pages to find the right command to install Docker and Docker-compose.

It usually takes me more than 5' just to find the command line and set up. Sometimes some websites might not have what you want. So I created this blog to help me and maybe you to setup Docker and Docker-compose in less than a minute.

# Requirement
1. In this tutorial, I’d assume you already had a new Compute Engine running Debian (as it’s the default), and get ready to run your app with Docker.
2. You have knowledge with Docker and it’s better that your server has Dockerfile and/or docker-compose.yml

# Getting Started
1. ssh into your virtual machine either via command line or Cloud Console.
2. Login as admin: `$ sudo -s`

> Note: If you don’t do sudo -s first, you need to have sudo in every command

# The 1 Line Command
If you already know what you’re doing but getting somewhere, here is a 1-line command that will get you Docker & Docker-compose set up for Compute Engine.
I also make commands below for you to see what’s going on.
```
apt update && 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 - && add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" && apt update && apt install --yes docker-ce && curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose
```

# Docker
If you prefer doing separately, do the following Commands
```
$ apt update
$ 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 -
$ add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
$ apt update
$ apt install --yes docker-ce
```

## Testing
```
$ docker
$ docker info
```

## Executing the Docker Command without sudo
* Whenever you run docker, you either have to add sudo or run `sudo -s`, and it annoys me sometimes.

* Run the commands below: `$ sudo usermod -aG docker $USER`
Then log in again, you will be able to run docker without sudo

# Docker-compose

## Why do we need docker-compose
When you run `docker run ...`, your app will keep running UNTIL you close the browser. This is inefficient because you don’t want to leave your tab open forever. So we will use docker-compose, and it’ll run your app in the background

## Install
`$ curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose`

`$ chmod +x /usr/local/bin/docker-compose`

## Testing
`docker-compose --version`

## Usage
Make sure you have a docker-compose.yml in your directory
Run `docker-compose up --build`
After starting your app successfully, run `docker-compose up -d`
Otherwise, check the error and fix the bug

# Congratulation
Now, you’re able to run your app in the background and use it using the public IPv4!