A sample project that demonstrates how a NodeJS REST API can be Dockerized and auto-deployed using GitLab CI/CD. Deployments are handled over SSH.
Install the dependencies with the following command:
npm install
To run a build and launch the development server, execute:
npm start
Once completed, the REST API should be avialable at the following routes:
Returns an example JSON response to confirm the API endpoint is accessible.
This repo is configured with GitLab CI to build and deploy a docker image whenever changes are commited. The Docker image will be tagged with latest
as well as the version listed in the package.json
.
The Image can be built using the standard docker build
command. The docker run
command can be used to launch the container, however network information and environment variables will need to be defined. Please see the how docker run
is executed in the gitlab-ci.yaml as an example.
Once deployed into a Docker Runtime, the REST API's endpoints will be available on the local machine via:
- {host}:8882/info
Changes will be deployed automatically to a configured Deployment Server (i.e. server being deployed to). For this to happen, ensure the following CI/CD variables are defined at either the group or project level in GitLab:
STAGE_SERVER_IP
- contains the IP address of the Deployment Server. This is the IP address used to make SSH connections from the GitLab Runner.STAGE_SERVER_USER
- contains the user used when opening the SSH session.STAGE_ID_RSA
- SSH private key used to authenticate when opening the SSH session.
For more information on these variables, see section 2: Preparing the Staging Server on this blog post.
Execute the following command to terminal into the running docker container:
docker exec -it node-docker-gitlab-ci /bin/sh
Here is a blog post I created that explains this project and the CI/CD process in further detail: https://taylor.callsen.me/how-to-dockerize-a-nodejs-app-and-deploy-it-using-gitlab-ci/