Answer the question
In order to leave comments, you need to log in
Yandex.Cloud: How to set up automatic image updates in the Optimized Container Image when updating an image in the Container Registry?
Greetings.
There is a docker container hosted in Yandex Container Registry. This container is deployed in Yandex Compute based on the Optimized Container Image. When I update a container and push a new version to the Container Registry, the Optimized Container Image does not automatically update the image, i.e. the old version of the application remains deployed.
Does Yandex automatically update the image? How to turn it on?
Answer the question
In order to leave comments, you need to log in
To automatically update the IMAGE in the VM, you need to create a cloud function that will have a trigger that fires when the image tag is updated in the container registry.
The content of the function is something like this (node.js 14):
const { Session } = require("yandex-cloud");
const { InstanceService } = require("yandex-cloud/api/compute/v1");
const { ImageService } = require("yandex-cloud/api/containerregistry/v1");
const folderId = "айди каталога";
const instanceId = "айди инстанса (вм)";
module.exports.handler = async function (event, context) {
// создаем сессию из IAM токена и инстансы сервисов
const session = new Session(context.token.access_token);
const instanceService = new InstanceService(session);
const imageService = new ImageService(session);
// получаем метадату обновляемого инстанса и список всех изображений в каталоге
const { metadata } = await instanceService.get({ instanceId, view: 1 });
const { images } = await imageService.list({ folderId });
// находим самое новое изображение и предыдущее
const latestImage = images[0];
const previousImage = images[1];
// обновляем контейнер в метадате
const latestContainerName = "cr.yandex/" + latestImage.name + ":" + latestImage.tags[0];
const previousContainerName = "cr.yandex/" + previousImage.name + ":" + previousImage.tags[0];
const containerMetadata = metadata["docker-container-declaration"].replace(previousContainerName, latestContainerName)
try {
// обновляем ВМ
const response = await instanceService.updateMetadata({
instanceId,
upsert: { "docker-container-declaration": containerMetadata },
});
return {
metadata,
response,
status: 1,
};
} catch (err) {
return {
err,
status: 0,
};
}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question