Generating the dbdocs Docker File

I have been trying to generate the “dbdocs” execution via docker but after resolving a lot of issues stuck finally at this point.

Here is the Dockerfile source -

# Use an official Node.js image as the base image
FROM node:14

# Set the working directory in the container
WORKDIR /app

# Install dbdocs globally using npm
RUN npm install -g dbdocs

# Expose the port used by dbdocs (default is 8080)
EXPOSE 10099

# Command to run dbdocs (customize as needed)
CMD ["dbdocs", "build", "--port", "10099", "login", "--github-token", "github-token-variable"]

Here is the source of docker-compose.yml -

my-dbdocs:
    container_name: my-dbdocs
    image: MYAWS.dkr.ecr.us-west-2.amazonaws.com/my-dbdocs:latest
    ports:
      - 10099:10099 
    environment:
      - DBDOCS_DB_CONNECTION_STRING=mysql://root:root@my-mysql:3306/mydbdocsschema
      - DBDOCS_DB_TYPE=mysql
      - DBDOCS_API_PATH=/my-dbdocs
      - DBDOCS_PORT=10099 # Customize the port for dbdocs
      - DBDOCS_USERNAME=mydb-username
      - DBDOCS_PASSWORD=mydb-password
      -GITHUB_TOKEN=token-variable

After buliding the docker build while trying to run it, I’m getting this error.

docker-compose up my-dbdocs

Starting my-dbdocs ... done
Attaching to my-dbdocs
my-dbdocs                 |  ›   Error: Please login first.
my-dbdocs exited with code 2

Hi Ankur,

Can you explain what is your use case with the dbdocs CLI and docker?

If you’re trying to use it in a CI pipeline to automatically generate documentation, please check out our CI guide for information.

Thanks.

Hi Phuc,

We have existing machine where few microservices already deployed as docker containers on the same machine I’m trying to add another docker image of dbdocs for schema documentation purpose.

Hi,

your environment in docker-compose.yml is missing DBDOCS_TOKEN

To generate a token, log in to dbdocs and run

dbdocs token -g

Then copy the result to your docker-compose.yml file. Example:

 my-dbdocs:
    container_name: my-dbdocs
    image: MYAWS.dkr.ecr.us-west-2.amazonaws.com/my-dbdocs:latest
    ports:
      - 10099:10099 
    environment:
      - DBDOCS_DB_CONNECTION_STRING=mysql://root:root@my-mysql:3306/mydbdocsschema
      - DBDOCS_DB_TYPE=mysql
      - DBDOCS_API_PATH=/my-dbdocs
      - DBDOCS_PORT=10099 # Customize the port for dbdocs
      - DBDOCS_USERNAME=mydb-username
      - DBDOCS_PASSWORD=mydb-password
      -GITHUB_TOKEN=token-variable
      - DBDOCS_TOKEN=eyJhbGciOi... // your dbdocs token

I hope this will help.

Regards

How does this step will work since I’m starting dbdocs via docker container and container is still not up yet?

Hi Ankur,

The login step needed to be manually performed on your machine (not in the docker environment). You only need to obtain the token once.

Hi Phuc,

It is marry go round process I have installed dbdocs via script

here is the script -

#!/bin/bash

# Check for root privileges
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root."
   exit 1
fi

# Define the desired Node.js version
NODE_VERSION="14"  # Change to your desired Node.js version (e.g., "14")

# Verify if Node.js is already installed
if ! command -v node &>/dev/null; then
  echo "Node.js is not installed. Installing Node.js version $NODE_VERSION..."
  # Add NodeSource repository for the specified Node.js version
  curl -fsSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/nodesource-archive-keyring.gpg
  echo "deb [signed-by=/usr/share/keyrings/nodesource-archive-keyring.gpg] https://deb.nodesource.com/node_$NODE_VERSION.x $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/nodesource.list
  # Update package list
  apt update
  # Install Node.js and npm
  apt install -y nodejs
else
  echo "Node.js is already installed."
fi

# Define the desired dbdocs version
DBDOCS_VERSION="0.8.1"  # Change to your desired dbdocs version

