sysbox icon indicating copy to clipboard operation
sysbox copied to clipboard

Mounting directory from host fails

Open d-mankowski-samsung opened this issue 5 years ago • 8 comments

Mounting directory from host to container running in Swarm mode (one node Swarm) fails and it seems that behavior is a bit inconsistent. One time it fails on container startup, the other time it mounts directory with incorrect ownership.

When using Docker's runc - it works as expected.

MWE:

root@test:~# cat docker-compose.yaml
version: "3.8"

services:
  test:
    image: alpine:latest
    deploy:
      mode: replicated
      replicas: 1
    command: 'tail -f /dev/null'
    volumes:
      - /var/lib/docker:/var/lib/docker:ro
root@test:~# docker swarm init && docker stack deploy -c docker-compose.yaml test
Creating network test_default
Creating service test_test
root@test:~# docker node ps --no-trunc
ID                          NAME              IMAGE                                                                                   NODE      DESIRED STATE   CURRENT STATE            ERROR
                                                                                                                       PORTS
xtdvpaqr28ptb25jfzlv13gl3   test_test.1       alpine:latest@sha256:69e70a79f2d41ab5d637de98c1e0b055206ba40a8145e7bddb55ccc04e13cf8f   test      Running         Running 20 seconds ago

hgmyesbpsxph551bd9m3r4l8v    \_ test_test.1   alpine:latest@sha256:69e70a79f2d41ab5d637de98c1e0b055206ba40a8145e7bddb55ccc04e13cf8f   test      Shutdown        Failed 39 seconds ago    "starting container failed: OCI runtime create failed: error in the container spec: invalid mount config: failed to request mount source preps from sysbox-mgr: failed to invoke PrepMounts via grpc: rpc error: code = DeadlineExceeded desc = context deadline exceeded: unknown"

After a while this container did start, however the behavior is unexpected:

CONTAINER ID   IMAGE           COMMAND               CREATED              STATUS              PORTS     NAMES
8ac5cf4fa108   alpine:latest   "tail -f /dev/null"   About a minute ago   Up About a minute             test_test.1.xtdvpaqr28ptb25jfzlv13gl3
root@test:~# docker exec -it 8a sh
/ # cd /var/lib/docker/overlay2/
/var/lib/docker/overlay2 # ls
ls: can't open '.': Permission denied
/var/lib/docker/overlay2 # cd ..
/var/lib/docker # ls -l
total 52
drwx--x--x    4 nobody   nobody        4096 Jun  6 12:28 buildkit
drwx-----x    7 nobody   nobody        4096 Jun  9 09:29 containers
drwx------    3 nobody   nobody        4096 Jun  6 12:28 image
drwxr-x---    3 nobody   nobody        4096 Jun  6 12:28 network
drwx-----x   70 nobody   nobody       12288 Jun  9 09:29 overlay2
drwx------    4 nobody   nobody        4096 Jun  6 12:28 plugins
drwx------    2 nobody   nobody        4096 Jun  9 09:22 runtimes
drwx------    5 nobody   nobody        4096 Jun  9 09:22 swarm
drwx------    2 nobody   nobody        4096 Jun  9 09:22 tmp
drwx------    2 nobody   nobody        4096 Jun  6 12:28 trust
drwx-----x    9 nobody   nobody        4096 Jun  9 09:22 volumes
/var/lib/docker # whoami
root
/var/lib/docker # id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),11(floppy),20(dialout),26(tape),27(video)

On host, all these folders are owned by root:root (uid and gid 0).

root@test:~# cat /etc/docker/daemon.json
{
    "default-runtime": "sysbox-runc",
    "runtimes": {
        "sysbox-runc": {
            "path": "/usr/bin/sysbox-runc"
        }
    }
}

Sysbox version: HEAD (8ddb036).

