Build a Vultr Marketplace application image from an instance snapshot using the Vultr Console or Vultr API to package a configured server as your app.
Building from a snapshot means you pre-install your application on a server, set up post-deployment scripts, and take a snapshot of the disk image. When customers deploy your app, they get your snapshot, and cloud-init runs your scripts. Prefer building from Vendor Data when your application supports it, since it avoids maintaining a snapshot.
This guide explains how to prepare a server and build an image from its snapshot using the Vultr Console or the Vultr API.
To open the snapshot selector, click Marketplace in the Vultr Console's left-hand menu, click the application name, click the Builds tab, then click the Build From Snapshot button to switch from the default Vendor Data view.
Optimize the server before you take the snapshot:
marketplace-2c-2gb plan, which is optimized for Marketplace applications. This plan includes 2 vCPUs, 2 GB of memory, and 8 GB of storage.Adapt the following code snippets to your needs and your OS distribution. These steps are also available as an example shell script in our GitHub repository.
Create the /tmp directory if it doesn't already exist.
# mkdir /tmp
Set the proper permissions on the directory.
# chmod 1777 /tmp
For yum-based distributions:
Update installed packages.
# yum update -y
Clean the package cache.
# yum clean all
For apt-based distributions:
Refresh the package index.
# apt-get -y update
Upgrade installed packages.
# apt-get -y upgrade
Remove packages that are no longer needed.
# apt-get -y autoremove
Clean the package cache.
# apt-get -y autoclean
Clean the temporary directories.
Clean /tmp.
# rm -rf /tmp/*
Clean /var/tmp.
# rm -rf /var/tmp/*
Clean SSH keys.
Remove the authorized keys and existing host keys.
# rm -f /root/.ssh/authorized_keys /etc/ssh/*key*
Create an empty revoked keys file.
# touch /etc/ssh/revoked_keys
Set the proper permissions on the revoked keys file.
# chmod 600 /etc/ssh/revoked_keys
Clean the logs.
Truncate logs modified in the last day.
# find /var/log -mtime -1 -type f -exec truncate -s 0 {} \;
Remove compressed log archives.
# rm -rf /var/log/*.gz
Remove numbered rotated logs.
# rm -rf /var/log/*.[0-9]
Remove date-stamped rotated logs.
# rm -rf /var/log/*-????????
Clear the auth log.
# echo "" >/var/log/auth.log
Clean old cloud-init information.
# rm -rf /var/lib/cloud/instances/*
Clean the session history.
Clear the current shell session's history.
# history -c
Clear the saved history file.
# cat /dev/null > /root/.bash_history
Unset the history file variable so nothing is written back to it before the snapshot.
# unset HISTFILE
Update the mlocate database.
# /usr/bin/updatedb
Wipe the random seed file.
# rm -f /var/lib/systemd/random-seed
Distributions that use systemd must wipe the machine identifier to prevent boot problems.
Remove the existing machine ID. Some distributions keep a separate copy at /var/lib/dbus/machine-id instead of symlinking it to /etc/machine-id, so remove both.
# rm -f /etc/machine-id /var/lib/dbus/machine-id
Create an empty replacement file. systemd generates a new machine ID the next time the system boots.
# touch /etc/machine-id
Clear the last login log.
# cat /dev/null > /var/log/lastlog
Clear the login/logout record.
# cat /dev/null > /var/log/wtmp
Wipe unused disk space with zeros for security and compression.
Fill the free space with a zeroed file.
# dd if=/dev/zero of=/zerofile
The command runs until the disk is full and exits with a No space left on device error. This is the expected result.
Flush the write to disk.
# sync
Remove the zeroed file.
# rm /zerofile
Flush the deletion to disk.
# sync
Not all Linux distributions support fstrim, but you may consider running this final step before making the snapshot.
# fstrim /
Vultr can snapshot a running server, but you may prefer a clean shutdown. If so, consider halting and powering off the server before taking the snapshot.
# shutdown -h now
To power off:
Navigate to the Add Snapshot page in the Vultr Console.
Select your instance in the dropdown.
Enter a descriptive label.
Click Take Snapshot. Wait a few minutes for the snapshot to complete.
Click Marketplace in the left-hand menu, then click the application name.
Click the Builds tab.
Select the snapshot image you just created.
Click Build App Image. The Vultr Marketplace takes your snapshot and builds it into an App Image.
Send a POST request to the Create Marketplace App Build Image endpoint with use_vendor_data set to false. Replace {app-id} with the app's ID and SNAPSHOT-ID with the ID of the snapshot you prepared.
$ curl "https://api.vultr.com/v2/marketplace/apps/{app-id}/builds" \
-X POST \
-H "Authorization: Bearer ${VULTR_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"use_vendor_data": false,
"snapshot_id": "SNAPSHOT-ID"
}'
A successful request returns an HTTP 201 Created response with the build's marketplace_image_id.