Snap that deploy a Docker image

Hi all,
I’m new to Snap and trying to create a snap that pulls and run my Docker image on Docker.
And if it possible to bundle the Docker inside the snap so the users won’t need to install Docker or know it’s necessary.

Do you have some examples that I can look as a reference?

Additionally, I found 2 interfaces relevant to Docker: docker interface and docker-support interface.
But I can’t understand from the description what can I perform with each one of them. Can you elaborate more details?

Thanks!

You should not need to use the docker-support interface at all. The docker interface will provide access to the Docker daemon socket to allow you to start and stop containers. You will need to either use the docker-executables content interface from the Docker snap or ship the relevant utilities or libraries you need to interact with Docker. An example of using the docker-executables interface is below (though I haven’t tested the correctness of it you should get a good idea of how it works):

plugs:
  docker-executables:
    interface: content
    target: $SNAP/docker-exes
    default-provider: docker:docker-executables

apps:
  my-app:
    command: ...
    plugs:
    - docker
    - docker-executables

Hi @daniel,
Really appreciate your help!

Found the following dockerized-app-snap repository on GitHub which really helped me to create a Snap that run a dockerzied app through the content-interface shared by the docker snap.

Attached my snapcraft.yaml for anyone who trying to do something similar:

name: my-app
version: '1.0.0'
summary: |
  my-app Summary
description: |
  Some my-app Description.

grade: devel
confinement: strict

plugs:
  docker-cli:
    interface: docker
  docker-executables:
    content: docker-executables
    default-provider: docker
    interface: content
    target: docker-env

parts:
  environment:
    plugin: dump
    source: ./src/
    organize:
      'docker-wrapper' : bin/

apps:
  my-app:
    command: docker-wrapper docker <any docker command ps/pull/run>
    plugs: [docker-executables, docker-cli]

You can find general explanation on Snap Interfaces(plugs and slots) here In short, an interface consists of a connection between a slot and a plug. The slot is the provider of the interface while the plug is the consumer. In my case, the docker snap has 2 interfaces that it provides(slots) which my-app consumes(plugs) - the docker interface under the docker-cli plug and the content interface under docker-executables plug.

Regarding the question if Docker must be installed via Snap. Because my-app snap uses the docker snap interfaces, the answer it yes. But I’m not sure if it’s conflict with a docker installed on the machine. will update when I have the answer.

Hoped I helped somebody!

Hi @Talzzz, thanks for posting this, very helpful.

I’m trying to follow these steps by adding this to my snapcraft.yaml:

  docker-cli:
    interface: docker
  docker-executables:
    content: docker-executables
    default-provider: docker
    interface: content
    target: docker-env

# ...

    plugs:
     # ...
      - docker-cli
      - docker-executables

And then after installing my snap I do:

snap connect dotrun:docker-executables docker:docker-executables

(Using the docker snap)

But when I try to run this, it fails:

> snap run --shell dotrun
$ /snap/dotrun/current/docker-env/bin/docker images
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/images/json: dial unix /var/run/docker.sock: connect: permission denied

Any idea why I’m getting permission denied? Is there a connection I’m missing? I couldn’t work out how to connect the docker/docker-cli interface to anything.

you need to connect docker-cli to docker-daemon from the docker snap as in

snap connect <your-snap>:docker-cli docker:docker-daemon

Worked, thanks. Should we add this to documentation somewhere? The only mention of the docker interface I found in the docs is incredibly sparse - doesn’t mention docker-executables or docker-daemon.

We do not have official docs on using the docker snap this way (or should not to my knowledge). I think a forum post is more appropriate for this information. BTW, I have an unofficial example of a snap doing this that could be used for an example: https://github.com/anonymouse64/docker-snap-usage-example