root@test:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.2 LTS
Release:        20.04
Codename:       focal
root@test:~# uname -a
Linux test 5.4.0-74-generic #83-Ubuntu SMP Sat May 8 02:35:39 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
root@test:~# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
  scan: Docker Scan (Docker Inc., v0.8.0)

Server:
 Containers: 3
  Running: 0
  Paused: 0
  Stopped: 3
 Images: 36
 Server Version: 20.10.7
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: active
  NodeID: up73h9hp27xkhizwdf5lrd7si
  Is Manager: true
  ClusterID: rszlmdwxumeiuhzy46k9qm9cd
  Managers: 1
  Nodes: 1
  Default Address Pool: 10.0.0.0/8
  SubnetSize: 24
  Data Path Port: 4789
  Orchestration:
   Task History Retention Limit: 5
  Raft:
   Snapshot Interval: 10000
   Number of Old Snapshots to Retain: 0
   Heartbeat Tick: 1
   Election Tick: 10
  Dispatcher:
   Heartbeat Period: 5 seconds
  CA Configuration:
   Expiry Duration: 3 months
   Force Rotate: 0
  Autolock Managers: false
  Root Rotation In Progress: false
  Node Address: 10.0.0.180
  Manager Addresses:
   10.0.0.180:2377
 Runtimes: io.containerd.runtime.v1.linux runc sysbox-runc io.containerd.runc.v2
 Default Runtime: sysbox-runc
 Init Binary: docker-init
 containerd version: d71fcd7d8303cbf684402823e425e9dd2e99285d
 runc version:
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 5.4.0-74-generic
 Operating System: Ubuntu 20.04.2 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 7.775GiB
 Name: test
 ID: O4JQ:UZCX:ZSNV:SJJQ:FEW7:34MD:6S3J:3IPN:LH5L:O5QP:247V:6B35
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

