Dagger & Dockerfile
We build packages, use Dagger commands, but why reinvent the wheel?
We are used to Dockerfiles, and all our projects already utilize them. So, let’s reuse this code with Dagger!
Let’s try to build our Dockerfile and publish it to a private registry like Scaleway.
Dockerfile
For now, I’ll start with a very simple Dockerfile. (Yes, it’s not using a Node.js user, but it’s just for the demo.)
Here it is:
Daggerization
Initialization
To daggerize our project, we’ll start by initializing Dagger with the TypeScript SDK:
Then, in dagger.json
, I’ll exclude node_modules
to avoid copying them during development.
This can cause issues between different OS and Node.js versions.
Build
We’ll first create a function to install dependencies.
This function allows us to return the directory with the installed dependencies for later use.
Let’s test it:
Great! Now let’s build the Dockerfile.
Building the Dockerfile
Before trying to publish, let’s test the container live from Dagger.
Is that possible?
Yes! You can create services and expose them on your host. If you don’t expose them, you can use the service as an HTTP endpoint for your other functions.
I want to expose this as a service to verify that it works and the Dockerfile is properly applied.
If we simply run the following command, the service will start but won’t be accessible on our host:
Let’s expose the service using --ports
:
Our container is responding properly!
Perfect! Now, let’s revert it to container mode:
And let’s test exposing it on our host in container mode:
It works! 😲
Conclusion
We now have a function that builds using the Dockerfile. For publishing to a private registry, check out the next page!
I prefer to separate topics to keep the information digestible and well-structured.
Full Dagger Code
Here’s the complete index.ts
for Dagger:
GitHub Repository
https://github.com/Killian-Aidalinfo/node-api-demo-dockerfile