Deploying to Compute Engine

Compute Engine can pull containers directly from Artifact Registry repositories so you can run containerized workloads on virtual machine (VM) instances. You can configure service account roles and access scopes to control how VMs access or upload artifacts across projects.

Required permissions

The Compute Engine service account needs access to Artifact Registry in order to pull container images.

Depending on your organization policy configuration, the default service account might automatically be granted the Editor role on your project. We strongly recommend that you disable the automatic role grant by enforcing the iam.automaticIamGrantsForDefaultServiceAccounts organization policy constraint. If you created your organization after May 3, 2024, this constraint is enforced by default.

If you disable the automatic role grant, you must decide which roles to grant to the default service accounts, and then grant these roles yourself.

If the default service account already has the Editor role, we recommend that you replace the Editor role with less permissive roles.To safely modify the service account's roles, use Policy Simulator to see the impact of the change, and then grant and revoke the appropriate roles.

Some examples of required access scopes and required roles for different scenarios are as follows:

  • To pull container images from Artifact Registry repositories, you must grant the Compute Engine service account the Artifact Registry Reader role (roles/artifactregistry.reader). Additionally, ensure the read-only access scope is set for Cloud Storage storage buckets.
  • You want the VM instance to upload to repositories. In this case, you must configure an access scope with write access to storage: read-write, cloud-platform, or full-control.
  • The VM instance is in a different project than the repositories that you want to access. In the project with the repositories, grant the required permissions to the instance's service account.
  • The repositories are in the same project, but you don't want the default service account to have the same level of access across all repositories. In this case, you must grant the appropriate permissions at the repository level and revoke the Artifact Registry permissions at the project level.
  • The VM is associated with a custom service account. Ensure that the service account has the required permissions and access scope.
  • You are using custom roles to grant permissions and the custom role doesn't include the required Artifact Registry permissions. Add the required permissions to the role.

Deploy containers

To deploy containers on VM instances and managed instance groups (MIGs), use startup scripts or cloud-init. The container startup agent (konlet) was shut down for new VMs on July 31, 2026, and is deprecated for existing VMs. For more information about the deprecation, see Compute Engine container startup agent deprecation. To evaluate other container hosting options on Google Cloud, such as managed container platforms, see Compare container deployment options.

For instructions on deploying containers on VMs, see the following resources:

To pull an image stored in Artifact Registry, specify your Artifact Registry image URL in your startup script or cloud-init configuration:

LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE:TAG

Replace the following:

  • LOCATION: the regional or multi-regional location of the repository.
  • PROJECT_ID: your Google Cloud project ID.
  • REPOSITORY: your Artifact Registry repository name.
  • IMAGE: the image name.
  • TAG: the image tag.

Authenticate Docker to pull private images

Unlike the deprecated container startup agent, startup scripts and cloud-init don't configure Docker authentication automatically. If you pull private images from Artifact Registry, you must configure Docker to authenticate with Artifact Registry.

For instructions on configuring Docker credentials, see Configure authentication to Artifact Registry for Docker. On Container-Optimized OS instances, use the preinstalled docker-credential-gcr helper as described in Container-Optimized OS instances.

Container-Optimized OS instances

To run a container on a Container-Optimized OS instance by using an image in your Artifact Registry repository, see the following resources:

The docker-credential-gcr credential helper is preinstalled on Container-Optimized OS. You don't need to download the helper or install it into /usr/bin/.

When configuring authentication for private images on Container-Optimized OS by using docker-credential-gcr:

  • Set a writable directory for Docker configuration. The /root directory is read-only on Container-Optimized OS, which prevents docker-credential-gcr from writing credentials to /root/.docker/config.json. Setting DOCKER_CONFIG to a dedicated writable directory (such as export DOCKER_CONFIG=/var/lib/docker-config) or setting a writable home directory (such as export HOME=/home/USERNAME, where USERNAME is your user account name) allows docker-credential-gcr configure-docker and docker run to succeed without writing under /root.
  • Ensure that the same writable DOCKER_CONFIG or HOME environment variable is set for both docker-credential-gcr and subsequent docker run commands (or set in Environment= within a systemd service unit).
  • Specify the exact repository registry host in the --registries flag (such as --registries=LOCATION-docker.pkg.dev). Docker's credHelpers configuration checks exact hostnames rather than wildcards. If you pass a wildcard such as *.pkg.dev, Docker doesn't match your registry and authentication fails silently.

The following example shows a startup script that configures authentication and runs a private container image in the background (-d):

#!/bin/bash
export DOCKER_CONFIG=/var/lib/docker-config
docker-credential-gcr configure-docker \
    --registries=LOCATION-docker.pkg.dev

# Stop and remove any container from a previous VM boot
CONTAINER_NAME="my-app"
docker stop "$CONTAINER_NAME" || true
docker rm "$CONTAINER_NAME" || true

docker run -d \
    --name="$CONTAINER_NAME" \
    --restart=always \
    LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE:TAG

To configure authentication and run containers by using cloud-init instead of a startup script, set Environment="DOCKER_CONFIG=/var/lib/docker-config" and call docker-credential-gcr configure-docker in an ExecStartPre command within your systemd service unit:

#cloud-config

write_files:
- path: /etc/systemd/system/cloudservice.service
  permissions: 0644
  owner: root
  content: |
    [Unit]
    Description=Start container from Artifact Registry
    Wants=gcr-online.target
    After=gcr-online.target

    [Service]
    Environment="DOCKER_CONFIG=/var/lib/docker-config"
    ExecStartPre=/usr/bin/docker-credential-gcr configure-docker \
        --registries=LOCATION-docker.pkg.dev
    ExecStart=/usr/bin/docker run --rm --name=mycloudservice \
        LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE:TAG
    ExecStop=/usr/bin/docker stop mycloudservice
    ExecStopPost=-/usr/bin/docker rm mycloudservice

runcmd:
- systemctl daemon-reload
- systemctl start cloudservice.service