---
myst:
html_meta:
"description lang=en": "Learn how to pre-load workspace images on Docker agents to speed up autoscaling in Kasm Workspaces. This provider-agnostic guide shows how to generate a Kasm API key, fetch image metadata, and prepare a Linux VM template with pre-pulled Docker images for faster workspace launch times."
"keywords": "Kasm, autoscaling, Docker, VM template, cloud-init, workspace images, Kubernetes, Linux VM, pre-pulled images, API key"
"property=og:locale": "en_US"
---
```{title} Pre-load Workspace Images on Agents
```
# Pre-load Workspace Images on Agents
```{contents} Table of Contents
:depth: 3
:local:
```
To optimize autoscaling of Docker agents, you can pre-load workspace images on your agents so that you don't have to wait for Kasm to pull your workspace images after the agent is provisioned. You can configure this during the creation of your Linux VM template, irrespective of the VM provider.
## Create a Kasm API key
- Go to your Kasm Admin Dashboard -> "Settings" -> "Developers"
- Click "Add API Key"
- Give your API key a name
- Enable `Read Only`
- Set an Expiration date for your API key (Optional)
```{figure} /images/autoscaling/create_api_key_image_read.png
:align: center
**Create API key to view images**
```
- This will generate your `API KEY` and `API KEY SECRET`. Save them securely as you cannot see these values again
```{figure} /images/autoscaling/create_api_key_image_read2.png
:align: center
**Save your API Key and Secret**
```
- Next, give your API key the `Images View` permission and click "Submit"
```{figure} /images/autoscaling/create_api_key_image_read3.png
:align: center
**Give "Images View" permission to your API key**
```
## Fetch and pull your Workspace Images
Run the following bash script that does the following:
- Install Docker CE (based on instructions from [https://docs.docker.com/engine/install/ubuntu](https://docs.docker.com/engine/install/ubuntu))
- Export Kasm Variables (`KASM_HOST`, `API_KEY`, `API_KEY_SECRET`)
- Fetch the Workspaces Images list from your Kasm deployment
- Pull the Workspace Images with `docker pull`
This script assumes you are running Ubuntu OS on your agents as listed [here](https://docs.docker.com/engine/install/ubuntu/#os-requirements). If you are using a different Linux distro, make sure to change the docker installation logic accordingly.
Make sure to replace following variables with the actual values from your Kasm deployment:
- `KASM_HOST`: The IP/FQDN of your Kasm deployment. If you're using a multi-server setup, this is the IP/FQDN of your Kasm Web App role.
- `API_KEY`: The API Key you generated earlier.
- `API_KEY_SECRET`: The API Key Secret you generated earlier.
```bash
# Install Docker on Ubuntu (ref: https://docs.docker.com/engine/install/ubuntu/)
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install -y docker-ce
# Kasm API variables
export API_KEY=
export API_KEY_SECRET=
export KASM_HOST=
# Fetch and pull all images
curl -sk -X POST "https://${KASM_HOST}/api/admin/get_images" \
-H "Content-Type: application/json" \
-d "{\"api_key\":\"${API_KEY}\",\"api_key_secret\":\"${API_KEY_SECRET}\"}" \
| jq -r '.images[] | select(.name != null) | .name' \
| while read -r image_name; do
echo "Pulling image: $image_name"
sudo docker pull "$image_name"
done
```
After pulling all the required workspace images, you can shut down the VM and create a template from it. When Kasm triggers autoscaling and provisions new agent VMs from this template, the workspace images will already be pre-loaded. This eliminates the delay caused by image downloads during provisioning, resulting in faster workspace launches.