```

d-mankowski-samsung avatar Jun 09 '21 09:06 d-mankowski-samsung

Also, I would like to do the following: Lets say we have multiple containers with Jenkins agents capable of docker-in-docker using sysbox runtime. And now, I would like to share layers of docker images that each agent builds/pulls between all of the agents. My initial idea was to mount - /var/lib/docker:/var/lib/docker to every container, however, as can be seen above, this doesn't work.

My next attempt was to create a docker volume (docker volume create docker_images) and mount it to each container ( - docker_images:/var/lib/docker ), however, when trying to build an image (inside Jenkins agent) I am getting:

+ docker image build -t test Dockerfile .
Sending build context to Docker daemon  14.82MB

Step 1/6 : FROM alpine:3.13.0

3.13.0: Pulling from library/alpine
596ba82af5aa: Pulling fs layer
596ba82af5aa: Verifying Checksum
596ba82af5aa: Download complete

596ba82af5aa: Pull complete
Digest: sha256:d9a7354e3845ea8466bb00b22224d9116b183e594527fb5b6c3d30bc01a20378
Status: Downloaded newer image for alpine:3.13.0
 ---> 7731472c3f2a
Step 2/6 : RUN addgroup -S test && adduser -S test -G test
 ---> Running in b7a1d2a71f5d

addgroup: warning: can't create backup copy '/etc/group-': No error information
lstat /var/lib/docker/overlay2/5c4ebc6a04348b0b3596c9f662597f0a590aea6b1aa33af22fa75c6912eaff4b/merged/etc: object is remote

Do you have some idea how can this be achieved?

d-mankowski-samsung avatar Jun 09 '21 14:06 d-mankowski-samsung

Hi @d-mankowski-samsung,

Thanks for filing this new issue; I'll take a look at reproducing the problem to see what's going on.

Now, regarding:

And now, I would like to share layers of docker images that each agent builds/pulls between all of the agents. My initial idea was to mount /var/lib/docker:/var/lib/docker to every container, however, as can be seen above, this doesn't work.

This won't work (unfortunately) because each Docker instance needs a dedicated /var/lib/docker (it can't be shared among multiple Docker instances). This is a limitation of the Docker daemon.

Mounting a host dir into the sysbox container's /var/lib/docker is useful as a way of caching images on the host in case a given sysbox container starts, stops, then restarts again. But it can't be used for sharing images across multiple Docker daemons running inside multiple sysbox containers.

By the way, one related feature in Sysbox is that it allows the outer container image to be preloaded with inner container images. That is, you can create a sysbox container that comes preloaded with many inner container images. This is helpful when you know ahead-of-time what those inner container images should be, and voids the need to pull them from the network everytime. Not sure if this is helpful in your case, thought I would mention it.

Sysbox Enterprise pushes that a step further and allows those preloaded inner images to be shared across multiple Sysbox containers using copy-on-write. But this only works for preloaded images, not for images downloaded at runtime by the inner Docker.

Hope that helps!

ctalledo avatar Jun 09 '21 16:06 ctalledo

Hi @d-mankowski-samsung,

Looking more closely at the docker-compose.yaml for you docker stack, I see the problem: it's mounting the host's /var/lib/docker into the container's /var/lib/docker.

 volumes:
      - /var/lib/docker:/var/lib/docker:ro

This is not good because you should never expose the host's docker-cache into the container: it breaks container isolation since the container can essentially see all other sibling containers on the host, and (as mentioned in my prior comment) the docker cache can't be shared among docker daemons (i.e., the host's docker daemon can't share it's cache with any other docker instance). I see you mounted it read-only which is better, but it's still not a good idea.

Having said this, Sysbox should do a better job of detecting this situation and failing with a more appropriate message. I'll look into that.

Now, you can mount an arbitrary host directory to the sysbox container and things should work. For example, this worked:

 volumes:
      - /home/someone/docker-cache:/var/lib/docker

I verified that you can deploy the docker stack, use the inner docker to pull some images (which get implicitly store in the host's cache at /home/someone/docker-cache), remove the docker stack, then restart the docker stack, and verify that the inner docker sees the images pulled by the prior stack instance.

ctalledo avatar Jun 09 '21 17:06 ctalledo

Thanks for the explanation! Now I see that I should have read documentation more carefully - it is all there 👍🏻 Great project by the way. I think this can be closed then.

d-mankowski-samsung avatar Jun 09 '21 18:06 d-mankowski-samsung

Great project by the way. I think this can be closed then.

Thanks @d-mankowski-samsung!

Thank you for filing the issues and please let us know if we can help in any other way ...

ctalledo avatar Jun 09 '21 19:06 ctalledo

@ctalledo

By the way, one related feature in Sysbox is that it allows the outer container image to be preloaded with inner container images. That is, you can create a sysbox container that comes preloaded with many inner container images. This is helpful when you know ahead-of-time what those inner container images should be, and voids the need to pull them from the network everytime. Not sure if this is helpful in your case, thought I would mention it.

A more generic and dynamic solution that I've decided to implement is using a private registry acting as a proxy between all the daemons and DockerHub, https://docs.docker.com/registry/recipes/mirror/ - maybe it would be a good idea to mention it in the docs?

d-mankowski-samsung avatar Jun 10 '21 10:06 d-mankowski-samsung

Hi @d-mankowski-samsung,

A more generic and dynamic solution that I've decided to implement is using a private registry acting as a proxy between all the daemons and DockerHub, https://docs.docker.com/registry/recipes/mirror/ - maybe it would be a good idea to mention it in the docs?

Good idea, that should work; I'll add a note on this in the docs for future reference. Thanks!

ctalledo avatar Jun 10 '21 17:06 ctalledo

I'll add a note on this in the docs for future reference.

Text is here:

https://github.com/nestybox/sysbox/blob/master/docs/quickstart/dind.md#caching-docker-images-among-multiple-docker-in-docker-instances

ctalledo avatar Jun 10 '21 18:06 ctalledo