Prerequisites
- Your app
- Docker to build standalone images to serve your production app.
Getting Started
Dockerizing your app ensures consistency, portability, isolation, and several other benefits. If you haven’t installed Docker already, you can find the installation instructions for various operating systems in the official docs. Let’s create a simple Node.js script that runs an HTTP server on port3000 and when the URL / is hit, it prints
Hello World!
app.js
Writing the Dockerfile
Now, let’s create a Dockerfile for the above app. ADockerfile is a set of instructions that tells Docker how to get
our app working in our environment.
Dockerfile
node_modules to be present while running the application. Prefer to
opt-in for multistage builds whenever possible.
In real-world scenarios, you might have more than just an HTTP server. You might have other services running
parallelly too, like schedulers, workers, notification services, gateways, etc,. You’ll need to create a Dockerfile for
each and link them while creating the Helm charts.You might also choose to run everything in the same container, which is mostly not recommended, since you might lose
isolation, scalability, etc.
.dockerignore
node_modules since when installing dependencies, few libraries might build
binaries depending on your platform. We don’t want them to be copied since it might replace the binaries built inside
the container itself while running npm ci, causing the application to break or misbehave.
Examples for Dockerfiles for Other Languages
GoLang
GoLang
You can put any content in here, including other components, like code:This Dockerfile uses a multistage build to compile a Go application in a Golang builder image and then runs it in a
lightweight Alpine image, ensuring a minimal final image size.A
Dockerfile
.dockerignore file for reference:Django (Python)
Django (Python)
This Dockerfile installs Django dependencies in one stage and then copies the dependencies and application code to a
slim Python image, optimizing the final container size and performance.A
Dockerfile
.dockerignore file for reference:Ruby on Rails
Ruby on Rails
This Dockerfile leverages a multistage build to install Ruby and system dependencies, precompile assets in a Ruby
builder image, and then run the Rails application in a slimmer Ruby image, reducing the overall image size.A
Dockerfile
.dockerignore file for reference:All these Dockerfiles are references to how you might package the application; actual steps might vary depending on
the application you build and the features you use.
Building Images
To build your Docker image, use the following command in the directory containing your Dockerfile:node-app image for platform linux/amd64 and tags it as latest. If you are locally testing
your application, you can skip the platform key to build the images
Running Your Docker Image
Now, we have images, its time run the image. Use the following command:-it: enables interactivity with TTY--rm: to tell the Docker Daemon to clean up the container and remove the file system after the container exits--name node-app: Name of the containernode-app.-e PORT=3000: Sets the environment variable PORT in docker3000.-d: Runs the container in detached(background) mode. You can skip the flag to see the logs directly in your terminal window.-p 3000:3000: Maps port 3000 on your host to port 3000 in the container.node-appin the end is the name of the image
3000 of the container to port 3000 on your local machine. Once the
service is up, visit http://localhost:3000 to see the app we built running.
Hurray 🎉. Now we have package our app for production use using docker.