Skip to content

containerd-sdk to create container, An error occurred "failed to mount /tmp/containerd-mount1866540063: no such file or directory" #11849

@J4sonZ

Description

@J4sonZ

Description

I built a containerd client and created container. The code is as follows

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/containerd/containerd/v2/client"
	"github.com/containerd/containerd/v2/pkg/cio"
	"github.com/containerd/containerd/v2/pkg/namespaces"
	"github.com/containerd/containerd/v2/pkg/oci"
	"github.com/containerd/go-cni"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

const os = "linux/amd64"

const containerName = "containerd-test"

func main() {
	ctx := namespaces.WithNamespace(context.Background(), "default")

	conn, err := grpc.NewClient("192.168.3.239:10010", []grpc.DialOption{
		grpc.WithTransportCredentials(insecure.NewCredentials()),
	}...)
	if err != nil {
		log.Fatalf("Grpc client build failed: %v", err)
	}
	defer conn.Close()

	c, err := client.NewWithConn(conn)
	if err != nil {
		fmt.Printf("close err: %v\n", err)
		return
	}
	defer c.Close()

	image, err := c.Pull(ctx, "docker.io/library/nginx:1.28.0-alpine", client.WithPullUnpack, client.WithPlatform(os))
	if err != nil {
		fmt.Printf("failed to create container: %v\n", err)
		return
	}

	container, err := c.NewContainer(
		ctx,
		containerName,
		client.WithNewSnapshot(containerName, image),
		client.WithNewSpec(oci.WithImageConfig(image), oci.WithProcessArgs("/bin/sh")),
	)
	if err != nil {
		log.Fatalf("failed to create container: %v", err)
	}
	defer container.Delete(ctx, client.WithSnapshotCleanup)

	task, err := container.NewTask(ctx, cio.NewCreator(cio.WithStdio))
	if err != nil {
		log.Fatalf("failed to create task: %v", err)
	}
	defer task.Delete(ctx)

	if err := task.Start(ctx); err != nil {
		log.Fatalf("failed to start task: %v", err)
	}

	return
}

when i run it in the localhost
it work!

Image

BUT!
when i run it in the pod, it not working!

2025-05-13T11:50:55Z	ERROR	Failed to create container	{"controller": "enovaserving", "controllerGroup": "enova.emergingai.io", "controllerKind": "EnovaServing", "EnovaServing": {"name":"es-container-service-51c7c797-0bc2-4dbe-group1","namespace":"emergingai"}, "namespace": "emergingai", "name": "es-container-service-51c7c797-0bc2-4dbe-group1", "reconcileID": "3ec838b8-77f3-4341-b23c-dcd1f7fdf2a8", "Container Name": "es-container-service-51c7c797-0bc2-4dbe-group1-container", "error": "failed to mount /tmp/containerd-mount1866540063: no such file or directory"}
github.com/Emerging-AI/emgai/enova-operator/internal/container.CreateContainer
	/workspace/enova-operator/internal/container/crud.go:37
github.com/Emerging-AI/emgai/enova-operator/internal/container.CreateOrUpdate
	/workspace/enova-operator/internal/container/crud.go:147
github.com/Emerging-AI/emgai/enova-operator/internal/detector.(*ContainerDetector).HandleOneServing
	/workspace/enova-operator/internal/detector/container.go:28
github.com/Emerging-AI/emgai/enova-operator/internal/controller.handleOneServing
	/workspace/enova-operator/internal/controller/enovaserving_controller.go:145
github.com/Emerging-AI/emgai/enova-operator/internal/controller.(*EnovaServingReconciler).syncEnovaServing
	/workspace/enova-operator/internal/controller/enovaserving_controller.go:162
github.com/Emerging-AI/emgai/enova-operator/internal/controller.(*EnovaServingReconciler).Reconcile
	/workspace/enova-operator/internal/controller/enovaserving_controller.go:140
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller[...]).Reconcile
	/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:116
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller[...]).reconcileHandler
	/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:303
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller[...]).processNextWorkItem
	/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:263
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller[...]).Start.func2.2
	/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:224

As we can see, he prompts no such file or directory

so, i have look for the code

Image

i copy the code in my app

root, uerr := os.MkdirTemp(tempMountLocation, "containerd-mount")
if uerr != nil {
	return fmt.Errorf("failed to create temp dir: %w", uerr)
}

it's work

Image

So I don't understand why os. Mkdir Temp is obviously effective, but an error "no such file or directory" will be reported below.
Also, I don't understand that the same program works in localhost, but it doesn't work in pods.
my yaml:

spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      control-plane: controller-manager
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      annotations:
        cattle.io/timestamp: '2025-05-13T12:32:29Z'
        kubectl.kubernetes.io/default-container: manager
      creationTimestamp: null
      labels:
        control-plane: controller-manager
    spec:
      containers:
        - args:
            - '--metrics-bind-address=:8443'
            - '--leader-elect'
            - '--health-probe-bind-address=:8081'
          command:
            - /enova-operator
          env:
          image: >-
            operator:develop
          imagePullPolicy: Always
          livenessProbe:
            failureThreshold: 3
            httpGet:
              path: /healthz
              port: 8081
              scheme: HTTP
            initialDelaySeconds: 15
            periodSeconds: 20
            successThreshold: 1
            timeoutSeconds: 1
          name: manager
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: /readyz
              port: 8081
              scheme: HTTP
            initialDelaySeconds: 5
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 1
          resources:
            limits:
              cpu: 500m
              memory: 128Mi
            requests:
              cpu: 10m
              memory: 64Mi
          securityContext:
            privileged: true
            runAsGroup: 0
            runAsUser: 0
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      imagePullSecrets:
        - name: gcr-json-key
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext:
        runAsUser: 0
      serviceAccount: enova-operator-controller-manager
      serviceAccountName: enova-operator-controller-manager
      terminationGracePeriodSeconds: 10

Steps to reproduce the issue

Describe the results you received and expected

What version of containerd are you using?

(base) root@eai4090x4m1:/tmp# containerd --version containerd github.com/containerd/containerd/v2 v2.0.5 fb4c30d

Any other relevant information

No response

Show configuration if it is related to CRI plugin.

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions