Deploying containers on instances and MIGs

You can configure a Compute Engine instance or aninstance template to deploy and launch aDocker container. Compute Engine supplies an up-to-dateContainer-Optimized OS (COS)image with Docker installed and launches your container when your instancestarts.

Before you begin

Choosing to deploy containers on instances and MIGs

By deploying containers on Compute Engine, you can simplifyapp deployment while controlling your instance infrastructure.

  • Manage instances that are running containers in the same way you would treatany other instance when configuring and managing your Compute Engineinfrastructure.
  • Use familiar processes and tools such as the Google Cloud CLI orthe Compute Engine API to manage your instances with containers.
  • Create scalable services using managed instance groups (MIGs) runningcontainers, which offer features like autoscaling, autohealing,rolling updates, multi-zone deployments, and load balancing.
Note: The Compute Engine feature that deploys containers on VMs during VM creation is deprecated. Use thedocker run commands in a startup script or use thecloud-init tool to configure and to run containers on your VMs and MIGs. For more information, seeMigrate containers that were deployed on VMs during VM creation.

Alternatively, you might consider deploying toGoogle Kubernetes Engine to:

  • Run a large number of microservices
  • Have faster container startup time
  • Take advantage ofKubernetes automated orchestration, including auto upgrades, node auto repair, andautoscaling

Running each microservice on a separate instance on Compute Enginecould make the operating system overhead a significant part of your cost.Google Kubernetes Engine lets you deploy multiple containers and groups of containersfor each instance, which can allocate host instance resources more efficientlyto microservices with a smaller footprint.

How deploying containers on Compute Engine works

The common methods of deploying software onto a Compute Engineinstance include:

  • Deploying software on instance boot using astartup script orcloud-init.
  • Creating a custom boot disk image with software pre-installed.

Both of the methods in the previous list combine the tasks of configuring theapp and setting up the operating system environment. As the developer, you mustcarefully track and resolve any runtime dependencies. For example, if two appsrunning on a VM use different versions of the same library, you must installboth versions and point to them through system variables.

Apps running on different versions of the same library.
An instance with apps deployed directly to the operating system

Alternatively, you can deploy software in a container onto an instance or to aMIG. A container carries both application software and the requiredlibraries and is isolated from OS apps and libraries. A container can bemoved between deployment environments without dealing with conflicting libraryversions in the container and its OS.

An example of apps deployed in containers.
An instance with apps deployed in a container

The following process describes how you deploy a container onCompute Engine:

  1. You bundle your app and required libraries into a Docker image andpublish the image toArtifact Registry, or athird-party registry such as Docker Hub.
  2. You specify a Docker image name and thedocker runconfiguration when creating an instance or an instance template for aMIG.

Note: As of November 1, 2020, Docker Hubrate limits apply to unauthenticated or authenticated pull requests on theDocker Free plan. To avoid disruptions and have greater control over yoursoftware supply chain, you can migrate your dependencies toArtifact Registry.

Compute Engine executes the following tasks after you make a requestto create an instance:

  1. Compute Engine creates a VM instance that uses a Google-providedContainer-Optimized OS image. Thisimage includes a Docker runtime and additional software that is responsiblefor starting your container.
  2. Compute Engine stores your container settings ininstance metadata under thegce-container-declaration metadata key.
  3. When the VM starts, the Container-Optimized OS image uses thedocker run command configuration that is stored in the instance's metadata,pulls the container image from the repository, and starts the container.
Using the container image and docker run command.
Steps to create an instance or a managed instance group running a container

Limitations

Preparing a container for deployment

Choose one of the following approaches to make your container image accessibleto Compute Engine:

  • Upload your Docker imageto Artifact Registry.
  • Use any publicly available container images fromDocker Hub or other registries.
Note: Consider running your container image builds onCloud Build, with none of the overhead of managingyour own build servers.

Deploying a container on a new instance

You can deploy a container on a new VM instance by using theGoogle Cloud console or the Google Cloud CLI.

Note: The Compute Engine feature that deploys containers on VMs during VM creation is deprecated. Use thedocker run commands in a startup script or use thecloud-init tool to configure and to run containers on your VMs and MIGs. For more information, seeMigrate containers that were deployed on VMs during VM creation.

Console

Note: TheDeploy container option in the Google Cloud console is deprecated. Use the equivalentdocker run command to configure and to run the container.

