I don't have a Docker Image
You are here because your awesome app is ready and now it’s time to package it.
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 port 3000
and when the URL /
is hit, it prints
Hello World!
We can run this app by running the following command:
That’s it, we have our Node.js app running.
Writing the Dockerfile
Now, let’s create a Dockerfile for the above app. A Dockerfile
is a set of instructions that tells Docker how to get
our app working in our environment.
As you can see, the Dockerfile is a set of instructions that automate the steps you would take manually to get the app running on your machine.
Though Docker supports Multi-Stage Builds, we won’t be using that
here since Node.js needs all the source code and 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.
It is heavily recommended to have a .dockerignore
file to ignore certain prebuilt binaries and non-project-related
stuff being copied from our local to the Docker container.
Read more on .dockerignore here. An example ignore file:
It is essential to ignore folders like 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 .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 .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 .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:
This command builds the 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-app
in the end is the name of the image
This command runs your Docker image, mapping port 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.
Next Steps
Once the docker is ready, the next steps will be