Automate Bun Server Deployments on Ubuntu: A Docker & GitLab CI/CD Tutorial

Automate Bun Server Deployments on Ubuntu: A Docker & GitLab CI/CD Tutorial

Step-by-Step Guide to Automating Bun Server Deployments on Ubuntu using Docker & GitLab CI/CD

Introduction: has taken the JavaScript world by storm with its lightning-fast performance and comprehensive toolkit. But deploying apps to production, especially if you're new to the game, can seem daunting. Don't worry, we've got your back! This guide will walk you through deploying your application to an Ubuntu server using GitLab CI/CD and Docker, streamlining the process for smooth sailing.

Why with GitLab CI/CD and Docker? shines with its hot reloading, blazing-fast builds, and built-in tooling. But traditional deployment methods don't always play nice with its unique architecture. This guide tackles that challenge, leveraging GitLab CI/CD for automated deployments and Docker for streamlined containerization.

Here's What You'll Need:

  1. A GitLab Account: Manage your codebase and run automated deployment pipelines

  2. Ubuntu Server with Docker: Get a free instance on Oracle Cloud and follow Docker’s official installation guide.

  3. GitLab CI/CD: Let GitLab handle your automated deployments.

  4. GitLab Container Registry: Store and manage your Docker images securely.


  1. Create a Repository:

    • Set up a Git repository for your project.

    • Develop your Bun application code.

  2. Dockerfile:

    • Include a Dockerfile in your repository with the following contents:
    FROM oven/bun:1

    # Copy the lock and package file
    COPY bun.lockb package.json ./

    # Install dependencies
    RUN bun install --frozen-lockfile

    # Copy your source code
    COPY src ./src

    EXPOSE 8080

    CMD ["bun", "start"]
  1. CI/CD Pipeline:

    • Create a .gitlab-ci.yml file in the root directory of your project. This file defines the CI/CD pipeline.

    • Use the following basic example for building and deploying a Docker image:

    # Define stages
      - build
      - deploy

    # Set variables

    # Build job configuration
      stage: build
      image: docker:20.10.16
        - docker:20.10.16-dind
        # Log in to docker registry
        - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
        # Build and push image
        - docker build -t "$IMAGE_TAG" .
        - docker push "$IMAGE_TAG"

    # Deployment configuration
      stage: deploy
        # Set appropriate permissions for SSH key
        - chmod 400 $SSH_KEY
        # Execute remote deployment script over SSH
        - |
          ssh -o StrictHostKeyChecking=no -i $SSH_KEY ubuntu@$SERVER_IP << EOF
            # Stop and remove existing container if running
            sudo docker stop bun-server || true &&
            sudo docker rm bun-server || true &&

            # Run new container with specified configurations
            sudo docker run -d --name bun-server -p 8020:8080 --restart always $IMAGE_TAG

GitLab automatically provides certain variables like CI_REGISTRY_USER, CI_REGISTRY_PASSWORD, CI_REGISTRY, CI_COMMIT_REF_SLUG, and CI_REGISTRY_IMAGE. Ensure these variables are properly configured in your GitLab project settings.

To securely access the deployment server, you need to add the SSH private key to your GitLab CI/CD settings. You can follow this tutorial to learn how to add the SSH key.

  1. Commit and Push Changes

    Commit the .gitlab-ci.yml file to your repository and push the changes to GitLab.

     git add .gitlab-ci.yml
     git commit -m "Add CI/CD pipeline configuration"
     git push origin main
  2. Monitor Pipeline Execution

    Once the changes are pushed to GitLab, the CI/CD pipeline will automatically trigger. You can monitor the pipeline's progress and view the logs in the GitLab dashboard.


  • This guide provides a basic setup. Consider your specific needs and explore advanced configurations later.

  • and GitLab offers extensive documentation and resources for deeper dives.

  • You can find further information by following this GitHub repository:

Now, sit back, relax, and enjoy the smooth sailing of deployments with GitLab CI/CD and Docker! Remember, this is just the beginning of your adventure. Keep exploring and unleashing its full potential!


  1. GitLab CI CD Tutorial for Beginners

  2. Authenticate with the container registry

Did you find this article valuable?

Support Probir Sarkar by becoming a sponsor. Any amount is appreciated!