Build on Docker


#1

Building snaps within an isolated environment, such as Docker or LXD, has several advantages over a native build:

  • containers keep your host system clear of your new snap’s dependencies
  • your host system can’t expose conflicting libraries to your new snap build

Take a look at Building on LXD to learn how to build snaps with LXD, and read on to discover how to build snaps with Docker.

Install Docker

To get started, first check whether Docker is already installed. This can be done with the following command (look for Hello from Docker in the output):

$ docker run hello-world
...
Hello from Docker!
This message shows that your installation appears to be working correctly.
...

If Docker isn’t installed, take a look at Install Docker. Ubuntu users can alternatively step through the following:

Install Docker on Ubuntu 16.04 LTS (or later)
  1. Update the package details held on your system:

     $ sudo apt-get update
    
  2. Download and add Docker’s third-party repository key:

    $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    
  3. Next, add Docker’s package repository:

    $ sudo add-apt-repository -u \
       "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
       $(lsb_release -cs) \
       stable"
    
  4. You can now install Docker from the new packages:

    $ sudo apt-get install docker-ce
    
  5. Finally, add your user to the docker group:

    $ sudo usermod -a -G docker $USER
    

For more information, see Docker’s Install using the repository documentation.


ⓘ If snapcraft is not yet installed, enter sudo snap install snapcraft --classic.

Building with Docker

With Docker installed, first pull the latest snapcraft image:

$ docker pull snapcore/snapcraft

Make sure you’re in your project’s top-level directory, where snap/snapcraft.yaml describes how to build your snap, and enter the following command to build your snap:

$ docker run --rm -v "$PWD":/build -w /build snapcore/snapcraft bash \
      -c "apt update && snapcraft"

That’s all there is to it. If the build completes successfully, you will find a .snap file in the current directory. You can inspect its contents to ensure the snap contains all of your application’s assets:

$ unsquashfs -l *.snap

Snap Documentation
Snapcraft overview
#2

You can also use the snapcore/snapcraft image.


#3

not certain what that means.

Do you mean there is a docker snapcore/snapcraft image ?


#4

Indeed: https://hub.docker.com/r/snapcore/snapcraft/


#5

This topic duplicates with Building the snap on Docker, maybe merge to one of them?


#6

I agree, I think this topic should be closed and point to the other one

Also, the other topic reads better on docs.snapcraft.io: https://docs.snapcraft.io/building-the-snap-on-docker/6757 reads better and is more consistent than https://docs.snapcraft.io/build-on-docker/4158, which also appears to suffer from some formatting issues converting from here


#7

Hello,

I think this doc should be superseded by https://docs.snapcraft.io/building-the-snap-on-docker/6757

First of all, it gives docker installation instruction which seems out of scope (and a moving target depending on version of ubuntu and docker).

Then it uses sudo to interact with docker, which is not needed if the user is in the docker group.

Finally, it forgets to mention the command to be run against the docker container, so:

$ sudo docker run -v "$PWD":/snapcraft_build \
       -w /snapcraft_build snapcore/snapcraft 
$ snapcraft

This doesn’t result in an interactive shell, so either it should be something like (-it was added to Keep STDIN open even if not attached and Allocate a pseudo-TTY; we could also add --rm to clean after the operation):

$ docker run -it -v "$PWD:/snapcraft_build" -w /snapcraft_build snapcore/snapcraft
# snapcraft       // <-- this is being executed within the container

Or something like the other page suggests (different folder names don’t matter, but snapcraft at the end does).

$ docker run -v "$PWD":/build -w /build snapcore/snapcraft snapcraft

Another things is that we need more than just snapcraft, see Building the snap on Docker

I think this should be updated to mention that before running snapcraft from the container, apt update needs to be run, because as it is the snapcore/snapcraft docker image has out of date apt sources and as such snapcraft will fail to install/download packages when using apt because the sources/sites are out of date

What do you folks think?


Building the snap on Docker
#8

Thanks for the input on this important topic. And yes, we should have only a single doc for Docker - and make sure it works - I’ll look into merging the two together, or reverting to the newer version, as suggested.


#9

The docker command should probably read docker run --rm -v ... otherwise each run will leave a container behind.


#10

Great point - thanks! I’ll add it to the command.