---
slug: /544174/multistage-build
displayed_sidebar: "current"
category: "guides"
tags: ["go", "python", "nodejs"]
authors: ["Kyle Penfound"]
date: "2022-11-09"
---

# Use Dagger with Multi-stage Container Builds

Multi-stage builds are a common practice when building containers with Docker.

- First, your application is compiled in a context which has tools that are required for building the application, but not necessarily required for running it.
- Next, to reduce the number of dependencies and hence the size of the image, the compiled application is copied to a different base image which only has the required components to run the application.

:::tip
[Learn more about multi-stage builds in the Docker documentation](https://docs.docker.com/build/building/multi-stage/).
:::

This guide explains how to perform multi-stage builds with the Dagger SDKs.

## Requirements

This guide assumes that:

- You have a Go, Python or Node.js development environment. If not, install [Go](https://go.dev/doc/install), [Python](https://www.python.org/downloads/) or [Node.js](https://nodejs.org/en/download/).
- You have a Dagger SDK installed for one of the above languages. If not, follow the installation instructions for the Dagger [Go](../sdk/go/371491-install.mdx), [Python](../sdk/python/866944-install.mdx) or [Node.js](../sdk/nodejs/835948-install.mdx) SDK.
- You have Docker installed and running on the host system. If not, [install Docker](https://docs.docker.com/engine/install/).
- You have an application that you wish to build. This guide assumes a Go application, but you can use an application of your choice.

## Example

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

The following code snippet demonstrates a multi-stage build.

<Tabs groupId="language" className="embeds">
<TabItem value="Go">

```go file=./snippets/multistage-build/main.go
```

</TabItem>
<TabItem value="Node.js">

```typescript file=./snippets/multistage-build/index.mts
```

</TabItem>
<TabItem value="Python">

```python file=./snippets/multistage-build/main.py
```

</TabItem>
</Tabs>

This code listing starts by creating a Dagger client and loading the project to be built. It obtains a reference to the project and then builds the application by using the `golang:latest` image to mount the source directory, sets `CGO_ENABLED=` since the binary will be published on `alpine`, and executes `go build`.

Next, the multi-stage build is achieved by transferring the build artifact from the builder image to a runtime image based on `alpine`. The steps are:

- Create a new container image which will be used as the runtime image.
- Transfer the build artifact from the builder image to the new container image.
- Set the container entrypoint to the application so that it is executed by default when the container runs.

The final optimized image can now be pushed to a registry and deployed!

## Conclusion

This tutorial walked you through the process of performing a multi-stage build with Docker.

Use the [API Key Concepts](../api/975146-concepts.mdx) page and the [Go](https://pkg.go.dev/dagger.io/dagger), [Node.js](../sdk/nodejs/reference/modules.md) and [Python](https://dagger-io.readthedocs.org/) SDK References to learn more about Dagger.
