Scalable microservices - Part 2

Lesson 2 tells us how to use Docker and run an instance on Google Cloud Platform.

Using Google Compute Engine we first create a new linux image, connect to it through SSH. Then run docker containers on top of it.

Containers main advantage is that you can run two virtual machines on the same operating system. You can retrieve image from a repo and manage instances with the docker command line tool like you would with a regular UNIX process.

Docker images can be either downloaded or customly built using dockerfiles. Developers responsibility in this case is to delivery the application AND the configuration file for the container.

The docker images then can be sent in a public or private registry for reuse. Most notables ones are Docker Hub, Quay, Google Cloud Registry.

Useful commands

Creating an instance called ubuntu on Compute Engine

1
gcloud compute instances create ubuntu --image-project ubuntu-os-cloud --image ubuntu-1604-xenial-v20160420c

SSH connecting to it

1
gcloud compute ssh ubuntu

Running nginx on the remote host.

1
2
3
michel@ubuntu:~$ sudo apt-get install nginx
michel@ubuntu:~$ sudo systemctl start nginx
michel@ubuntu:~$ curl localhost

Installing docker on the operating system.

1
sudo apt-get install docker.io

Listing docker images and downloading nginx image.

1
2
michel@ubuntu:~$ docker images
michel@ubuntu:~$ docker pull nginx:1.10.0

Running an image. The image is pulled off automatically if not found locally.

1
2
michel@ubuntu:~$ docker run -d nginx:1.10.0
michel@ubuntu:~$ docker run -d nginx:1.9.3

Checking which instance is running

1
michel@ubuntu:~$ docker ps

Stoping a docker instance

1
2
3
4
# Stoping instance
michel@ubuntu:~$ docker stop [container id]
# Removing instance and configuration files
michel@ubuntu:~$ docker rm [container id]

Get the ip address of a docker instance

1
2
docker inspect [container id]
curl http://[ip address from inspect]

Sample Dockerfile defining a base image (Alpine Linux),

1
2
3
4
FROM alpine:3.1
MAINTAINER Kelsey Hightower <kelsey.hightower@gmail.com>
ADD hello /usr/bin/hello
ENTRYPOINT ["hello"]

Then build the docker image and run it as told above.

1
docker build -t hello:1.0.0

Pushing an image on a registry

1
2
3
docker tag hello:1.0.0 username/hello:1.0.0
docker login
docker push username/hello:1.0.0

To check later