Configuring data output from a Docker container to a serial port
In this tutorial, you'll create a VM from a Container Optimized Image and set up a redirect of the application output stream to the VM serial port.
Before you start
If the required Docker image is pushed to Container Registry, create a service account with the container-registry.images.puller role for the registry in use. A VM with a Container Optimized Image pulls the Docker image from the registry on behalf of this account.
Create a VM from a Container Optimized Image with serial port output
If you don't 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 command to create a VM from a Container Optimized Image:
$ yc compute instance create-with-container --help
-
Create a VM specification file. Specify the username, set the public part of the SSH key to connect to the VM, and save this data to a file named
cloud-config-ports.yaml
:#cloud-config runcmd: - [ sudo, chmod, 666, /dev/ttyS1] users: name: <username> groups: sudo shell: /bin/bash sudo: ['ALL=(ALL) NOPASSWD:ALL'] ssh-authorized-keys: - <public SSH key to connect to the VM>
-
Create a Docker container specification file. Save the following data to a file named
container-spec-ports.yaml
:spec: containers: - image: ubuntu name: app command: ["/bin/bash", "-c", "sleep 30 && echo 'Hello World!' > /dev/ttyS1"] securityContext: privileged: true stdin: false tty: false volumeMounts: - mountPath: /dev/ttyS1 name: log-port restartPolicy: Always volumes: - name: log-port hostPath: path: /dev/ttyS1
-
Create a VM with multiple disks.
-
Get the ID of the image to create the VM:
-
Bash
$ IMAGE_ID=$(yc compute image get-latest-from-family container-optimized-image --folder-id standard-images --format=json | jq -r .id)
-
PowerShell
> $IMAGE_ID=(yc compute image get-latest-from-family container-optimized-image --folder-id standard-images --format=json | ConvertFrom-Json).id
-
-
Create a VM:
yc compute instance create \ --name coi-vm-with-sp \ --zone=ru-central1-c \ --network-interface subnet-name=<subnet name>,nat-ip-version=ipv4 \ --metadata-from-file user-data=cloud-config-ports.yaml,docker-container-declaration=container-spec-ports.yaml \ --create-boot-disk image-id=$IMAGE_ID
Where:
--name
: VM name.--zone
: Availability zone.--network-interface
: VM network settings.--metadata-from-file
: YAML metadata files to create the VM.--create-boot-disk
: ID of the image to create a boot disk from.
Once the VM is created, it appears in the list of VMs under Compute Cloud in the management console.
-
Check the results.
- In the management console, go to the folder page and select Compute Cloud.
- Click on the name of the VM
coi-vm-with-sp
. - Under Serial port, select
COM2
. In a few minutes, the screen displaysHello world!
.
-
For more information about working with VMs, see our step-by-step instructions.