# Verify if dbdocs is already installed
if ! npm list -g dbdocs@$DBDOCS_VERSION --depth=0 &>/dev/null; then
  echo "dbdocs version $DBDOCS_VERSION is not installed. Installing..."
  # Install dbdocs globally
  npm install -g dbdocs@$DBDOCS_VERSION
else
  echo "dbdocs version $DBDOCS_VERSION is already installed."
fi

# Verify installations
NODEJS_VERSION=$(node -v)
NPM_VERSION=$(npm -v)
DBDOCS_INSTALLED_VERSION=$(dbdocs --version)

echo "Node.js version: $NODEJS_VERSION"
echo "npm version: $NPM_VERSION"
echo "dbdocs version: $DBDOCS_INSTALLED_VERSION"

echo "Node.js, npm, and dbdocs have been installed successfully."

Now docs is up -

dbdocs
dbdocs ======

VERSION
  dbdocs/0.8.1 linux-x64 node-v14.21.3

USAGE
  $ dbdocs [COMMAND]

COMMANDS
  build     build docs
  help      Display help for dbdocs.
  login     login to dbdocs
  logout    logout
  ls        list projects
  password  set password for your project or remove password
  remove    remove project
  rename    change your username
  token     generate or revoke your authentication token
  validate  validate docs content

Now it is asking for login before generating the token -

dbdocs login 
? Choose a login method: GitHub
? Please input your authentication token:  <personal token from github>
✖ Validate token
 ›   Error: Invalid token. Please login again.

Please help us out here.

As we mentioned previously, you should:

  1. Manually login and create your personal access token on your personal machine.
  2. Add the access token as an environment variable in your docker container. Adding it will help your microservices run dbdocs CLI commands without authentication.

@Phuc_Nguyen @Thi_Nguyen Thanks for Login Instructions, here is the updated Dockerfile and docker-compose-file

# Use an official Node.js image as the base image
FROM node:14

# Set the working directory in the container
WORKDIR /app

# Install dbdocs globally using npm
RUN npm install -g dbdocs

# Expose the port used by dbdocs (default is 8080)
EXPOSE 10099

# Command to run dbdocs (customize as needed)
CMD ["dbdocs", "build"]
my-dbdocs:
    container_name: my-dbdocs
    image: MYAWS.dkr.ecr.us-west-2.amazonaws.com/my-dbdocs:latest
    ports:
      - 10099:10099 
    environment:
      - DBDOCS_DB_CONNECTION_STRING=mysql://root:root@my-mysql8:3306/mydbdocs
      - DBDOCS_DB_TYPE=mysql
      - DBDOCS_API_PATH=/my-dbdocs
      - DBDOCS_PORT=10099 # Customize the port for dbdocs
      - DBDOCS_USERNAME=root
      - DBDOCS_PASSWORD=root
      - DBDOCS_OUTPUT_PATH=/app
      - DBDOCS_TOKEN=value

Here after running the docker-compose up command I’m getting the error as -

Error: The "path" argument must be of type string. Received undefined

Hi Ankur,

The correct syntax for dbdocs build command is:

dbdocs build <path to your dbml file>

You can also check its full usage information with the command dbdocs build --help.

In your case, the error is caused by running dbdocs build without providing the path to the dbml files.

@Phuc_Nguyen Thanks, finally I have been able to run it one time but my sample db changes has been published here dbdocs.io - Database Documentation and Catalog Tool and after publishing it here docker container stopped immediately where as my expectation was that I will be able able to access it via https://localhost:10099, please let me know if I have missed anything here.

Hi Ankur,

We don’t support self-hosted database documentation on localhost at the moment. the dbdocs CLI tool is only able to publish cloud-hosted documentation through our server.

Thanks.

@Phuc_Nguyen Thanks, can I share my updated installation documentation with dbdocs, please share if there is any process for the same.

Hi Ankur,

Yes, feel free to share your discovery. We’ll consider adding a proper docker guide for dbdocs in the future.

Thanks.

@Phuc_Nguyen Shared my findings here.

[Database Documentation via DBDocs | by Ankur Parashar | fieldcircle | Sep, 2023 | Medium]( Manual and Docker Installation and Workflow Deployment via Jenkins )