[Yandex Cloud documentation](../../index.md) > [Yandex Compute Cloud](../index.md) > [Tutorials](index.md) > Container Optimized Image > Creating an instance group from a Container Optimized Image with multiple Docker containers

# Creating an instance group from a Container Optimized Image with multiple Docker containers

You can create an [instance group](../concepts/instance-groups/index.md) based on a [image](../concepts/image.md) [Container Optimized Image](../../cos/concepts/index.md) with multiple Docker containers inside.

Docker containers are created based on the [Docker Compose](../../cos/concepts/coi-specifications.md#compose-spec) specification.

{% note alert %}

When creating instance groups, keep the [limits](../concepts/limits.md) in mind. Not to disrupt the component Instance Groups, do not update or delete manually created resources: [target group](../../network-load-balancer/concepts/target-resources.md) Network Load Balancer, VMs, and disks. Instead of this, change or delete the entire group.

{% endnote %}

## Getting started {#before-you-begin}

If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

## Set up your environment {#prepare}

1. Create a [service account](../../iam/concepts/users/service-accounts.md) named `group-coi` and assign the `editor` [role](../../iam/concepts/access-control/roles.md) to it:

   {% list tabs group=instructions %}

   - Management console {#console}

     1. In the [management console](https://console.yandex.cloud), select the [folder](../../resource-manager/concepts/resources-hierarchy.md#folder) where you want to create your service account.
     1. Navigate to **Identity and Access Management**.
     1. Click **Create service account**.
     1. Enter the name: `group-coi`.
     1. To assign the service account the `editor` role for the current folder, click **Add role** and select `editor`.
     1. Click **Create**.

   - CLI {#cli}

     1. Create a service account:

        ```bash
        yc iam service-account create --name group-coi
        ```

        Result:

        ```text
        id: ajeabccde01d********
        folder_id: b0g12ga82bcv********
        created_at: "2023-03-13T14:32:18.900092Z"
        name: group-coi
        ```

     1. Assign the role to the service account:

        ```bash
        yc resource-manager folder add-access-binding b0g12ga82bcv******** \
          --role editor \
          --subject serviceAccount:ajeabccde01d********
        ```

   - API {#api}

     Use the [Create](../../iam/api-ref/ServiceAccount/create.md) method for the `ServiceAccount` resource.

   {% endlist %}

1. Create a [network](../../vpc/concepts/network.md#network) named `yc-auto-network` and a [subnet](../../vpc/concepts/network.md#subnet) in the same [availability zone](../../overview/concepts/geo-scope.md):

   {% list tabs group=instructions %}

   - Management console {#console}

     1. In the [management console](https://console.yandex.cloud), select the folder where you want to create your network.
     1. Navigate to **Virtual Private Cloud**.
     1. Click **Create network**.
     1. Enter the network name: `yc-auto-network`.
     1. Select the additional option: **Create subnets**.
     1. Click **Create network**.

   - CLI {#cli}

     1. Create a network:

        ```bash
        yc vpc network create --name yc-auto-network
        ```

        Result:

        ```text
        id: enpabce123hd********
        folder_id: b0g12ga82bcv********
        created_at: "2023-03-13T14:57:15Z"
        name: yc-auto-network
        ```

     1. Create a subnet in the `ru-central1-a` availability zone:

        ```bash
        yc vpc subnet create --network-id enpabce123hd******** --range 192.168.1.0/24 --zone ru-central1-a
        ```

        Result:

        ```text
        id: e1lnabc23r1c********
        folder_id: b0g12ga82bcv********
        created_at: "2023-03-13T16:23:12Z"
        network_id: enpabce123hd********
        zone_id: ru-central1-a
        v4_cidr_blocks:
        - 192.168.1.0/24
        ```

   - API {#api}

     1. Create a network using the [Create](../../vpc/api-ref/Network/create.md) method for the `Networks` resource.
     1. Create a subnet in the `ru-central1-a` availability zone using the [Create](../../vpc/api-ref/Subnet/create.md) method for the `Subnets` resource.

   {% endlist %}

## Create an instance group from a Container Optimized Image with multiple Docker containers {#create}

1. Get the ID of the latest version of the [public](../operations/images-with-pre-installed-software/get-list.md) Container Optimized Image.
   
   A Container Optimized Image in [Container Registry](../../container-registry/index.md) may get updates and modifications that come with new releases. This will not automatically update the [VM](../concepts/vm.md) image to the latest version. To create an [instance group](../concepts/instance-groups/index.md) with the latest Container Optimized Image version, perform an availability check:
   
   {% list tabs group=instructions %}
   
   - CLI {#cli}
   
     ```bash
     yc compute image get-latest-from-family container-optimized-image --folder-id standard-images
     ```
   
     Result:
   
     ```bash
     id: <latest_COI_version_ID>
     folder_id: standard-images
     ...
     ```
   
   - Yandex Cloud Marketplace {#marketplace}
   
     1. Go to the Cloud Marketplace page and select the image with the configuration you need:
        * [Container Optimized Image](https://yandex.cloud/en/marketplace/products/yc/container-optimized-image).
        * [Container Optimized Image GPU](https://yandex.cloud/en/marketplace/products/yc/container-optimized-image-gpu).
     1. Under **Product IDs**, copy the `image_id` value.
   
   {% endlist %}

1. Save the specification of the instance group with Container Optimized Image and multiple Docker containers to the `specification.yaml` file:

   ```yaml
   name: group-coi-containers # VM group name, unique within the folder.
   service_account_id: ajeabccde01d******** # Service account ID.
   instance_template:
     service_account_id: ajeabccde01d******** # ID of the service account to access private Docker images.
     platform_id: standard-v3 # Platform ID.
     resources_spec:
       memory: 2G # Amount of memory (RAM).
       cores: 2 # Number of processor cores (vCPUs).
     boot_disk_spec:
       mode: READ_WRITE # Disk access mode (read and write).
       disk_spec:
         image_id: <latest_COI_version_ID>
         type_id: network-hdd # Disk type.
         size: 32G # Disk size. It must be at least 30 GB.
     network_interface_specs:
      - network_id: enpabce123hd******** # Network ID.
        subnet_ids:
          - e1lnabc23r1c******** # Subnet ID.
        primary_v4_address_spec: {
          one_to_one_nat_spec: {
            ip_version: IPV4 # IPv4 specification for public access to the VM.
          }
        }
     metadata: # Values to write to the VM metadata.
      docker-compose: |- # Key in the VM metadata that is used with the Docker Compose specification.
        version: '3.7'
        services:
          app1:
            container_name: nginx
            image: "nginx"
            ports:
              - "80:80"
            restart: always
          app2:
            container_name: redis
            image: "redis"
            restart: always
      ssh-keys: | # Parameter for providing an SSH key to the VM.
        yc-user:ssh-ed25519 ABC...d01 user@desktop.ru # Username for connecting to the VM.
   deploy_policy: # VM deployment policy for the group.
     max_unavailable: 1
     max_expansion: 0
   scale_policy: # Instance scaling policy for the group.
     fixed_scale:
       size: 2
   allocation_policy: # Instance allocation policy between availability zones.
     zones:
       - zone_id: ru-central1-a
   ```

   {% note info %}

   You can provide an SSH key in the [VM metadata](../concepts/vm-metadata.md) using the `ssh-keys` parameter or in the `user-data` key with the user metadata. This tutorial uses the first option.

   {% endnote %}

1. Create an instance group named `group-coi-containers` using the `specification.yaml` specification:

   {% list tabs group=instructions %}

   - CLI {#cli}

     Run this command:

     ```bash
     yc compute instance-group create --file=specification.yaml
     ```

     Result:

     ```text
     done (48s)
     id: cl0q12abcd4e********
     folder_id: b0g12ga82bcv********
     ...
       target_size: "2"
     service_account_id: ajeabccde01d********
     status: ACTIVE
     ```

   - API {#api}

     Use the [CreateFromYaml](../instancegroup/api-ref/InstanceGroup/createFromYaml.md) method for the `InstanceGroup` resource.

   {% endlist %}

1. Make sure the instance group from the Container Optimized Image and with multiple Docker containers was created:

   {% list tabs group=instructions %}

   - Management console {#console}

     1. In the [management console](https://console.yandex.cloud), select the folder where you created the instance group.
     1. Navigate to **Compute Cloud**.
     1. Navigate to **Instance groups**.
     1. Click `group-coi-containers`.

   - CLI {#cli}

     ```bash
     yc compute instance-group list-instances group-coi-containers
     ```

     Result:

     ```text
     +----------------------+---------------------------+----------------------------------+-------------+------------------------+----------------+
     |     INSTANCE ID      |           NAME            |            EXTERNAL IP           | INTERNAL IP |         STATUS         | STATUS MESSAGE |
     +----------------------+---------------------------+----------------------------------+-------------+------------------------+----------------+
     | fhmabcv0de12******** | cl0q12abcs4g********-fmar | 84.201.155.117                   | 10.130.0.14 | RUNNING_ACTUAL [2h35m] |                |
     | fhmab0cdqj12******** | cl0q12abcs4g********-fqeg | 84.252.131.221                   | 10.130.0.47 | RUNNING_ACTUAL [2h35m] |                |
     +----------------------+---------------------------+----------------------------------+-------------+------------------------+----------------+
     ```

   - API {#api}

     View the list of created VM instances using the [List](../instancegroup/api-ref/InstanceGroup/list.md) method for the `InstanceGroup` resource.

   {% endlist %}

## Test the instance group based on the Container Optimized Image with multiple Docker containers {#check}

1. [Connect](../operations/vm-connect/ssh.md#vm-connect) to one of the instances via SSH.

   {% list tabs group=instructions %}

   - CLI {#cli}

     ```bash
     ssh yc-user@84.201.155.117
     ```

     Result:

     ```text
     Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-54-generic x86_64)

      * Documentation:  https://help.ubuntu.com
      * Management:     https://landscape.canonical.com
      * Support:        https://ubuntu.com/advantage
     Last login: Mon Mar 13 15:23:28 2023 from 123.456.789.101
     ```

   {% endlist %}

1. View a list of Docker containers running on the VM:

   {% list tabs group=instructions %}

   - CLI {#cli}

     ```bash
     sudo docker ps -a
     ```

     Result:

     ```text
     CONTAINER ID  IMAGE  COMMAND                 CREATED             STATUS             PORTS               NAMES
     c0a125a1765a  redis  "docker-entrypoint.s…"  About a minute ago  Up About a minute  6379/tcp            redis
     01288d7e382f  nginx  "/docker-entrypoint.…"  About a minute ago  Up About a minute  0.0.0.0:80->80/tcp  nginx
     ```

   {% endlist %}