Configuring data output from a Docker container to a serial port
To create a VM from an image, such as a Container Optimized Image, and set up a redirect of the application output stream to the VM's serial port:
- Prepare your cloud.
- Create a VM specification file.
- Create a Docker container specification file.
- Get the ID of the image to create the VM.
- Create a VM.
- Check the result.
If you no longer need the resources you created, delete them.
Prepare your cloud
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 Container Optimized Image VM will pull the Docker image from the registry on behalf of this account.
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.
If you don't have a network or subnet, create them.
Required paid resources
The infrastructure support cost includes:
- Fee for a continuously running VM (see Yandex Compute Cloud pricing).
- Fee for using a dynamic or static external IP address (see Yandex Virtual Private Cloud pricing).
Create a VM specification file
Create a VM specification file named cloud-config-ports.yaml
and populate it with the following data:
#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_for_connecting_to_VM>
In the file configuration, set the username and specify the public part of the SSH key required to connect to the VM. You need to create an SSH key pair yourself.
Create a Docker container specification file
Create the specification for a Docker container as a file named container-spec-ports.yaml
and populate it with the following data:
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:
BashPowerShellIMAGE_ID=$(yc compute image get-latest-from-family container-optimized-image --folder-id standard-images --format=json | jq -r .id)
> $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-a \ --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 for creating the VM.--create-boot-disk
: ID of the image to create a boot disk from.
Once created, the VM will appear in the VM list under Compute Cloud in the management console
. -
Check the result.
- In the management console
, go to the folder page and select Compute Cloud. - Click the VM name,
coi-vm-with-sp
. - Under Serial port, select the
COM2
port. In a few minutes, the screen will displayHello world!
.
- In the management console
-
Get the ID of the image to create the VM
To get the ID of the latest image used for VM creation, run:
IMAGE_ID=$(yc compute image get-latest-from-family container-optimized-image \
--folder-id standard-images \
--format=json | jq -r .id)
$IMAGE_ID=(yc compute image get-latest-from-family container-optimized-image `
--folder-id standard-images `
--format=json | ConvertFrom-Json).id
Create a VM
Enter a name for the subnet to create your VM in and run:
yc compute instance create \
--name coi-vm-with-sp \
--zone ru-central1-d \
--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 for creating the VM.--create-boot-disk
: ID of the image to create a boot disk from.
Once created, the VM will appear in the VM list under Compute Cloud in the management console
Check the result
To check the result of configuring data output from the Docker container to the serial port:
- In the management console
, go to the folder page and select Compute Cloud. - Click the VM name,
coi-vm-with-sp
. - Under Serial port, select the
COM2
port. In a few minutes, the screen will displayHello world!
.
For more information about working with VMs, see our step-by-step guides.
How to delete the resources you created
To stop paying for the resources you created:
- Delete the VM.
- If you reserved a public static IP address for the VM, delete it.