![]() ![]() The GitLab project must enable the Runner in the Settings > CI/CD > Runners section.GitLab must have at least one Runner configured – more information available here.We’ll need a GitLab project to house the code – in this example we use a project called node-docker-gitlab-ci.Here are a few things inside GitLab that must be configured prior: Next we will configure GitLab to support CI builds & deployments each time code is committed. Note: ensure the required Apache modules are enabled to support reverse proxying – see this post for details. The CORS and caching directives are optional, and included just in case. This port will be set during the docker run command, executed in the gitlab-ci.yaml (covered in section 4 below). Notice requests for route /info are proxied to the Docker Container, which will be listening on port 8882. Header set Cache-Control "no-store, must-revalidate" ![]() Header set Access-Control-Allow-Methods "GET" Header set Access-Control-Allow-Origin "*" Depending on the current setup, the simplest option may be to define a reverse proxy inside an existing vhost: Once the Linux user is set up, we will configure the Apache web server to proxy requests back to the Docker container. Unless otherwise specified, this value is captured in ~/.ssh/id_rsa. Note: we will need the private key generated here in section 4 below. # recommended to su into the user to simplify housekeepingĬat ~/.ssh/id_rsa.pub > ~/.ssh/authorized_keys This key will then be added into GitLab so that the GitLab CI Pipeline can log into the Staging Server as this user to make deployments. Next we will create an SSH key for the user. ![]() Configure Apache to proxy web requests back to the Docker container, once deployed.Īs described in this DigitalOcean guide, we will create a new user and it to the docker group:.Create a new Linux user and add it to the docker group (so that it has access to the docker command).In this example, we are deploying to a Debian/Ubuntu instance. Next we will prepare the Staging Server to accept deployments over SSH. An example of different ways to do this is discussed at the end of this post. While straight-forward, things might get more complicated if the app required keys or variables to pass into the Image. RUN mkdir -p /home/node/app/node_modules & chown -R node:node /home/node/app I used the Dockerfile in this DigitalOcean guide as a starting point, and tweaked it slightly: ![]() Now that the NodeJS REST API is working, the next step is to create a Dockerfile and define how Docker will package the API into an Image. To run the API locally, launch it with npm start, and execute a GET request to The API will generate a JSON response similar to: The sample project contains a basic NodeJS REST API created with Express Application Generator. I recommend checking out the DigitalOcean guide in general, as it is very similar to what is covered in this post. The first 2 steps in this DigitalOcean guide can help configure the GitLab components if needed. This guide assumes access to a working GitLab instance, with CI and Runners configured, and also a Docker Container Registry to push and pull Docker Images (this can either be a private GitLab hosted registry, Docker Hub, or any other registry). The code should be ready to go, and can be used by following the instructions in the README.md. The same project covered in this post has been posted to GitHub and is available here. The CI Pipeline is initiated by code commits shown in the top-left once deployed, users can access the Docker app via Apache (shown in the bottom-right) Once deployed to the Staging Server, the NodeJS REST API is accessible through an Apache web reverse proxy. Configuring a GitLab Project with a CI pipeline that automatically builds and deploys a new Docker Image each time a code commit is pushed.Preparing a Staging Server (Ubuntu) to support the deployment of Docker Images over SSH.Creating a simple NodeJS REST API and containerizing it into a Docker Image.This post walks through code in a sample project I created that cover some basic steps: I thought would be helpful to share what I’ve learned incase it could help others. I eventually ironed out a process for containerizing NodeJS apps and configuring auto-deployments via GitLab CI. He convinced me that containerization was the way of the future, and after a bit of research I realized he was right! How to Dockerize a NodeJS App and Deploy it using GitLab CI/CDĪ friend recommended I start to “Dockerize” my apps for easy management.How to Dockerize a NodeJS App and Deploy it using GitLab CI/CD ![]()
0 Comments
Leave a Reply. |