This lesson is being piloted (Beta version)

Creating containers in the cloud

Overview

Teaching: 20 min
Exercises: 0 min
Questions
  • How can I create Docker containers in the cloud?

Objectives
  • Demonstrate how to effect creation of a container from the Docker image in the cloud.

  • Gain an initial experience of the container functionality provided by Google Cloud’s Compute Engine.

Creating Containers on Cloud Platforms

There are lots of ways containers can be run on cloud computing platforms (a.k.a., “in the cloud”). Most commercial cloud providers now offer a container hosting service that will connect to the Docker Hub (and their own container registries) in order to fetch the container images that you specify, and charge for the time and resources that the containers use.

Free and Introductory Offer Container Hosting Options

Please note these options can be constantly changing, check with the Cloud providers webpage for the latest details. Beware that many of the free trials require you to give payment information and you will be billed at the end of the trial or if you exceed the free trial limits. Many of the cloud providers offer charge limit options, set these to ensure you don’t get an sudden large bill.

Educational Credits

For this lesson (run by AIMLAC CDT, January 2021) we have received free credit from Google for $50 per student, you will not need to register a credit card to use this. You will have each been emailed a link to activate this.

Running a container in the cloud, using your Google account

Setting up your account

Creating a virtual machine

VM instances

Cloud OS

SSH into your container from the Google Cloud Console

SSH to VM step 1 SSH to VM step 2

$ docker run -it alice/alpine-python

When using the docker run command (as you have done previously), the container takes some default actions after being created, which are specified in your Dockerfile (e.g., the CMD line).

SSH into your container from your computer’s SSH client

If you want to SSH into your Google Cloud virtual machine from your computer’s SSH client instead of the web interface you’ll need to generate an SSH key and add it to the virtual machine.

If you’ve never done this before generate an SSH key with the ssh-keygen command in your terminal.

$ ssh-keygen

This will create two files in the .ssh directory inside your home directory. One called id_rsa and the other id_rsa.pub. These form the public and private halves of an encryption key, you need to place the public half on any systems you want to login to. Display the contents of the id_rsa.pub file and copy it to the clipboard. Then from the Compute Engine VM Instances page do the following:

SSH keys

Destroying your virtual machine

Running a web server container from Google Cloud

Now lets create a new Dockerfile which defines a container based on the nginx webserver. Create a directory called docker-webserver-example and cd into it.

mkdir docker-webserver-example
cd docker-webserver-example

Open up a text editor for a new Dockerfile and type/paste in the following:

FROM nginx
MAINTAINER your@email.address

COPY startup.sh /

RUN apt update
RUN apt -y install procps
RUN chmod 777 /startup.sh

#expose the web server port to outside the container
EXPOSE 80/tcp

ENTRYPOINT ["bash","/startup.sh"]

In the same directory create another file and call it startup.sh and type/paste in the following code. This code will start the nginx web server and then every second it will update index.html (the default webpage) with the current time and a random number.

#!/bin/bash

nginx

#loop forever 
while [ "0" = "0" ] ; do
    time=$(date +%H:%M:%S) 

    echo "My latest random number is $RANDOM and the time is $time" > /usr/share/nginx/html/index.html
    sleep 1
done

Now build the container we’ve just specified

docker build -t alice/docker-webserver-example .

Now lets push it to Docker Hub:

docker push alice/docker-webserver-example

Go back to the Cloud console’s VM Instances page and do the following:

Allow HTTP

List of instances

Working webpage

Key Points

  • You can create Docker containers on cloud computing resources just using a web browser.

  • Google Cloud’s Compute Engine is an online repository storage service that can create Docker containers to perform computation on demand.