Deploy docker private image to compute engine instance by using GCP

帅比萌擦擦* 提交于 2020-12-08 00:43:00

问题


stages:
  - build
  - docker-push
  - deploy

cache:
  paths:
    - node_modules/

build:
  stage: build
  image: node:latest
  script:
    - yarn install
    - npm run build
  artifacts:
    paths:
      - dist/

docker:
  stage: docker-push
  image: docker:18.09.7

  services:
    - docker:18.09.7-dind

  script:
    - docker login --username=$DOCKER_USERNAME --password=$DOCKER_PASSWORD
    - docker tag $DOCKER_REPOSITORY:$CI_RUNNER_ID $DOCKER_REPOSITORY:latest
    - docker push $DOCKER_REPOSITORY

test:
  stage: deploy
  image: google/cloud-sdk:latest
  script:
    - echo $GCP_ACCESS_JSON > /tmp/$CI_PIPELINE_ID.json
    - gcloud auth activate-service-account $GCP_CE_PROJECT_EMAIL --key-file /tmp/$CI_PIPELINE_ID.json --project $GCP_PROJECT_ID

I have this .gitlab-ci.yml file . I've successfully created docker image and published it to docker hub private repository . I also have created GCP compute enigne instance and as you can see in gitlab-ci file I can login to this instance by using gcloud command , now I want to find some gcloud command which will give an opportunity to deploy this private docker hub image to this compute engine instance . How can i do it ? If you need more information , pls let me know !


回答1:


If you are not tied to docker hub and can instead use the Google Container Registry, there is a simple gcloud command to deploy an instance using a very slimmed down operating system that is designed only for containers:

 gcloud compute instances create-with-container [INSTANCE_NAME] \
     --container-image [DOCKER_IMAGE]

However, it does not support private docker hub registries except for GCR. Here is a guide to pushing to GCR if that is a route you can take.

Otherwise, there isn't really a gcloud command to do what you want, other than by perhaps configuring a startup script or cloud-init file that has the necessary data to authenticate and pull from the private repository, but it certainly is not automated.




回答2:


Compute Engine VM instance by itself does not run container images.

1) Install Docker and deploy the image manually.

2) Deploy Compute Engine with Container OS and deploy your image manually.

3) Switch to Google Container Registry and deploy the image with your GCE VM instance running Container OS.




回答3:


gcloud create instance make sure docker available(if not install) Deploy manually container stream to gcloud
or gcloud instance to connect dockerhub registry and pull your .yml file deploy it



来源:https://stackoverflow.com/questions/58349465/deploy-docker-private-image-to-compute-engine-instance-by-using-gcp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!