The following example deploys a container from a Google-provided NginxDocker image,https://gcr.io/cloud-marketplace/google/nginx1:latest, to aVM instance. To use a different Docker image, replace the Nginx Dockerimage with the one you want in the following steps.

Go to Create an instance

If prompted, select your project and clickContinue. TheCreate an instance page appears and displays theMachine configuration pane.

  1. In theMachine configuration pane, go toName and specify a namefor your instance. For more information, seeResource naming convention.

  2. In the navigation menu, clickOS and storage. TheOperating system and storage pane appears.

  3. Go to theContainer section and then clickDeploy container. IntheConfigure container pane that appears, do the following:

    1. Specify theContainer image to use. Consider the following examples:
      • To select an NGINX 1.12 container image from Cloud Launcher,specifygcr.io/cloud-marketplace/google/nginx1:1.12.
      • To deploy an Apache container image from Docker Hub, always specifythe full Docker image name, such asdocker.io/httpd:2.4.
    2. Optional. Specify any other configuration parameters of your choice.For more information about container configuration options, seeConfiguring options to run your container.
    3. To finish configuring the container and return to theOperating system and storage pane, clickSelect.
  4. Optional: Specify other configuration options. For more information, seeConfiguration options during instance creation.

  5. To finish creating the instance, clickCreate.

    After creating your instance, Compute Engine starts the instanceand launches the container.

gcloud

Note: Thecreate-with-container gcloud CLI command is deprecated. Use the equivalentdocker run command to configure and to run the container.

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, aCloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. Run thegcloud compute instances create-with-container command:

    gcloud compute instances create-with-containerINSTANCE_NAME \    --container-image=CONTAINER_IMAGE

    Replace the following:

    • INSTANCE_NAME:name for thenew instance.
    • CONTAINER_IMAGE: name of the container image.

    For example, the following command creates an instance namednginx-vm, which launches and runs the container image:

    gcr.io/cloud-marketplace/google/nginx1:1.12

    gcloud compute instances create-with-container nginx-vm \    --container-image=gcr.io/cloud-marketplace/google/nginx1:1.12

    To deploy an Apache container image from Docker Hub, always specifythe full Docker image name:

    docker.io/httpd:2.4.

Updating a container on an instance

You can update a Docker image and configuration options to run thecontainer on an instance using Google Cloud console or the Google Cloud CLI.

Note: The Compute Engine feature that deploys containers on VMs during VM creation is deprecated. Use thedocker run commands in a startup script or use thecloud-init tool to configure and to run containers on your VMs and MIGs. For more information, seeMigrate containers that were deployed on VMs during VM creation.

When you update an running a container, Compute Engine performs twosteps:

  • Updates container declaration on the instance. Compute Enginestores the updated container declaration ininstance metadata under thegce-container-declaration metadata key.
  • Stops and restartsthe instance to actuate the updated configuration, if the instance is running.If the instance is stopped, updates the container declaration and keeps theinstance stopped. The instance downloads the new image and launches thecontainer at startup.
Note: If you specify a Docker image labeled withlatest, the instancedownloads the latest image and launches a container from the new image each timethe instance starts. Don't use thelatest label in production if you dependon a specific image version.

Console

Note: TheDeploy container option in the Google Cloud console is deprecated. Use the equivalentdocker run command to configure and to run the container.
  1. Go to theVM instances page.

    Go to VM instances

  2. Click the name of the instance to update.

  3. On the instance details page, clickEdit.

  4. Specify the new container image and update theoptions to run the containeras needed.

  5. To save your changes, clickSave and restart. Compute Enginesaves the changes and restarts the instance automatically to make theupdate. After the instance restarts, it downloads the new image andstarts the container with the updated configuration.

gcloud

Note: Thecreate-with-container gcloud CLI command is deprecated. Use the equivalentdocker run command to configure and to run the container.

Update the container declaration by using thegcloud compute instances update-container command. For example:

gcloud compute instances update-container nginx-vm \    --container-image gcr.io/cloud-marketplace/google/nginx1:latest

This command sets the container image togcr.io/cloud-marketplace/google/nginx1:latestand restarts the instance to actuate the changes. You can also updateany of the properties described inConfiguring options to run your containerby adding corresponding flags.

After the instance restarts, it downloads the new container image andstarts the container with the new configuration.

Deploying a container on a managed instance group

Note: The Compute Engine feature that deploys containers on VMs during VM creation is deprecated. Use thedocker run commands in a startup script or use thecloud-init tool to configure and to run containers on your VMs and MIGs. For more information, seeMigrate containers that were deployed on VMs during VM creation.

