Deploying containers on instances and MIGs Stay organized with collections Save and categorize content based on your preferences.
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
- If you aren't familiar with containers, readWhat are containers and their benefits.
- If you aren't familiar with Docker, read theDocker documentation.
- Read aboutContainer-Optimized OS.
- Read aboutmanaged instance groups (MIGs).
- If you haven't already, set upauthentication. Authentication verifies your identity for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine by selecting one of the following options:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
Install the Google Cloud CLI. After installation,initialize the Google Cloud CLI by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.- Set a default region and zone.
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.
docker 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.

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.

The following process describes how you deploy a container onCompute Engine:
- You bundle your app and required libraries into a Docker image andpublish the image toArtifact Registry, or athird-party registry such as Docker Hub.
- You specify a Docker image name and the
docker 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:
- 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.
- Compute Engine stores your container settings ininstance metadata under the
gce-container-declarationmetadata key. - When the VM starts, the Container-Optimized OS image uses the
docker runcommand configuration that is stored in the instance's metadata,pulls the container image from the repository, and starts the container.

Limitations
- You can only deploy one container for each instance. ConsiderGoogle Kubernetes Engine if you need to deploy multiplecontainers per instance.
You can only deploy containers from a public repository or from a privateArtifact Registry or Container Registry repository that you can access. Otherprivate repositories are not supported.
See the access control documentation forArtifact Registry orContainer Registry for informationabout private registry permissions.
Caution: Container Registry is deprecated. Effective March 18, 2025, Container Registry is shut down, and writing images to Container Registry is unavailable. For details on the deprecation and how to migrate to Artifact Registry, seeContainer Registry deprecation.
You can't map an instance's ports to the container's ports (Docker's
-poption). To enable access to your containers, seePublishing container ports.You can only useContainer-Optimized OS imageswith this deployment method.
You can only use this feature through the Google Cloud console or theGoogle Cloud CLI, not the API.
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.
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.
If prompted, select your project and clickContinue. TheCreate an instance page appears and displays theMachine configuration pane.
In theMachine configuration pane, go toName and specify a namefor your instance. For more information, seeResource naming convention.
In the navigation menu, clickOS and storage. TheOperating system and storage pane appears.
Go to theContainer section and then clickDeploy container. IntheConfigure container pane that appears, do the following:
- Specify theContainer image to use. Consider the following examples:
- To select an NGINX 1.12 container image from Cloud Launcher,specify
gcr.io/cloud-marketplace/google/nginx1:1.12. - To deploy an Apache container image from Docker Hub, always specifythe full Docker image name, such as
docker.io/httpd:2.4.
- To select an NGINX 1.12 container image from Cloud Launcher,specify
- Optional. Specify any other configuration parameters of your choice.For more information about container configuration options, seeConfiguring options to run your container.
- To finish configuring the container and return to theOperating system and storage pane, clickSelect.
- Specify theContainer image to use. Consider the following examples:
Optional: Specify other configuration options. For more information, seeConfiguration options during instance creation.
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.In the Google Cloud console, 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.
Run the
gcloud compute instances create-with-containercommand: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 named
nginx-vm, which launches and runs the container image:gcr.io/cloud-marketplace/google/nginx1:1.12gcloud 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 the
gce-container-declarationmetadata 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.
latest, 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.Go to theVM instances page.
Click the name of the instance to update.
On the instance details page, clickEdit.
Specify the new container image and update theoptions to run the containeras needed.
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:
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.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.
Go to theInstance templates page.
To create an instance template, clickCreate instance template.
UnderContainer, selectDeploy container image.
UnderContainer image, specify the Docker image name and configureoptions to run the container.For example, you can specify
gcr.io/cloud-marketplace/google/nginx1:15for the container image.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:
- Prepare a new Docker image for deployment.
- Create an instance template based on the new Docker image in the same way youcreate a container-based template.
- 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:
- 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.
- 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 instanceCONTAINER_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.
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:
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.
Docker event logs report container events, including container startand stop events.
Logs from your container include the
STDOUTfrom apps that runin your container.
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
Go to theVM instances page.
Select the instance for which you want to view startup agent logs.
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
- Connect to your instancewith a container by using SSH.
Execute the
sudo journalctlcommand 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
Go to theVM instances page.
Select the instance for which you want to view startup agent logs.
UnderLogs, clickCloud Logging to viewCloud Logging logs.
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 theinstanceINSTANCE_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 theinstanceINSTANCE_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
- Connect to your instancewith a container using SSH.
Execute the
sudo journalctlcommand with the following filter to seeDocker event logs.sudo journalctl -u docker-events-collector
Viewing Docker event logs in Logging
Console
Go to theVM instances page.
Select the instance for which you want to view startup agent logs.
UnderLogs, clickCloud Logging to viewCloud Logging logs.
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 theinstanceINSTANCE_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 theinstanceINSTANCE_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
Go to theVM instances page.
Select the instance for which you want to view startup agent logs.
UnderLogs, clickCloud Logging to viewCloud Logging logs.

The Cloud Logging page loads with a default search filter. Copy thevalue for
resource.labels.instance_id. You will use it later.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 instanceINSTANCE_ID: the ID of the instance that youwant to get logs for
gcloud
Use thegcloud logging read commandto view container logs.
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 forZONE: the zone where the instance islocated
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 in
my-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 container
ENTRYPOINT(default command to be executed on containerstart). - Pass arguments to container
ENTRYPOINTcommand. - Run a container in a privileged mode.
- Mount a host directory or
tmpfsas a data volume inside the container. - Set environment variables.
- Allocate a buffer for
STDINin the container runtime. - Allocate a pseudo-TTY.
Learn more aboutconfiguring options to run your container.
What's next
- Learn aboutconfiguring options to run your container.
- Learn more aboutmanaged instance groups.
- Learn aboutContainer-Optimized OS.
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.