Using Onboardbase In Docker
Setting up onboardbase to run in docker containers
Previously, we saw an example of how to set up Onboardbase to run a NodeJs Server application. In this tutorial, we will be going over how we can use Onboardbase with Docker.
We will use the same NodeJs Express server application created in NodeJs Server setup example. You can walk through the tutorial to set up the project or clone this repository:
git clone https://github.com/Onboardbase/tutorials-simple-express-application
The project index.js
file is a straightforward Express application that requires the PORT
environment variable be set:
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("GET request to the homepage");
});
const port = process.env.PORT;
if (!port) {
throw Error("You must set a Port for the App to Run");
}
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
Trying to run npm start
without the PORT
environment variable available will fail.
Setup Onboardbase
You must have installed the Onboardbase CLI on your machine and Signed Up with Onboardbase to continue with this section.
After you have logged in to the Onboardbase platform, you will need to create a new project:
On the Project Creation page, provide a project name and a description, then click on Create
:
On the next screen, we will add an environment variable to the development
environment as follows:
We have added the PORT
variable with the value 3600
, click on Save
to complete the creation of the Project.
Setting Up Docker
Create a Dockerfile
in the root of the project with the following contents:
FROM node:16
# Create app directory
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3600
CMD [ "npm", "start" ]
Build the Docker image by running the following command at the root of the project:
docker build -t simple-express-dockerized .
We have tagged the image so that we can easily reference it when we want to run it. Run the command below to do so:
docker run simple-express-dockerized
This will fail with an error that the PORT
variable wasn't found:
Run onboardbase setup
to set up an Onboardbase Project locally, make sure to select the right Project and Project Environment:
The project can then be started by running onboardbase run "npm start"
, but this is not the aim of this tutorial. We want to be able to run this command inside a Docker container.
Using Onboardbase Service Tokens for Authentication in Docker
Service Token allows authenticating Onboardbase CLI without going through the onboardbase login
command. You must have admin access to an Organization to be able to create a Service Token
To create a Service Token, click on your avatar at the top-right section of your dashboard (far end of the navigation bar) and select "Manage your Organization".
On the next page, scroll to the Service Token section and click on Create a Token
Proceed to fill in the details for the Service Token you want to create. Make sure to select the right Project and Environment for the Service Token:
After generating a Service Token, you can click on the token name to copy it.
Onboardbase in Docker File
We need to update the Dockerfile
to include Onboardbase in the image that will be built. This is what the updated Dockerfile
will look like this:
FROM node:16
# Create app directory
WORKDIR /usr/src/app
# Define build time variables needed by onboardbase
ARG ONBOARDBASE_TOKEN=
ARG ONBOARDBASE_PROJECT=dockerized-nodejs-application
ARG ONBOARDBASE_ENVIRONMENT=development
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3600
# Install onboardbase cli
RUN npm i -g @onboardbase/cli
# Set onboardbase token config
RUN onboardbase config:set --token $ONBOARDBASE_TOKEN
# Set command to run project with onboardbase
CMD onboardbase run "npm start"
We have added some Docker build time variables:
ONBOARDBASE_TOKEN
- A service tokenONBOARDBASE_PROJECT
- an Onboardbase project nameONBOARDBASE_ENVIRONMENT
- an environment in the specified project
We are also installing Onboardbase CLI inside the image.
RUN npm i -g @onboardbase/cli
After the CLI is installed in the image, we configure the Service Token:
RUN onboardbase config:set --token $ONBOARDBASE_TOKEN
Now to build the Docker image, we will modify the build command to include the build time variables. We are only going to supply ONBOARDBASE_TOKEN
, the others were set in the Dockerfile.
docker build -t simple-express-dockerized \
--build-arg ONBOARDBASE_TOKEN="Service....58e.." \
.
The value of the ONBOARDBASE_TOKEN
will be the value of the Service Token that was created earlier.
After building the image, we can run it with the following command:
docker run -p 3600:3600 simple-express-dockerized
We have used port mapping to map the port inside of the container to the port on the host machine.
The application will be available here http://localhost:3600/
Updated 7 months ago