Skip to content
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
99 lines (70 sloc) 3.61 KB
title layout
Optimizing a Tiltfile

This tutorial looks at a Tiltfile with build optimizations. We explain what they do, and why you would want to use them.

In the Tutorial, we introduced the docker_build() function. This function builds a Docker image. Tilt will watch the inputs to the image, and rebuild it every time they change.

This works well for interpreted languages like JavaScript and Python where you can add the files and go. For servers that need to be compiled, it would be too slow to recompile from scratch every time.

That's why Tilt has a function fast_build() for lightning-fast local Kubernetes development.

Let's look at an example in the tiltdemo repo:

git clone
cd tiltdemo

The Tiltfile at the root of the repo contains this example:

# tiltdemo1
dm1_img_name = ''
(fast_build(dm1_img_name, 'Dockerfile', '/go/bin/demoserver1')
  .run('go install'))

This looks similar to the Tiltfile in previous tutorials, but instead of building with docker_build(), it contains fast_build(). Let's zoom in on that part of the function.

(fast_build(dm1_img_name, 'Dockerfile', '/go/bin/demoserver1')
  .run('go install'))

These lines configure tilt to do incremental image builds. We'll step through it line-by-line.

  • fast_build(dm1_img_name, 'Dockerfile', '/go/bin/demoserver1')

fast_build begins the build. This is setting up the build environment before we add any code. We build on top of the image in Dockerfile. Our new image has the name in dm1_img_name and has an entrypoint /go/bin/demoserver1.

Here's what's in Dockerfile:

FROM golang:1.10

It's only one line! This line says we're starting in a golang:1.10 container.

Fast build Dockerfiles cannot contain any ADD or COPY lines. It's only for setting up the environment, not for adding your code. So this Dockerfile might look different than most.

  • add('./cmd/demoserver1', '/go/src/')

The add method copies a directory from outside your container to inside of your container.

In this case, we copy the directory ./cmd/demoserver (relative to the Tiltfile) into the container filesystem.

While Tilt is running, it watches all files in ./cmd/demoserver. If they change, it copies the file into the container.

  • run('go install')

The run method runs shell commands inside your container.

Every time a file changes, Tilt will run this command again.

One of the major build optimizations that Tilt does is to keep the container running, and start the command inside the running container.

This is much closer to how we normally run commands for local development. Real humans don't delete all their code and re-clone it from git every time we need to do a new build! We re-run the command in the same directory. Modern tools then take advantage of local caches. Tilt runs commands with the same approach, but inside a container.

In this guide, we explored just a few of the functions we can use in a Tiltfile to keep your build fast. For even more functions and tricks, read the complete Tiltfile API reference.

You can’t perform that action at this time.