This is a sample on how to use container VMs for a simple guestbook. You can read more about containers on GCE here.
There are 2 containers that get deployed into one VM:
- A Redis database. This uses the dockerfile/redis container image.
- A simple python application (based on Flask) that implements the guestbook. The source is included at the root of this repo and is pushed to google/guestbook-python-redis on the Docker Index.
To launch simply have gcloud compute
configured (see the Cloud SDK) with a GCE enabled project and your credentials. There is no need to have Docker installed on your workstation/development machine.
Either run the start-containers.sh
shell script or run the following commands:
VM_NAME=container-vm-guestbook
gcloud compute firewalls create ${VM_NAME}-www --allow tcp:80 --target-tags ${VM_NAME}
gcloud compute instances create ${VM_NAME} \
--tags ${VM_NAME} \
--zone us-central1-a --machine-type n1-standard-1 \
--image https://www.googleapis.com/compute/v1/projects/google-containers/global/images/container-vm \
--metadata-from-file google-container-manifest=manifest.yaml
This will create a new VM called containervm-guestbook
running the 2 containers described above. We also open up the firewall to just this VM so that you can reach the web server running on it. The containers that are running in the VM are specified in manifest.yaml
. It may take a while to get up and running as the container image must be downloaded from the docker registry.
Just hit the public IP of the VM with your web browser to access the guest book.
If you want to change the manifest and reload it, the easiest thing to do is to upload a new manifest and restart the VM hosting the containers.
You can run the restart-containers.sh
script or run the following commands:
gcloud compute instances add-metadata --zone us-central1-a ${VM_NAME} \
--metadata-from-file google-container-manifest=manifest.yaml
gcloud compute instances reset --zone us-central1-a ${VM_NAME}
To clean up the VM, simply run stop-containers.sh
. This will delete the VM and its root disk. Any data on the VM will be lost!
Or run the following commands:
gcloud compute firewalls delete --quiet ${VM_NAME}-www
gcloud compute instances delete --quiet --zone=us-central1-a ${VM_NAME}
-
Have Docker installed on a development workstation. You can use a GCE instance for this. See instructions here.
-
Build your application with (this project should match where you plan to create the VM):
docker build -t gcr.io/<your-project-id>/guestbook-python-redis .
-
Push your application to the Google Container Registry with:
gcloud preview docker push gcr.io/<your-project-id>/guestbook-python-redis
-
Modify
manifest.yaml
to refer to usegcr.io/<your-project-id>/guestbook-python-redis
in place ofgoogle/guestbook-python-redis
. -
Start up a new VM with
start-containers.sh
or reload you rexisting one withrestart-containers.sh
.