Skip to content

Instantly share code, notes, and snippets.

@phattv
Created November 30, 2019 19:43
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save phattv/a1cbe9400258bc6f1d0faf06a8d95c0a to your computer and use it in GitHub Desktop.
Save phattv/a1cbe9400258bc6f1d0faf06a8d95c0a to your computer and use it in GitHub Desktop.
How to use Docker to deploy an image to Google Cloud Platform

Setup Google Cloud Platform (GCP)

  • Login to https://console.cloud.google.com/
  • Create a project (take note of project's ID)
  • Enable billing
  • Go to https://console.cloud.google.com/compute/instances
  • Create a VM instance
    • Name: use a semantic name (take note of instance's name)
    • Region and zone: most local
    • Machine type: smallest possible f1-micro
    • (Optional) Tick "Deploy a container image to this VM instance" and enter the Docker image name
    • Boot disk: Choose Debian/Linux or Container-Optimized OS
    • Create & wait (take note of instance's external IP)
  • Go to https://console.cloud.google.com/compute/metadata/sshKeys
  • Add local ~/.ssh/id_rsa.pub content for project-wide ssh access
  • Make sure ssh with external IP works
  • Enable project's Container Registry API

Build and deploy Docker image

  • Add Dockerfile
  • docker build . -t {image_name} (gcr.io/{project_id}/{instance_name}:{image_tag}) (e.g. gcr.io/phattv-ecommerce/phattv-ecommerce-backend:1.0.0 or docker build . -t gcr.io/evisa-198403/evisa:3.1.1)
  • (optional) docker run --name {container_name} --rm -d -p 80:80 {image_name}
  • gcloud auth login & choose your account
  • docker -- push {image_name}

Apply Docker image on vm instance without Kubenetes (k8s)

  • ssh with external IP
  • docker images
  • docker run --name {container_name} --rm -d -p 80:80 {image_name}

Apply Docker image on vm instance with Kubenetes (k8s)

Tips and tricks

  • Copy from local terminal to remote shell:
vim file
:set paste
cmd + v
:wq

or

cat >file
cmd + v
ctrl + d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment