0

I'm trying to figure out how to copy files from host to a container I started using Containerd ctr command.

Just for little bit of context: I've been using the image docker.io/bitnami/apache:latest for this exercise and as it is rootless, it makes things much more complicated. For example installing curl on the running container is really painful if not impossible.

ctr has a really poor documentation. Here is how I managed to start a container using ctr only and trying to connect onto it as root

ctr image pull docker.io/bitnami/apache:latest
ctr container create -t docker.io/bitnami/apache:latest bitnamiap
ctr containers ls # check container's created 
ctr task start -d bitnamiap # start it
ctr -n default task ls # check it is running
executor_id="bitnamiap" 

# connect as root but it won't help you much
ctr -n default tasks exec --tty --exec-id test1 --user=0 $executor_id bash 

I tried to use snapshot to create a mount with the container but to no avail. I can read files from the container but not write files to the container. I think this is by design and not a consequence of the rootless container because this post tends to confirm this is unidirectional: https://stackoverflow.com/a/70208804/12512199

mkdir -p /tmp/ctrtransfer
ctr -n default snapshot mounts /tmp/ctrtransfer "$executor_id" | sh

But I guess, with a little hacking, if ctr won't help us, we could access the container's write layer, working on ctr snapshot mount output.

NB:

  • I tried docker cp but it doesn't work on this example. But the point here is really to use containerd and runc tooling to achieve this, if possible
  • I haven't tried nerdctl because this is not the point. Or maybe someone can explain how nerdctl achieves it from the grounds up.
  • There's the option of nsenter and using network tools to wriggle my file somewhere onto the container but I'd rather understand the inner working of containerd to fathom how to do it using mounts or ctr api. Working with what you have on premises can turn out to be very useful

1 Answers1

0

Couldn't use ctr cli, so I investigated how Containerd builds the container from the image.

This is a hackish solution validated only for overlay fs:

# become root.
sudo su 

# Find container overlay mount
mount | grep bitnamiap
  # should output something like this
  # overlay on /run/containerd/io.containerd.runtime.v2.task/default/bitnamiap/rootfs type overlay (rw,relatime,lowerdir=...

# copy the file from host into the writable container layer.Here I'm copying curl binary.
cd /run/containerd/io.containerd.runtime.v2.task/default/bitna1/rootfs
cp "$(curl)" .

# Connect to the container and check it has worked
ctr -n default tasks exec --exec-id test1 --user=0 $executor_id ls -lrta /