You can deploy a container to a new managed instance group (MIG) usingGoogle Cloud console or the Google Cloud CLI by following these steps:

  1. Create aninstance template that isbased on a Docker image.

    Note: To maintain identical instances in your group, include a specificDocker image version in your instance template, such asnginx1:15. Formore information, seeDeterministic instance templates.
  2. Create aMIGfrom the new instance template.

Console

Note: TheDeploy container option in the Google Cloud console is deprecated. Use the equivalentdocker run command to configure and to run the container.

The following example creates an instance template that deploys acontainer from a Google-provided Nginx(gcr.io/cloud-marketplace/google/nginx1:15) Docker image to a MIG.To use other Docker images, replacegcr.io/cloud-marketplace/google/nginx1:15 in the following example withthe image you want to use.

  1. Go to theInstance templates page.

    Go to Instance templates

  2. To create an instance template, clickCreate instance template.

  3. UnderContainer, selectDeploy container image.

  4. UnderContainer image, specify the Docker image name and configureoptions to run the container.For example, you can specifygcr.io/cloud-marketplace/google/nginx1:15 for the container image.

  5. ClickCreate.

Next, create a MIG that uses the new instance template.

gcloud

Note: Thecreate-with-container gcloud CLI command is deprecated. Use the equivalentdocker run command to configure and to run the container.

Create an instance template for running Docker images using thegcloud compute instance-templates create-with-container command:

gcloud compute instance-templates create-with-containerTEMPLATE_NAME \  --container-imageDOCKER_IMAGE

You can also configureoptions to run your container.

For example, the following command creates a new instance template with namenginx-template, which includes information about the Docker image.An instance created from this template launches and runs the Dockerimagegcr.io/cloud-marketplace/google/nginx1:15 when the instance starts.

gcloud compute instance-templates create-with-container nginx-template \    --container-image gcr.io/cloud-marketplace/google/nginx1:15

Next, create a MIG using the new instance template.

Now that you have an instance template, you cancreate a MIGthat uses the instance template. For example, to create a MIGby using the gcloud CLI and thenginx-template that you justcreated, run the following command:

gcloud compute instance-groups managed create example-group \    --base-instance-name nginx-vm \    --size 3 \    --template nginx-template

Updating a managed instance group running a container

Beta

This feature is subject to the "Pre-GA Offerings Terms" in the General Service Terms section of theService Specific Terms. Pre-GA features are available "as is" and might have limited support. For more information, see thelaunch stage descriptions.

You can update a managed instance group (MIG) to deploy a new version of aDocker image or a new version of the Container-Optimized OS image.

Updating a MIG to a new version of a container image

You can deploy a new version of a Docker image to a MIGby using the Managed Instance Group Updater, in three steps:

  1. Prepare a new Docker image for deployment.
  2. Create an instance template based on the new Docker image in the same way youcreate a container-based template.
  3. Update a MIG to the new instance template by using theManaged Instance Group Updater.

Updating a managed instance group to a new version of Container-Optimized OS image

Google updates Container-Optimized OS imagesregularly,and you might want to apply those updates to your containerized MIGs withoutchanging your Docker image. You can update a MIG to a new version of aContainer-Optimized OS image by using Google Cloud console or theGoogle Cloud CLI in two steps:

  1. Create an instance template based on the current version of your Dockerimage, the same way youcreate a container-based templatefor a new MIG. The latest supported version of aContainer-Optimized OS image is used by default.
  2. Update a MIG with the new instance template by usingManaged Instance Group Updater.

Connecting to a container using SSH

You can connect to a container on an instance by using SSH. Use thegcloud CLI to rungcloud compute ssh with the--container flag:

gcloud compute sshINSTANCE_NAME --containerCONTAINER_NAME

Replace the following:

  • INSTANCE_NAME: the name of the instance
  • CONTAINER_NAME: the name of the container

Learn more about thegcloud compute sshcommand and its arguments.

Monitoring containers on Compute Engine

To monitor your instances running a Container-Optimized OS image, use theNode Problem Detector agent,which communicates with Cloud Monitoring and reports health-related metrics.The agent is built into Container-Optimized OS images starting with Milestone77.

To enable the agent, in containers using images with Milestone 88 or later, editthecustom metadata sectionand setgoogle-monitoring-enabled totrue.

Note: For more information about setting custom metadata for an instance, referto theVM metadata documentation.

To find other ways of enabling the Node Problem Detector, visitEnabling health monitoring.

The Node Problem Detector agent supports the metrics in themetrics listthat begin withguest/.

To interact with the metrics collected by the agent, visit theMetrics Explorer.

Viewing logs

You can view three types of logs related to containers:

  1. Startup agent logs, also known askonlet logs. The startup agent parses the container's configuration and runs tasksto start the container on a Compute Engine instance.

  2. Docker event logs report container events, including container startand stop events.

  3. Logs from your container include theSTDOUT from apps that runin your container.

Note: Depending on the container that you're using, you might need to allowincoming traffic to the container before you see any log entries. To enableaccess to your containers, seePublishing container ports.".

Viewing startup agent logs

Startup agent logs are available in the serial console, through thejournald system service included in the OS image, and throughCloud Logging.

Viewing startup agent logs in the serial console

Console

  1. Go to theVM instances page.

    Go to VM instances

  2. Select the instance for which you want to view startup agent logs.

  3. UnderLogs, clickSerial port 1 (console) to view serialconsole logs.

gcloud

Use theget-serial-port-outputcommand to view logs on the instance's serial port.

gcloud compute instances get-serial-port-outputINSTANCE_NAME

ReplaceINSTANCE_NAME with the name of the instance.

For example, use the following command to view the serial port output of ainstance namednginx-vm:

gcloud compute instances get-serial-port-output nginx-vm

Viewing startup agent logs injournald

  1. Connect to your instancewith a container by using SSH.
  2. Execute thesudo journalctl command to see the instance startup andcontainer startup logs. Use the following command to filter for containerstartup agent logs (konlet).

    sudo journalctl -u konlet*

Viewing startup agent logs in Logging

Console

  1. Go to theVM instances page.

    Go to VM instances

  2. Select the instance for which you want to view startup agent logs.

  3. UnderLogs, clickCloud Logging to viewCloud Logging logs.

  4. Enter a search filter to retrieve startup agent logs.

    resource.type="gce_instance"logName="projects/PROJECT_ID/logs/cos_system"jsonPayload.SYSLOG_IDENTIFIER="konlet-startup"jsonPayload._HOSTNAME="INSTANCE_NAME"

    Replace the following:

    • PROJECT_ID: the project ID that contains theinstance
    • INSTANCE_NAME: the name of the instance you wantto get logs for

gcloud

Use thegcloud logging read commandwith an appropriate filter to view container startup agent logs.

gcloud logging read "resource.type=gce_instance AND \    logName=projects/PROJECT_ID/logs/cos_system AND \    jsonPayload.SYSLOG_IDENTIFIER=konlet-startup AND \    jsonPayload._HOSTNAME=INSTANCE_NAME"

Replace the following:

  • PROJECT_ID: the project ID that contains theinstance
  • INSTANCE_NAME: the name of the instance that youwant to get logs for

For example, use the following command to view the last 10 startup agentlogs in Logging for an instance namednginx-vmthat's running COS 70 and that exists inmy-project.

gcloud logging read "resource.type=gce_instance AND \    logName=projects/my-project/logs/cos_system AND \    jsonPayload.SYSLOG_IDENTIFIER=konlet-startup AND \    jsonPayload._HOSTNAME=nginx-vm" \    --limit 10

Viewing Docker event logs

You can view Docker event logs injournald and in Cloud Logging.

Viewing Docker event logs injournald

  1. Connect to your instancewith a container using SSH.
  2. Execute thesudo journalctl command with the following filter to seeDocker event logs.

    sudo journalctl -u docker-events-collector

Viewing Docker event logs in Logging

Console

  1. Go to theVM instances page.

    Go to VM instances

  2. Select the instance for which you want to view startup agent logs.

  3. UnderLogs, clickCloud Logging to viewCloud Logging logs.

  4. Enter the following search filter to retrieve Docker event logs.

    resource.type="gce_instance"logName="projects/PROJECT_ID/logs/cos_system"jsonPayload._HOSTNAME="INSTANCE_NAME"jsonPayload.SYSLOG_IDENTIFIER="docker"

    Replace the following:

    • PROJECT_ID: the project ID that contains theinstance
    • INSTANCE_NAME: the name of the instance youwant to get logs for

gcloud

Use thegcloud logging read commandwith an appropriate filter to view Docker event logs.

gcloud logging read "resource.type=gce_instance AND \    logName=projects/PROJECT_ID/logs/cos_system AND \    jsonPayload._HOSTNAME=INSTANCE_NAME AND \    jsonPayload.SYSLOG_IDENTIFIER=docker"

Replace the following:

  • PROJECT_ID: the project ID that contains theinstance
  • INSTANCE_NAME: the name of the instance you wantto get logs for

For example, use the following command to view the last 10 Docker event logsin Logging for an instance namednginx-vm that'srunning COS 70 and that exists inmy-project.

gcloud logging read "resource.type=gce_instance AND \    logName=projects/my-project/logs/cos_system AND \    jsonPayload._HOSTNAME=nginx-vm AND \    jsonPayload.SYSLOG_IDENTIFIER=docker" \    --limit 10

Viewing container logs

Console

  1. Go to theVM instances page.

    Go to VM instances

  2. Select the instance for which you want to view startup agent logs.

  3. UnderLogs, clickCloud Logging to viewCloud Logging logs.

    Logs serial port 1.

  4. The Cloud Logging page loads with a default search filter. Copy thevalue forresource.labels.instance_id. You will use it later.

  5. Update the search filter to retrieve container logs.

    resource.type="gce_instance"logName="projects/PROJECT_ID/logs/cos_containers"resource.labels.instance_id="INSTANCE_ID"

    Replace the following:

    • PROJECT_ID: the project ID that containsthe instance
    • INSTANCE_ID: the ID of the instance that youwant to get logs for

gcloud

Use thegcloud logging read commandto view container logs.

  1. Determine the ID for the instance that you want to get logs for:

    gcloud compute instances describeINSTANCE_NAME \   --zoneZONE \   --format="value(id)"

    Replace the following:

    • INSTANCE_NAME: the name of the instance thatyou want to get logs for
    • ZONE: the zone where the instance islocated
  2. Use the following command and filter to view the instance's containerlogs.

    gcloud logging read "resource.type=gce_instance AND \    logName=projects/PROJECT_ID/logs/cos_containers AND \    resource.labels.instance_id=INSTANCE_ID"

    Replace the following:

    • PROJECT_ID: the project ID that contains the instance.
    • INSTANCE_ID: the ID of the instance.

    For example, use the following command to view the last 10 containerlogs in Cloud Logging for an instance that is running COS 70,that exists inmy-project, and that has an instance ID of555123456789012345.

    gcloud logging read "resource.type=gce_instance AND \    logName=projects/my-project/logs/cos_containers AND \    resource.labels.instance_id=555123456789012345" \    --limit 10

Specifying container-optimized images or image families

Containerized instances or instance templates are created to use the latestsupportedcontainer-optimized image by default.The image belongs to thecos-cloud project.

You can override this default with another image from thecos-cloud project.For information about available image families and their attributes, seeChoosing the right Container-Optimized OS version.

For example, after you know which image you want to use, in thegcloud CLI, either provide the--image flag tooverride the default container-optimized image or provide the--image-family flag to pick the latest image from the specifiedfamily at instance creation time.

The following example creates a containerized instance that uses the latestimage from thecos-dev image family:

gcloud compute instances create-with-container nginx-vm \    --image-family cos-dev \    --image-project cos-cloud \    --container-image gcr.io/cloud-marketplace/google/nginx1:1.15

Configuring firewall rules

Containerized instances launch containers whose network is set to host mode. Acontainer shares the host network stack, and all interfaces from the host areavailable to the container.

Note: An instance starts a container with the--network="host" flag of thedocker run command. Learn aboutcontainer network settings and host mode.

Bydefault, Google Cloudfirewall rules block all incoming connections to aninstance and allow all outgoing connections from an instance.

Create firewall rulesto allow incoming connections to your instance and therefore to the container.

Note: This method of instance creation overrides thedefault host firewall configurationof container-optimized images and opens all ports for incoming TCP connections.The ports are still not accessible from outside the instance because of thedefault firewall settingsof theVirtual Private Cloud (VPC) network.

Configuring options to run a container

You can configure the following options to run your container:

  • Specify a container restart policy.
  • Override containerENTRYPOINT (default command to be executed on containerstart).
  • Pass arguments to containerENTRYPOINT command.
  • Run a container in a privileged mode.
  • Mount a host directory ortmpfs as a data volume inside the container.
  • Set environment variables.
  • Allocate a buffer forSTDIN in the container runtime.
  • Allocate a pseudo-TTY.

Learn more aboutconfiguring options to run your container.

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.