Running a containerized app in Yandex Serverless Containers
- Get your cloud ready
- Create a service account
- Create a cloud network and subnet
- Configure the security group
- Create a VM with MongoDB
- Create a Yandex Lockbox secret
- Create a registry in Container Registry
- Push the Docker image to the registry
- Create a container in Serverless Containers
- Create an API Gateway
- Test the application
- How to delete the resources you created
Follow this guide to deploy a containerized app in Serverless Containers to administer MongoDB.
MongoDB is deployed on a Yandex Compute Cloud VM. A Yandex Container Registry is used to host the container with the app. Yandex Lockbox is used to encrypt sensitive data. Secure access to the application is enabled over a Yandex API Gateway. The entire infrastructure of the containerized app resides in a single folder.
To deploy your containerized app:
- Get your cloud ready.
- Create a service account.
- Create a cloud network with a subnet.
- Configure the security group.
- Create a Compute Cloud VM with MongoDB.
- Create a Yandex Lockbox secret and version.
- Create a registry in Container Registry.
- Push the Docker image to Container Registry.
- Create a container in Serverless Containers.
- Create an API Gateway.
- Test the application.
If you no longer need the resources you created, delete them.
Get your cloud ready
Sign up for Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or create a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVEorTRIAL_ACTIVEstatus. If you do not have a billing account, create one and link a cloud to it.
If you have an active billing account, you can navigate to the cloud page
Learn more about clouds and folders here.
Required paid resources
The application support cost includes:
- Fee for a continuously running VM (see Yandex Compute Cloud pricing).
- Fee for storing secrets (see Yandex Lockbox pricing).
- Fee for storing a Docker image (see Yandex Container Registry pricing).
- Fee for container invocation count, computing resources allocated to run the application, and outbound traffic (see Serverless Containers pricing).
- Fee for API gateway requests (see Yandex API Gateway pricing).
- Fee for data logging and storage in a log group (see Yandex Cloud Logging pricing).
Create a service account
Create a service account and assign it the required roles for the folder that will host the containerized app infrastructure.
- In the management console
, go to the folder that will host the containerized app infrastructure. - In the list of services, select Identity and Access Management.
- Click Create service account.
- Enter
mongo-expressas the service account name. - Click Add role and select the
container-registry.images.puller,lockbox.payloadViewer, andserverless-containers.containerInvokerroles. - Click Create.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
-
Create a service account named
mongo-express:yc iam service-account create --name mongo-expressResult:
id: aje3pdqm1cul******** folder_id: b1g681qpemb4******** created_at: "2025-06-14T10:31:06.911280565Z" name: mongo-express -
Assign the service account the
container-registry.images.puller,lockbox.payloadViewer, andserverless-containers.containerInvokerroles for the folder that will host your container:yc resource-manager folder add-access-binding <folder_name_or_ID> \ --role container-registry.images.puller \ --subject serviceAccount:<service_account_ID> yc resource-manager folder add-access-binding <folder_name_or_ID> \ --role lockbox.payloadViewer \ --subject serviceAccount:<service_account_ID> yc resource-manager folder add-access-binding <folder_name_or_ID> \ --role serverless-containers.containerInvoker \ --subject serviceAccount:<service_account_ID>Where:
<folder_name_or_ID>: Name or ID of the folder to host the container.--subject serviceAccount:mongo-expressservice account ID.
Result:
effective_deltas: - action: ADD access_binding: role_id: container-registry.images.puller subject: id: aje3pdqm1cul******** type: serviceAccount effective_deltas: - action: ADD access_binding: role_id: lockbox.payloadViewer subject: id: aje3pdqm1cul******** type: serviceAccount effective_deltas: - action: ADD access_binding: role_id: serverless-containers.containerInvoker subject: id: aje3pdqm1cul******** type: serviceAccount
- To create a service account, use the create REST API method for the ServiceAccount resource or the ServiceAccountService/Create gRPC API call.
- To assign the service account roles for a folder, use the updateAccessBindings REST API method for the Folder resource or the FolderService/UpdateAccessBindings gRPC API call.
Create a cloud network and subnet
Create a cloud network and subnet the VM will reside in.
- In the management console
, select Virtual Private Cloud from the list of services. - At the top right, click Create network.
- In the Name field, specify
mongo-express-network. - In the Advanced field, select Create subnets.
- Click Create network.
- In the left-hand panel, select
Subnets. - At the top right, click Create.
- In the Name field, specify
mongo-express-subnet-ru-central1-b. - In the Availability zone field, select
ru-central1-b. - In the Network field, select
mongo-express-network. - In the CIDR field, specify
192.168.1.0/24. - Click Create subnet.
-
Create a cloud network:
yc vpc network create \ --name mongo-express-networkResult:
id: enp1b4okmg57******** folder_id: b1g681qpemb4******** created_at: "2025-06-13T12:57:02Z" name: mongo-express-network default_security_group_id: enphs1omp6m4********Save the default security group ID specified in the
default_security_group_idfield. -
Create a subnet:
yc vpc subnet create \ --name mongo-express-subnet-ru-central1-b \ --zone ru-central1-b \ --network-id <network_ID> \ --range 192.168.1.0/24Where:
--name: Subnet name.--zone: Availability zone.--network-id:mongo-express-networknetwork ID.--range: List of IPv4 addresses the traffic will come to or from.
Result:
id: e2l9i0eaq32t******** folder_id: b1g681qpemb4******** created_at: "2025-06-14T09:27:37Z" name: mongo-express-subnet-ru-central1-b network_id: enp1b4okmg57******** zone_id: ru-central1-b v4_cidr_blocks: - 192.168.1.0/24
-
To create a cloud network, use the create REST API method for the Network resource or the NetworkService/Create gRPC API call, providing the following in your request:
- ID of the folder to host the network.
- Name of the new network,
mongo-express-network, in thenameparameter.
-
To create a subnet, use the create REST API method for the Subnet resource or the SubnetService/Create gRPC API call.
Configure the security group
Configure the default security group in the cloud network you created earlier.
-
In the management console
, select Virtual Private Cloud from the list of services. -
In the left-hand panel, select
Security groups. -
Next to the default security group created for
mongo-express-network, click and select Edit. -
Under Rules, navigate to the Egress tab.
-
Click Add and create a rule based on this table:
Traffic
directionDescription Port range Protocol Destination name /
SourceCIDR blocks Ingressany27017AnyCIDR0.0.0.0/0 -
Click Save.
Add the rule to the security group:
yc vpc security-group update-rules <security_group_ID> \
--add-rule "direction=ingress,port=27017,protocol=any,v4-cidrs=[0.0.0.0/0,0.0.0.0/0]"
Where <security_group_ID> is the default_security_group_id value obtained in the previous step.
Result:
id: enpmal7qcb1q********
folder_id: b1g681qpemb4********
created_at: "2025-06-14T09:24:47Z"
name: default-sg-enpeiqghds1j********
description: Default security group for network
network_id: enpeiqghds1j********
status: ACTIVE
rules:
...
- id: enprm96de4ec********
direction: INGRESS
ports:
from_port: "27017"
to_port: "27017"
protocol_name: ANY
protocol_number: "-1"
cidr_blocks:
v4_cidr_blocks:
- 0.0.0.0/0
- 0.0.0.0/0
default_for_network: true
To add a rule to a security group, use the updateRules REST API method for the SecurityGroup resource or the SecurityGroupService/UpdateRules gRPC API call, providing the following in your request:
-
ID of the security group you want to add rules to, in the
securityGroupIdparameter.To get the security group ID, use the list REST API method for the SecurityGroup resource or the SecurityGroupService/List gRPC API call and provide the folder ID in the
folderIdrequest parameter. -
New security group rules, in the
additionRuleSpecs[]array:- Traffic direction:
ingress. - Name of the traffic transmission protocol, in the
additionRuleSpecs[].protocolNameparameter:any. - List of CIDRs and subnet masks, in the
additionRuleSpecs[].cidrBlocks.v4CidrBlocks[]parameter:[0.0.0.0/0,0.0.0.0/0]. - First port in the traffic ports range, in the
additionRuleSpecs[].ports.fromPortparameter:0. - Last port in the traffic port range, in the
additionRuleSpecs[].ports.toPortparameter:65535.
- Traffic direction:
Create a VM with MongoDB
We recommend using a VM with basic configuration.
-
On the folder page in the management console
, click Create resource and selectVirtual machine instance. -
Select Advanced setup.
-
Under Boot disk image:
-
Navigate to the Container Solution tab and click Configure.
-
In the window that opens, go to the Docker Compose tab and specify the Docker container as follows:
version: '3.1' services: mongo: image: mongo restart: always environment: MONGO_INITDB_ROOT_USERNAME: mongo_db_user MONGO_INITDB_ROOT_PASSWORD: <password> ports: - 27017:27017In the
MONGO_INITDB_ROOT_PASSWORDparameter, specify the password to use for accessing the database. To create a password, you can use this password generator . Save the password, as you will need it in the next steps. -
Click Apply.
-
-
Under Location, select an availability zone for your VM. If you are not sure which one to choose, leave the default.
-
Under Network settings:
- In the Subnet field, select
mongo-express-subnet-ru-central1-b. - In the Public IP address field, select
Auto.
- In the Subnet field, select
-
Under Access, select SSH key and specify the VM access credentials:
- In the Login field, enter the username. Do not use
rootor other names reserved for the OS purposes. To perform operations requiring root privileges, use thesudocommand. -
In the SSH key field, select the SSH key saved in your organization user profile.
If there are no SSH keys in your profile or you want to add a new key:
-
Click Add key.
-
Enter a name for the SSH key.
-
Select one of the following:
-
Enter manually: Paste the contents of the public SSH key. You need to create an SSH key pair on your own. -
Load from file: Upload the public part of the SSH key. You need to create an SSH key pair on your own. -
Generate key: Automatically create an SSH key pair.When adding a new SSH key, an archive containing the key pair will be created and downloaded. In Linux or macOS-based operating systems, unpack the archive to the
/home/<user_name>/.sshdirectory. In Windows, unpack the archive to theC:\Users\<user_name>/.sshdirectory. You do not need additionally enter the public key in the management console.
-
-
Click Add.
The system will add the SSH key to your organization user profile. If the organization has disabled the ability for users to add SSH keys to their profiles, the added public SSH key will only be saved in the user profile inside the newly created resource.
-
- In the Login field, enter the username. Do not use
-
Under General information, specify the VM name:
mongo-vm. -
Click Create VM.
Wait for the VM status to change to Running and save its public IP address: you will need it for connecting to the database.
-
Create a key pair (public and private keys) for SSH access to the VM.
-
Create
docker-spec.yaml, a Docker container specification file:version: '3.1' services: mongo: image: mongo restart: always environment: MONGO_INITDB_ROOT_USERNAME: mongo_db_user MONGO_INITDB_ROOT_PASSWORD: <password> ports: - 27017:27017In the
MONGO_INITDB_ROOT_PASSWORDparameter, specify the password to use for accessing the database. To create a password, you can use this password generator . Save the password, as you will need it in the next steps. -
Run this command:
yc compute instance create-with-container \ --docker-compose-file docker-spec.yaml \ --name mongo-vm \ --zone ru-central1-b \ --network-interface subnet-name=mongo-express-subnet-ru-central1-b,nat-ip-version=ipv4 \ --ssh-key <path_to_public_key_file> \ --create-boot-disk size=30Where:
--docker-compose-file: Path to the Docker container specification file.--name: VM name.--zone: Availability zone.--network-interface: VM network settings.--ssh-key: Path to the public SSH key file, e.g.,./ssh-key.pub. The default username for SSH access isyc-user.--create-boot-disk size: Boot disk size. It must be at least 30 GB.
Result:
id: epde18u4mahl******** folder_id: b1g7gvsi89m3******** created_at: "2023-02-08T10:34:06.601Z" name: mongo-vm ...
Create a Yandex Lockbox secret
The Yandex Lockbox secret will store encrypted authentication credentials.
-
In the management console
, select Lockbox from the list of services. -
Click Create secret.
-
In the Name field, enter a name for the secret:
mongodb-creds. -
Under Version:
- In the Key field, enter
login. - In the Value field, enter
mongo_db_useras your DB user login.
- In the Key field, enter
-
Click Add key/value and specify the second secret:
- In the Key field, enter
password. - In the Value field, enter the password to access the database: the
MONGO_INITDB_ROOT_PASSWORDvalue from the Docker container specification.
- In the Key field, enter
-
Click Create.
Create a secret named mongodb-creds:
yc lockbox secret create \
--name mongodb-creds \
--payload "[{'key': 'login', 'text_value': 'mongo_db_user'},{'key': 'password', 'text_value': '<password>'}]"
Where:
--name: Secret name.--payload: Contents of the secret provided as a YAML or JSON array.<password>:MONGO_INITDB_ROOT_PASSWORDvalue from the Docker container specification.
Result:
id: e6q6nbjfu9m2********
folder_id: b1qt6g8ht345********
created_at: "2023-02-08T10:34:06.601Z"
name: mongodb-creds
status: ACTIVE
current_version:
id: e6q0s9airqca********
secret_id: e6q6nbjfu9m2********
created_at: "2023-02-08T10:34:06.601Z"
status: ACTIVE
payload_entry_keys:
- login
- password
To create a secret, use the create REST API method for the Secret resource or the SecretService/Create gRPC API call.
Create a registry in Container Registry
The registry in Container Registry will house the Docker image of the mongo-express application.
- In the management console
, select Container Registry from the list of services. - Click Create registry.
- Specify
app-registryas the registry name. - Click Create registry.
Create a registry named app-registry:
yc container registry create --name app-registry
Result:
id: crpd50616s9a********
folder_id: b1g88tflru0e********
name: app-registry
status: ACTIVE
created_at: "2023-02-08T10:34:06.601Z"
To create a registry, use the create REST API method for the Registry resource or the RegistryService/CreateRegistryRequest gRPC API call.
Push the Docker image to the registry
-
Create a Docker Hub
account. -
Install Docker:
- For a Windows workstation, use this version
. - For a Linux workstation, use this version
. - For a Mac OS workstation, use this version
.
- For a Windows workstation, use this version
-
Download
themongo-expressimage:docker pull mongo-expressResult:
Using default tag: latest latest: Pulling from library/mongo-express 6a428f9f83b0: Pull complete ... e60224d64a04: Pull complete Digest: sha256:dcfcf89bf91238ff129469a5a94523b3025913dcc41597d72d4d5f4a******** Status: Downloaded newer image for mongo-express:latest docker.io/library/mongo-express:latest -
Authenticate in Container Registry using the Docker credential helper:
CLI-
Configure Docker to use
docker-credential-yc:yc container registry configure-dockerResult:
Credential helper is configured in '/home/<user>/.docker/config.json'The current user's profile holds the saved settings.
-
Check that the
${HOME}/.docker/config.jsonfile includes the following line:"cr.yandex": "yc"
Docker is ready to use.
-
-
Push the Docker image to the registry:
CLI-
Assign a URL to the pushed
mongo-expressimage using the following format:cr.yandex/<registry_ID>/<Docker_image_name>:<tag>:docker tag mongo-express \ cr.yandex/<registry_ID>/mongo-express:mongo-tag -
Push the
mongo-expressimage to the registry:docker push cr.yandex/<registry_ID>/mongo-express:mongo-tagResult:
\The push refers to repository [cr.yandex/crpbr3qaut47********/mongo-express] 7c550ce9591d: Pushed ... aedc3bda2944: Pushed mongo-tag: digest: sha256:c641... size: 1993
-
Create a container in Serverless Containers
To run your application in Yandex Cloud, create a container and its revision:
-
In the management console
, select Serverless Containers from the list of services. -
Click Create container.
-
Enter the container name:
mongo-express-container. -
Click Create.
-
Navigate to the Editor tab.
-
Under Resources, enter the amount of RAM:
1024 MB. -
Under Image settings:
-
In the Image URL field, specify the URL of the Docker image you pushed previously.
-
In the Environment variables field, add the following variables:
ME_CONFIG_BASICAUTH_USERNAME: Leave empty.ME_CONFIG_BASICAUTH_PASSWORD: Leave empty.VCAP_APP_PORT: Set it to8080.ME_CONFIG_MONGODB_SERVER: Specify the VM public IP address obtained earlier.
-
In the Lockbox secrets field, specify these secrets:
ME_CONFIG_MONGODB_AUTH_USERNAME: Secret with theloginkey.ME_CONFIG_MONGODB_AUTH_PASSWORD: Secret with thepasswordkey.
-
-
Under Settings:
- In the Service account field, specify
mongo-express. - In the Timeout field, specify
15.
- In the Service account field, specify
-
-
Click Create revision.
-
Create a container:
yc serverless container create --name mongo-express-containerResult:
id: bba3fva6ka5g******** folder_id: b1gqvft7kjk3******** created_at: "2023-02-08T10:34:06.601Z" name: mongo-express-container url: https://bba3fva6ka5g********.containers.yandexcloud.net/ status: ACTIVE -
Create a container revision:
yc serverless container revision deploy \ --container-name mongo-express-container \ --cores 1 \ --memory 1GB \ --image cr.yandex/<registry_ID>/mongo-express:mongo-tag \ --environment ME_CONFIG_BASICAUTH=false \ --environment VCAP_APP_PORT=8080 \ --environment ME_CONFIG_MONGODB_SERVER=<VM_public_address> \ --secret environment-variable=ME_CONFIG_MONGODB_AUTH_USERNAME,key=login,id=<secret_ID> \ --secret environment-variable=ME_CONFIG_MONGODB_AUTH_PASSWORD,key=password,id=<secret_ID> \ --service-account-id <mongo_express_service_account_ID> \ --network-id <network_ID> \ --execution-timeout 15s \ --concurrency 1Where:
-
--image: URL of themongo-expressDocker image you pushed earlier. -
--environment: Environment variables:ME_CONFIG_BASICAUTH: Set it tofalse.VCAP_APP_PORT: Set it to8080.ME_CONFIG_MONGODB_SERVER: Specify the VM public IP address obtained earlier.
-
--secret environment-variable:ME_CONFIG_MONGODB_AUTH_USERNAMEandME_CONFIG_MONGODB_AUTH_PASSWORDsecrets you created earlier. -
--service-account-id: ID of themongo-expressservice account you created earlier. -
--network-id:mongo-express-networknetwork ID.
Result:
id: bbas0ktemln9******** container_id: bbai45hrl5et******** created_at: "2025-06-14T10:18:06.791Z" image: image_url: cr.yandex/crpbr3qaut47********/mongo-express:mongo-tag image_digest: sha256:c641.... environment: ME_CONFIG_BASICAUTH: "false" ME_CONFIG_MONGODB_SERVER: 89.169.***.*** VCAP_APP_PORT: "8080" resources: memory: "1073741824" cores: "1" core_fraction: "100" execution_timeout: 15s concurrency: "1" service_account_id: ajeldi88vl73******** status: ACTIVE secrets: - id: e6qfi832v2j8******** version_id: e6qj9a9oifme******** key: login environment_variable: ME_CONFIG_MONGODB_AUTH_USERNAME - id: e6qfi832v2j8******** version_id: e6qj9a9oifme******** key: password environment_variable: ME_CONFIG_MONGODB_AUTH_PASSWORD log_options: folder_id: b1g681qpemb4******** runtime: http: {} -
- To create a container, use the create REST API method for the Container resource or the ContainerService/Create gRPC API call.
- To create a container revision, use the deployRevision REST API method for the Container resource or the ContainerService/DeployRevision gRPC API call.
Create an API Gateway
Create an API gateway with the x-yc-apigateway-integration:serverless_containers extension.
-
In the management console
, select API Gateway from the list of services. -
Click Create API gateway.
-
In the Name field, enter the API gateway name:
mongo-express-gw. -
Under Specification, add the following specification:
openapi: 3.0.0 info: title: Sample API version: 1.0.0 paths: /{proxy+}: x-yc-apigateway-any-method: x-yc-apigateway-integration: type: serverless_containers container_id: <container_ID> service_account_id: <service_account_ID> parameters: - explode: false in: path name: proxy required: false schema: default: '-' type: string style: simpleWhere:
container_id:mongo-express-containerID.service_account_id:mongo-expressservice account ID.
-
Click Create.
-
Open the created API gateway and copy the link from the Default domain field.
-
Create an OpenAPI 3.0
specification file to describe your API.Specification
openapi: 3.0.0 info: title: Sample API version: 1.0.0 paths: /{proxy+}: x-yc-apigateway-any-method: x-yc-apigateway-integration: type: serverless_containers container_id: <container_ID> service_account_id: <service_account_ID> parameters: - explode: false in: path name: proxy required: false schema: default: '-' type: string style: simpleWhere:
container_id:mongo-express-containerID.service_account_id:mongo-expressservice account ID.
-
Specify the properties and use the following command to create your API gateway:
yc serverless api-gateway create \ --name mongo-express-gw \ --spec=<specification_file_path>Result:
id: d5d63uh1h26g******** folder_id: b1g681qpemb4******** created_at: "2025-06-14T10:23:19.682Z" name: mongo-express-gw status: ACTIVE domain: d5d63uh1h26g********.********.apigw.yandexcloud.net connectivity: {} log_options: folder_id: b1g681qpemb4******** execution_timeout: 300sSave the link specified in the
domainfield.
To create an API gateway, use the create REST API method for the ApiGateway resource or the ApiGatewayService/Create gRPC API call.
Test the application
Follow the link formatted as d5d63uh1h26g********.********.apigw.yandexcloud.net obtained in the previous step. The MongoDB admin panel opens.
How to delete the resources you created
To stop paying for the resources you created: