Hi, I'm trying to understand the best way to build a Linux Docker image using Dagger functions.
In the hello-world example from the documentation there's a build function that uses the build_env function.
If I understand this correctly the build_env function creates a Docker container based on node:21-slim and then uses that container to build the nginx container.
Is my understanding correct?
I'm not clear on why a build_env is needed for building a Docker container. Can someone briefly explain why it's necessary?
If it is a best practice, is the node image the best build_env to use for general Docker image building?
` @function
def build(self, source: dagger.Directory) -> dagger.Container:
"""Build the application container"""
build = (
self.build_env(source)
.with_exec(["npm", "run", "build"])
.directory("./dist")
)
return (
dag.container()
.from_("nginx:1.25-alpine")
.with_directory("/usr/share/nginx/html", build)
.with_exposed_port(80)
)
@function
def build_env(self, source: dagger.Directory) -> dagger.Container:
"""Build a ready-to-use development environment"""
node_cache = dag.cache_volume("node")
return (
dag.container()
.from_("node:21-slim")
.with_directory("/src", source)
.with_mounted_cache("/src/node_modules", node_cache)
.with_workdir("/src")
.with_exec(["npm", "install"])
)`