Creating a VM from a Container Optimized Image and an additional volume for a Docker container
In this tutorial, you will create a VM from a Container Optimized Image that contains a Docker container with a running MongoDB instance and an additional 10 GB volume attached.
Getting started
If the required Docker image is pushed to Yandex Container Registry, create a service account with the container-registry.images.puller role for the registry in use. A Container Optimized Image VM will pull the Docker image from the registry on behalf of this account.
Create a VM from a Container Optimized Image with an additional volume for a Docker container
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
-
View a description of the CLI create VM command:
yc compute instance create-with-container --help
-
Prepare the Docker Compose specification. Save the following data to a file named
docker-compose.yaml
:version: '3.4' services: app2: container_name: container-name image: "mongo:latest" ports: - "27017:27017" restart: always volumes: - /home/yc-user/coi-data:/data x-yc-disks: - device_name: coi-data fs_type: ext4 host_path: /home/yc-user/coi-data
When creating your VM via the CLI, a default user is created:
yc-user
. -
Create a VM with multiple disks:
yc compute instance create-with-container \ --name coi-vm \ --zone ru-central1-a \ --create-boot-disk size=30 \ --create-disk name=data-disk,size=10,device-name=coi-data \ --network-interface subnet-name=<subnet_name>,nat-ip-version=ipv4 \ --ssh-key <path_to_the_public_part_of_the_SSH_key> \ --docker-compose-file docker-compose.yaml
Where:
--name
: VM name.--zone
: Availability zone.--create-boot-disk
: Parameters of the VM disk. The disk size must be at least 30 GB.--network-interface
: Network parameters:subnet-name
: Name of the subnet to host the VM.nat-ip-version
: Public IPv4 address assignment method.
--ssh-key
: Path to the file with the public key.--docker-compose-file
: YAML file with the container specification.
Once created, the VM will appear in the VM list under Compute Cloud in the management console
. -
Check the result.
-
Get the ID of the running Docker container:
sudo docker ps -a
Result:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1f71******** mongo:latest "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 27017/tcp container-name
-
Connect to the running Docker container:
sudo docker exec -it 1f71******** bash
-
View the list of attached disks. Take note of the
/dev/vdb 11G 24M 9.9G 1% /data
mounted disk:df -H
Result:
Filesystem Size Used Avail Use% Mounted on overlay 32G 4.0G 27G 14% / tmpfs 68M 0 68M 0% /dev ... tmpfs 1.1G 0 1.1G 0% /proc/acpi tmpfs 1.1G 0 1.1G 0% /proc/scsi tmpfs 1.1G 0 1.1G 0% /sys/firmware
For more information about working with VMs, see our step-by-step guides.