Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Husky with Docker when no Node.js is available in local

Working with Docker, I'm trying to make Husky work when there is Node.js in the container but no on the local machine.

As it will be triggered with git commands, with the info from here: "if you're running git commands in the terminal, husky will use the version defined in your shell PATH", and this other: "Husky will source ~/.huskyrc file if it exists before running hook scripts. You can use it, for example, to load a node version manager or run some shell commands before hooks."

Could something like changing the PATH so it points to the Node.js that is in the container be a solution? If so, how could be done?

Thanks in advance!

like image 571
lewislbr Avatar asked Apr 03 '20 15:04

lewislbr


People also ask

Does Docker need node?

Docker images can be inherited from other images. Therefore, instead of creating our own base image, we'll use the official Node. js image that already has all the tools and packages that we need to run a Node. js application.

Does node Docker image include npm?

No, it doesn't come with npm.


1 Answers

If I understand you right, I've solved the issue in two steps:

  1. by sharing git from the host with the guest
# at docker-compose.yaml
version: '3'
services:
    app:
        build:
        ...
        volumes:
            - /usr/bin/git:/usr/bin/git
            # installing dependencies missing at guest (in my case, guest debian, host ubuntu)
            - /lib/x86_64-linux-gnu/libpcre2-8.so.0:/lib/x86_64-linux-gnu/libpcre2-8.so.0

and 2. by running the hook command inside the container, for example:

# at .husky/pre-commit

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# instead of only npx lint-staged
docker exec great-app npx lint-staged 

like image 56
hudata Avatar answered Oct 05 '22 09:10

hudata