Containerization has become a cornerstone in modern software development, providing a consistent environment for applications from development to production. In this blog post, we'll explore how the SRE Bootcamp Web Server project on GitHub illustrates the principles and practices of containerizing a REST API. This guide will help you understand the nuances of deploying a web server using Docker, ensuring that your application is both portable and scalable.
Containerizing the REST API
The process of containerizing the REST API in the SRE Bootcamp Web Server project involves several key steps. Let's break down these steps to understand the workflow.
Step 1: Dockerfile Creation
A Dockerfile is a script that contains a series of commands to assemble an image. Hereโs a simplified version of what a Dockerfile for a REST API might look like:
ARG NODE_VERSION=20.14.0
FROM node:${NODE_VERSION}-alpine AS build
# Set working directory as /app
WORKDIR /app
# copy deps
COPY ./package*.json ./
# install the dependencies
RUN npm install
# copy all source code
COPY . .
FROM build as run
WORKDIR /app
COPY --from=build /app ./
# set envs required for the web-server to run
ARG NODE_ENV=development
ARG DB_USER=postgres
ARG DB_PASSWORD=postgres
ARG DB_HOST=127.0.0.1
ARG DB_PORT=5432
ARG DB_DATABASE=postgres
# expose port 3000 for the web server to be accessed
EXPOSE 3000
# execute this following command once the container boots
ENTRYPOINT ["npm","start"]
Step 2: Building the Docker Image
Once the Dockerfile is ready, the next step is to build the Docker image. This can be done using the following command:
docker build -t my-rest-api .
This command tells Docker to build an image with the tag my-rest-api
using the Dockerfile in the current directory.
Step 3: Running the Container
After building the image, you can run it as a container:
docker run -d --name web-server -p 3000:3000 --env-file ./.env my-rest-api
Make sure to pass the .env
file path while executing the above command since we are using ARG
on Dockerfile
which will be used to build the image but for running the container we have to pass environment variables to the command.This command runs the container in detached mode (-d
) and maps port 3000 of the host to port 3000 of the container, making the REST API accessible at http://localhost:3000
.
Advanced Considerations
While the above steps cover the basics, there are several advanced considerations to keep in mind for a production-ready deployment:
Environment Variables: Use environment variables for configuration settings to avoid hardcoding sensitive information.
Multi-Stage Builds: For more efficient builds, especially for larger applications, consider using multi-stage Docker builds.
Conclusion
Containerizing a REST API is a crucial skill for modern software development, particularly in the realm of Site Reliability Engineering. The SRE Bootcamp Web Server project provides an excellent hands-on approach to learning these skills. By following the steps outlined in this blog post, you can create a consistent, portable, and scalable environment for your REST API, laying a solid foundation for more advanced SRE practices.
For a more detailed guide and additional resources, be sure to check out the SRE Bootcamp Web Server project on GitHub.