ZNone
ZNone

Reputation: 71

How to create a Snap package that uses Docker to pull and run a Docker images?

I have a Docker image and a cli tool. I want to create a Snap package that pulls the Docker image and run it on the local Docker. I already have a snapcraft.yaml that installs the cli tool. I'm trying to understand what should I add/edit so I can call Docker actions.

Additionally, I'm trying to understand if in such case the Docker must be installed via Snap or as long as Docker is somehow installed on the machine everything is fine? What happens when there is no Docker installed?

From what I'v found on Snap Docs, I need to add to my snapcraft.yaml the docker interface so it will provide access to the Docker deamon socket, but I can't find any resources how to call Docker commands...

This is my snapcraft.yaml:

version: '1.0.0'
summary: |
  Test CLI and Service
description: |
  Some Test Description.

grade: devel
confinement: strict

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

parts:
  jre:
    source-type: tar
    source: ./jre-source/zulu11.33.10-sa-jre11.0.4-linux_x64.tar.gz
    plugin: dump
  test-snap:
    source-type: local
    source: ./test-snap-source
    plugin: dump
apps:
  test-snap:
    command: docker ps
    plugs: 
      - docker
      - docker-executables
      - docker-cli

When I run the test-snap I'm getting "/snap/test-snap/x6/command-test-snap.wrapper: 4: exec: docker: not found"

Thanks!

Upvotes: 2

Views: 494

Answers (1)

ZNone
ZNone

Reputation: 71

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!

Upvotes: 2

Related Questions