Push the Docker Image distributed by Docker Hub to Google Container Registry and start the VM based on that Image

Note

This article is a memo for myself, so it is not well-formatted. Please note.

Motivation to write an article

It is necessary to deal with the following cases, and to output the procedure etc.

--Digdag, Embulk Have the Docker Image for server construction shared (on Docker Hub) and --Use it to set up a GCP VM and build a server

Reference page

-Container Registry Quick Start --This document describes "How to push Docker Image prepared locally in Container Registry" and "How to pull Docker Image from Container Registry". --In this article, I referred to the former part. -Using Container Registry with Google Cloud -Start Docker container via Cloud-Config --Create and configure instance

Before you start

  1. In the Cloud Console Project Selector Page (https://console.cloud.google.com/projectselector2/home/dashboard), select or create a Cloud project.
  2. Make sure billing is enabled for your Google Cloud project. (Reference)
  3. Enable the Container Registry API (https://console.cloud.google.com/flows/enableapi).
  4. Initialize with Install Cloud SDK (https://cloud.google.com/sdk/docs).
  5. Install Docker (https://docs.docker.com/install/linux/docker-ce/ubuntu/).
  6. For Ubuntu, debian, etc., let's make it possible to execute the docker command without sudo by restarting sudo usermod -a -G docker $ {USER} +

List of procedures

  1. Pull Docker Image from Docker Hub
  2. Push the (pulled) Docker Image to the Container Registry
  3. Start the VM based on the Docker Image pushed to Container Registry
  4. Clean up to avoid being charged

1. Pull Docker Image from Docker Hub

This time, I will use busybox as an example.

docker pull busybox:latest
# check image
docker image ls
REPOSITORY  TAG     IMAGE ID      CREATED       SIZE
busybox     latest  6d5fcfe5ff17  8 months ago  1.22MB

2. Push the (pulled) Docker Image to the Container Registry

2.1. Configure docker to use the gcloud command line tool as an authentication helper

Below, quoted from the reference page

To push or pull an image, you need to configure Docker to authenticate requests to Container Registry using the gcloud command-line tool. To do this, run the following command (you only need to run it once):

gcloud auth configure-docker

2.2. Tag the image with the registry name

docker push normally pushes to Docker Hub. If you want to push to a specific location, it seems to be controlled by rewriting the registry name tag.

Quoted from the reference page

Before you can push a Docker image to Container Registry, you need to tag the image with the registry name. When you tag a Docker image with a registry name, the docker push command is configured to push the image to a specific location. In this quickstart, the host location is gcr.io. To tag the Docker image, run the following command:

docker tag busybox gcr.io/[PROJECT-ID]/busybox:v1
# check img
REPOSITORY                   TAG  IMAGE ID      CREATED       SIZE
gcr.io/[PROJECT-ID]/busybox  v1   6d5fcfe5ff17  8 months ago  1.22MB

2.3. Push the image into the Container Registry

docker push gcr.io/[PROJECT-ID]/busybox:v1

3. Start the VM based on the Docker Image pushed to Container Registry

* Consider a case where VM is started once with GUI </ font>

From the GCP dashboard screen, move to the VM instance startup screen by following the flow ComputeEngine-> VM instance.

On the VM startup screen, enter that an instance will be created from the container image (see the figure below).

image.png

In container image, describe the path of the container image pushed in advance as described in the example. When specifying the container image stored in ContainerRagistry, the path will be gcr.io / ....

  • By default, the image saved in GCR in the same project as this VM is given the authority to pull, so there is no need to adjust the access authority. If this is not the case, you need to grant the service account that is trying to pull the image the appropriate permissions to the GCS bucket that contains the container image. (Reference)

All you have to do is press the VM instance creation button.

4. Clean up to avoid being charged

4.1. Container Registry cleanup

According to Using Container Registry with Google Cloud (https://cloud.google.com/container-registry/docs/using-with-google-cloud-platform)

gcloud container images delete gcr.io/[PROJECT-ID]/busybox:v1 --force-delete-tags

I heard that it can be executed with, but I could not delete Docker Image from the gcloud command due to the following error.

ERROR: (gcloud.container.images.delete) [gcr.io/[PROJECT-ID]/busybox:v1] is not a valid name. Expected tag in the form "base:tag" or "tag" or digest in the form "sha256:<digest>"

(Currently under investigation ...)

This time, delete the corresponding image of GCR from Google Cloud Console.

I'm going, but I can't find it. .. .. .. (?)

4.2. Compute Engine cleanup

Delete the VM instance in the Google Cloud Console.

Recommended Posts

Push the Docker Image distributed by Docker Hub to Google Container Registry and start the VM based on that Image
[GCP] Until you push the local Docker image to the Container Registry
Push the image to docker hub using Jib
Test, build, and push your Docker image to GitHub Container Registry using GitHub Actions
Steps to push Docker image to GitHub Container Registry (ghcr.io)
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
Check the contents by docker pull from ECR and start the container
Minimal Workflow to push Docker image to Github Container Registry with Github Actions
Docker push to GitHub Container Registry (ghcr.io)
How to mount the batch file location via WSL2 and start the Docker container
Push Docker images from GitHub Actions to GitHub Container Registry
Wait for the container service to start with docker healthcheck
How to solve the problem that you can not pull image from docker hub with Minikube
Organize communication in an environment that uses various resources on the host machine and Docker container
Let's create a Docker container that can connect to CentOS 8 with the minimum configuration by SSH
Until you build the docker environment and start / stop the Ubuntu container
Install Ubuntu20.04 on RaspberryPi 4 and build Kubernetes to run the container
Create a Docker Image for redoc-cli and register it on Docker Hub
Pg_resetwal can be used to start the PostgreSQL Docker container when WAL is broken and cannot be started.
2. Create Docker image and register Registry
Wait for PostgreSQL to start with Docker and then start the WEB service
A shell script that builds a Docker image and pushes it to ECR
[Docker] How to build when the source code is bind-mounted on the container
Created a Docker container image for an OpenLDAP server based on Fedora
Create a container image for arm64 of Kibana and register it in GitHub Container Registry. Start Elastic Stack with Docker Compose on Raspberry Pi 4 (64bit)