Build on Docker

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 -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

Last updated 14 hours ago. Help improve this document in the forum.