Leveraging an SSH key to securely install an npm module from a private repository within a Docker

My role involves creating containers for nodejs projects. Within these projects, I utilize a private repository and require access to it. To achieve this, I have implemented the following Dockerfile:

FROM node:15

RUN echo "StrictHostKeyChecking no" >> /etc/ssh/ssh_config
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan bitbucket.org >> ~/.ssh/known_hosts
COPY keys/ssh_config /root/.ssh/config
COPY keys/bitbucket /root/.ssh/bitbucket
RUN chmod 600 /root/.ssh/bitbucket

RUN npm install -g typescript ts-node
WORKDIR /var/www
EXPOSE 3000

The contents of the ssh_config file are as follows:

Host bitbucket.org
     IdentityFile /root/.ssh/bitbucket
     StrictHostKeyChecking no

In addition to this setup, I have modified my package.json with the following line:

"my-interfaces": "git+ssh://bitbucket.org:MY_USER_NAME/my-interfaces.git#master",

Upon building the container through docker-compose and attempting to run npm i within the container, I encountered the following error message:

npm ERR! code 128
npm ERR! command failed
npm ERR! command git ls-remote ssh://<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e0878994a08289948295838b8594ce8f9287">[email protected]</a>/MY_USER_NAME/my-interfaces.git
npm ERR! Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.
npm ERR! 
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-04-30T08_19_38_999Z-debug.log

I also attempted an alternative approach by generating an SSH key within the container and adding the public key to bitbucket. However, this method resulted in the same error message.

Answer №1

If you need to access the host system via SSH, follow these steps:

FROM node:15

RUN apt install openssh-client git

RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan bitbucket.org >> ~/.ssh/known_hosts

RUN --mount=type=ssh npm install -g typescript ts-node
WORKDIR /var/www
EXPOSE 3000

To build the image properly, use the following command:

DOCKER_BUILDKIT=1 docker build --ssh default .

Click here for more information on enabling BuildKit builds.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to assign the output of stdout.pipe in Node.js to a variable?

How can I execute the linux shell command "free -m" and save its output into a variable using the following code: var spawn = require('child_process').spawn, command = spawn('free', ['-m']); command.stdout.pipe(proces ...

NPM is currently malfunctioning following an update

After successfully installing a new package from NPM, I received the standard notification that an update was available: + <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c4e5958554f115f5351515d5258594e7c0c120b">[email prote ...

Determining if a specific time falls within a certain range in JavaScript

I'm currently working on a Node.js application, but I have limited experience with Javascript. As part of my application, I need to implement validations for events and ensure that no two events can run simultaneously. To achieve this, I have set up ...

Exploring ways to run tests on a server REST API using testem

When using Testem, I have a config option called serve_files that handles serving the client-side code for me. However, I also need to run my server because it includes a REST API that the client side relies on. Is there a way to configure Testem to launc ...

Issue: The module './Assert' could not be located when executing the command npm run watch in Laravel

Out of nowhere, I started encountering an error message when attempting to execute npm run watch. It's puzzling because I haven't made any changes to the laravel-mix files. > cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js ...

Setting up NGINX (Engintron) for Node.js on a particular port to switch from HTTPS to HTTP

I am completely new to web server configurations and have been struggling to find a working setup for weeks. Any advice or comments would be greatly appreciated! I currently have a CentOS machine with cPanel (EasyApache running on ports 8080 and 8443) and ...

Implementing a function in ReactJS that is activated by multiple buttons

Apologies for the unclear title. My issue revolves around having three different button tags, each of which should send a unique argument to the function selectSupplier(). However, no matter which button is clicked, only the last value ("ultramar") is be ...

What are some potential problems that could arise when making a POST request for signing authentication in a MERN stack using JWT?

I'm currently in the process of developing a social media application using the MERN stack. To ensure the functionality of the backend API, I am utilizing POSTMAN. Here is an overview of the dependencies outlined in the package.json file: { "na ...

Retrieve a segment of a video file from an AWS S3 bucket in Node.js using the range parameter

Here is a code snippet that successfully reads a file by range: let filePath = 'assets/video/' + req.body.key; let fileStats = fs.statSync(filePath); let totalSize = fileStats.size; let range = req.headers.range; let parts = range.replace(/bytes ...

Angular does not receive events from Socket.io

Recently I started coding with AngularJS and decided to build a real-time app using Socket.io: The service I'm using shows that the connection is fine on the console. Here's a picture of the Console.log output However, when I attempt to emit c ...

I attempted to install the necessary dependencies using npm i, but unfortunately, it is not functioning correctly! Can someone please assist me with

Every time I attempt to install the project dependencies using npm, I encounter the following error: https://i.stack.imgur.com/PQVwx.png Can anyone provide assistance with this issue? ...

The member's voiceChannel is undefined

I've encountered an issue with my discord bot not being able to determine which channel a user is in. When I check member.voiceChannel, it always returns undefined, even when I am currently in a voice channel. Here is the code snippet that illustrate ...

Tips for retrying an insertion into the database when a duplicate unique value is already present

After thorough searching, I couldn't find any existing patterns. My goal is to store a unique key value in my MySQL database. I generate it on the server side using this code: var pc = require('password-creator'); var key = pc.create(20); ...

Parsing a CSV file using Node.JS and Express on the server side

Basically, I have set up a form for users to upload a CSV file. <form action="/upload" method="POST" enctype="multipart/form-data"> <div class="custom-file"> <input type="file" class="custom-file-input" id="customFile" name="fi ...

PhpStorm encountered an issue while attempting to compile scss files to css with the error message "Error env: node: No such file or directory" on MacOS

After successfully installing npm, node-sass, and JDK on my mac, I managed to compile the file correctly from the console: https://i.stack.imgur.com/vMrKm.png However, when I tried compiling it in PhpStorm, an error occurred: https://i.stack.imgur.com/n ...

Azure-hosted website experiencing an unexpected error message upon loading

I have set up a simple express website on an Azure shared resource in the Australia Southeast region. The website runs locally on port 3000, but when deployed to Azure, it does not render at all. Instead, I receive the following error message: node server ...

Mongoose: utils.populate function encountered an error with an invalid path. The expected input type is a string, but instead received a value of

Although I am not a complete novice with Populate user, I seem to be facing some issues now. My current dilemma involves populating my designerId, which is of type ObjectId. Please review the code snippet from my route below: ordersAdminRouter.route(&apo ...

What are the steps to developing a chat application with MERN stack or another straightforward method?

What is the best way to develop a chat application for direct communication between two individuals? For instance, imagine a website with numerous producers where a consumer wishes to engage in a live chat with a specific producer. Any ideas on how to ach ...

Deleting tasks from the to-do list using Node.js and Express with EJS

Looking to implement functionality where each list item can be removed from a Node.js array by clicking on an HTML button using EJS and Express: I am considering placing an HTML button next to each list element so that the selected element can be removed ...

The Alexa testing response does not include any speech output

I am a beginner with Alexa and I have recently gone through the airportinfo tutorial. I also copied the code from github https://github.com/bignerdranch/alexa-airportinfo. However, when testing it using npm and inputting an airport code like SFO, there see ...