Containerizing the Application
Before you can deploy your application to Kubernetes, you first need to build your application into a container and produce a container image. This packages your application along with all of its dependencies in a ready-to-run format.
The Nodeshift team provides a Docker project that provides several Dockerfile templates that can be used to build your container and produce your image.
For this learning path, you'll use the Dockerfile template, which builds a basic Docker image for your application.
Build a Docker image for your Express.js application using the following steps:
- Copy the Dockerfile template into the root of your project:
curl -fsSL -o Dockerfile https://raw.githubusercontent.com/NodeShift/docker/main/Dockerfile
- Also, copy the .dockerignore file into the root of your project:
curl -fsSL -o .dockerignore https://raw.githubusercontent.com/NodeShift/docker/main/.dockerignore
- Build the Docker run image for your application:
docker build --tag lholmquist/nodeserver:1.0.0 --file Dockerfile .
note: I used my username lholmquist as the prefix for the image name. Feel free to change this to your username.
You have now built a container image for your application called lholmquist/nodeserver with a version of 1.0.0
You can use the following command to run your application inside the container:
docker run --interactive --publish 3000:3000 --tty lholmquist/nodeserver:1.0.0
This runs your container image in a Docker container, mapping port 3000 from the container to port 3000 on your laptop so that you can access the application.
Visit your application's endpoints to check that it is running successfully:
- Homepage: http://localhost:3000/
- Liveness: http://localhost:3000/health