Install snap in Docker

I try to install snap in a Docker container. The OS is CentOS 8. For a project I have to use the Mumble client. The idea is to install snap and mumble in a Docker container .

I use the post as a template for my Docker file.
Snapd in Docker

I get this error message:

Step 7/9 : RUN systemctl enable --now snapd.socket
 ---> Running in 4271707f5477
Created symlink /etc/systemd/system/sockets.target.wants/snapd.socket → /usr/lib/systemd/system/snapd.socket.
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
The command '/bin/sh -c systemctl enable --now snapd.socket' returned a non-zero code: 1

here is the Dockerfile

FROM centos:latest

ENV container docker
ENV PATH /snap/bin:$PATH
ADD snap /usr/local/bin/snap

RUN dnf update -y  \
    && dnf install -y epel-release \
    && dnf install -y snapd \
    && dnf clean all \
    && rm -rf /var/cache/dnf

RUN systemctl enable --now snapd.socket
RUN ln -s /var/lib/snapd/snap /snap

CMD [ "/sbin/init" ]

It is not possible to run snapd inside a container, is there any problem why it can’t be installed directly on the host system?

Please read this

$ cat Dockerfile
FROM ubuntu:16.04
ENV container docker
ENV PATH /snap/bin:$PATH
ADD snap /usr/local/bin/snap
RUN apt-get update
RUN apt-get install -y snapd squashfuse fuse
RUN systemctl enable snapd
STOPSIGNAL SIGRTMIN+3
CMD [ "/sbin/init" ]

And:

$ cat snap
#!/bin/sh -e

while ! kill -0 $(pidof snapd) 2>/dev/null; do
  echo "Waiting for snapd to start."
  sleep 1
done

/usr/bin/snap $@

$ chmod +x snap

Now, build it:

$ docker build -t snapd . 

Run it:

$  docker run --name=snapd -ti -d \                                                                                     
  --tmpfs /run --tmpfs /run/lock --tmpfs /tmp \
  --privileged \ # [1]
  -v /lib/modules:/lib/modules:ro \ # [2]
  snapd

And install some snaps:

$ docker exec -it snapd snap install emoj
$ docker exec -it snapd emoj success
✔  ✅  ☑  📚  👌  🎓  💰

Notes:

  1. Otherwise systemd complains about /sys not being writable when reloading udev rules (ConditionPathIsReadWrite=/sys was not met)
  2. Otherwise strictly confined snaps fail to execute:
    $ docker exec -it snapd emoj
    cannot perform operation: mount --rbind /lib/modules /tmp/snap.rootfs_NCx2ET//lib/modules: No such file or directory

I don’t use centos, but looking to do similar on Arch for CI purposes.

I stumbled across this https://github.com/gdraheim/docker-systemctl-replacement which seems promising.

I’m immediately running into issues with your Dockerfile before getting to Step 7, but it might help. I’m still having issues myself, but I’m getting some systemctl functionality.

If you’d like a reference (and I would not recommend this implementation for a production application), https://github.com/jmelahman/monorepo/blob/fdba0895785097979de4616901a3a0c01c3a54ff/docker/snapify/Dockerfile#L27-L29