38

How do I run a docker image that I built locally on Google Container Engine?

proppy
  • 10,495
  • 5
  • 37
  • 66

4 Answers4

48

You can push your image to Google Container Registry and reference them from your pod manifest.

Detailed instructions

Assuming you have a DOCKER_HOST properly setup , a GKE cluster running the last version of Kubernetes and Google Cloud SDK installed.

  1. Setup some environment variables

    gcloud components update kubectl
    gcloud config set project <your-project>
    gcloud config set compute/zone <your-cluster-zone>
    gcloud config set container/cluster <your-cluster-name>
    gcloud container clusters get-credentials <your-cluster-name>
    
  2. Tag your image

    docker tag <your-image> gcr.io/<your-project>/<your-image>
    
  3. Push your image

    gcloud docker push gcr.io/<your-project>/<your-image>
    
  4. Create a pod manifest for your container: my-pod.yaml

    id: my-pod
    kind: Pod
    apiVersion: v1
    desiredState:
      manifest:
        containers:
        - name: <container-name>
          image: gcr.io/<your-project>/<your-image>
        ...
    
  5. Schedule this pod

    kubectl create -f my-pod.yaml
    
  6. Repeat from step (4) for each pod you want to run. You can have multiple definitions in a single file using a line with --- as delimiter.

Wernight
  • 36,122
  • 25
  • 118
  • 131
proppy
  • 10,495
  • 5
  • 37
  • 66
  • on `gcloud preview container pods --cluster-name cluster-1 create --zone europe-west1-c --config-file registry-pod.yaml` there is a `resource.go:132] The resource in the provided file has no apiVersion defined` – Vad1mo Dec 08 '14 at 19:02
  • see https://github.com/GoogleCloudPlatform/kubernetes/blob/master/examples/gce-pd/testpd.yaml for updated pod example – Vad1mo Dec 08 '14 at 19:41
  • Google Container Engine (GKE) has moved on to Kubernetes 0.6.1, so referring to localhost no longer works. The current workaround is to refer to your private repository by its IP. See also: https://github.com/GoogleCloudPlatform/kubernetes/wiki/User-FAQ – Shannon Dec 20 '14 at 01:15
  • ^ you get the private ip from creating the registry service. ```gcloud preview container services list --cluster $CLUSTER --zone $ZONE``` and replacing 'localhost:5000/image' with ':5000/image' – dgh Jan 18 '15 at 04:00
  • 1
    updated the answer to use [Google Container Registry](https://cloud.google.com/tools/container-registry/) – proppy Jan 23 '15 at 18:17
  • when `gcr.io//` is updated how do you update the running container? – eloone Jan 28 '15 at 23:52
  • @eloone you can just restart the pod if your image ends up with `:latest`, (the default `imagePullPolicy` is `PullAlways` if `:latest`). See https://github.com/GoogleCloudPlatform/kubernetes/blob/master/pkg/api/v1beta2/types.go#L199 and https://github.com/GoogleCloudPlatform/kubernetes/blob/master/docs/images.md#preloading-images for more information about pull policy. – proppy Jan 30 '15 at 18:58
  • @eloone another way to ensure a pull is to always version your images. Since `imagePullPolicy` defaults to `PullIfNotPresent` if not `:latest`, when you update the pod image with a new version number suffix, it will pull this image if it's not present on the node. – proppy Jan 30 '15 at 19:03
  • 2
    is it possible to use an image from docker hub or it has to be uploaded on gcloud registry tagged as 'gcr.io/'? I am probably missing something. – Kerem Mar 24 '16 at 00:55
1

The setup I use is to deploy my own docker registry combined with ssh port forwarding. For that purpose I set up a ssh server in the cluster and use ~/.ssh/config to configure a port forward to the registry.

Also I use jenkins to build the images right in the cloud.

ruediste
  • 2,434
  • 1
  • 21
  • 30
1

Step 1: Specify the container in which you have to work on

gcloud container clusters get-credentials [$cluster_name]

Step 2: Tag the docker image you want to run

docker tag nginx gcr.io/first-project/nginx

Step 3: Push image

gcloud docker push gcr.io/first-project/nginx

Step4:Create yaml file (test.yaml)

apiVersion: v1
kind: Pod
containers:
- name : nginx1
  image: gcr.io/first-project/nginx

Step 5 : Create the pod

kubectl create –f test.yaml
Vageesha
  • 51
  • 1
0

You could copy the registry authentication key of your private docker registry to the .dockercfg file in the root directory of the minions right before starting the pods. Or run docker login on minions before starting.

    docker login --username=<> --password=<> --email=<> <DockerServer>

Referring to the private docker image in the pod configuration should then work as expected.

Leela
  • 29
  • 3
  • Where can I find username and password for the Google Container Registry? – jiwhiz Jun 17 '15 at 21:15
  • Is there any way to automate this in GKE? I tried running docker login on the nodes but I still get imagepulloff errors when starting the workload. Im using gitlab container registry which does allow you to log in with your password and/or tokens. – Ray Foss Nov 17 '17 at 01:06