Building a webserver using google cloud platform and docker
Signup for Google cloud platform
- Log on into the GCP, create an instance
- Under machine type choose micro (1 shared vCPU) this is the free option because they offer 300 credits annually
- Under boot disk choose the operating system you would like to use, using debian 9
- Allow default access under access scopes
- Under firewall choose the traffic option you need (or both if you are not sure) then create.
Accessing VPC instance
Use Putty or any other SSH tool to login in to the instance you created. You will find the external IP address information from GCP and fill in the information to Putty then add your private key to the configuration and SSH into your instance.
Point to Note: *whatever name is in the private key comment section will be used by google as the login account name, so change it to any name you rather use to login in such as foo.bar
Installing Docker
After successfully SSHing into the new VPC, run these commands to install Docker
Run these commands as root:
sudo -i
apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys F76221572C52609D
apt-get update
apt-get install apt-transport-https
echo "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee -a /etc/apt/sources.list.d/docker.list
apt-get update
apt-get install docker-ce
systemctl enable docker; systemctl start docker
As a regular user configure docker:
sudo usermod -aG docker $USER # log out/in to get new group
mkdir blog
docker run -d --name nginx-proxy -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy:alpine
Output:
gate@instance-1:~$ docker run -d --name nginx-proxy -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock: ro jwilder/nginx-proxy:alpine
Unable to find image 'jwilder/nginx-proxy:alpine' locally
alpine: Pulling from jwilder/nginx-proxy
ff3a5c619c92: Pull complete
b123473be128: Pull complete
7d4e05a03409: Pull complete
8avnf9a3205f: Pull complete
1680d925a947: Pull complete
59b078d3ed91: Pull complete
4c2bca0b0766b: Pull complete
Db2738120c20: Pull complete
b74s56dc7def: Pull complete
fb067de2184c: Pull complete
28yh34bdc46d: Pull complete
Digest: sha256:4db4ecb7fbac270898d2dcb245c374be424c48827c52d8544677dbf3dfbea35f
Status: Downloaded newer image for jwilder/nginx-proxy:alpine
9b897ad2c481d7238a12c4102e5c760505deb7e83740658ac4d070e47ef6a62
gate@instance-1:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9b897ad2c48 jwilder/nginx-proxy:alpine "/app/docker-entrypo…" 4 seconds ago Up 2 seco nds 0.0.0.0:80->80/tcp nginx-proxy
gate@instance-1:~$ ls
gate@instance-1:~$ mkdir blog
gate@instance-1:~$ ls -l blog/
total 24
drwxr-xr-x 2 rsa-key-20180710 google-sudoers 4096 Jul 6 20:34 apps
drwxr-xr-x 2 rsa-key-20180710 google-sudoers 4096 Jul 11 10:26 data
drwxr-xr-x 2 rsa-key-20180710 google-sudoers 4096 Jul 6 20:34 images
drwxr-xr-x 2 rsa-key-20180710 google-sudoers 4096 Jul 11 10:26 logs
drwxr-xr-x 2 rsa-key-20180710 google-sudoers 4096 Jul 11 10:26 settings
drwxr-xr-x 2 rsa-key-20180710 google-sudoers 4096 Jul 11 10:26 themes
gate@instance-1:~$ ls -l blog/data
total 372
-rw-r--r-- 1 rsa-key-20180710 google-sudoers 376832 Jul 11 10:26 ghost.db
Install ghost blog
docker run -d --name blog -p 3001:2368 -e 'VIRTUAL_HOST=foo.bar.com,www.foo.bar.com' -e 'NODE_ENV=production' -e 'ROOT_URL=http://foo.bar.com' -v $HOME/blog:/var/lib/ghost/content:rw ghost:1-alpine
Output:
gate@instance-1:~$ docker run -d --name blog -p 3001:2368 -e 'VIRTUAL_HOST=rootedinkent.us' -e 'NODE_E NV=production' -e 'ROOT_URL=http://foo.bar.com' -v $HOME/blog:/var/lib/ghost/content:rw ghost:1-alpine
Unable to find image 'ghost:1-alpine' locally
1-alpine: Pulling from library/ghost
4064ffdc82fe: Pull complete
5bbf5885bde9: Pull complete
04a820cc9b10: Pull complete
4n7da9cada8a: Pull complete
6573b517daec: Pull complete
E267d724c8b1: Pull complete
564d09b26358: Pull complete
0034b895feef: Pull complete
Digest: sha256:cea63f1cdb9b8c457615f587e3e4ac04ee125149693e195c2e1a81ad49abf824
Status: Downloaded newer image for ghost:1-alpine
0b3f635a4172298a2ac73c3cf04f86efb84fa42f27029d177c5e102e049b6863
gate@instance-1:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b3f635a4172 ghost:1-alpine "docker-entrypoint.s…" 5 seconds ago Up 3 seconds 0.0.0.0:3001->2368/tcp blog
9b897ad2c48 jwilder/nginx-proxy:alpine "/app/docker-entrypo…" 10 minutes ago Up 10 minutes 0.0.0.0:80->80/tcp nginx-proxy
Securing your website using Cloudflare
- Purchase a domain name then go to Cloudflare and create and account
- Choose the plan that suite you then port your A-record over to Cloudflare
- In Cloudflare go to the DNS tab
- Populate the A record fields. In the name field type in the domain name you purchased, in the IPv4 address use the external IP from you GCP instance and set your TTL to automatic
- The go back to the company you purchased your domain name from and configure their custom DNS to point to the Cloudflare DNS nameservers (chan.ns.cloudflare.com and will.ns.cloudflare.com)