How to install apt packages on railpack?
cicerorph
HOBBYOP

2 months ago

How do I make so that some specific packages are downloaded before hand?

My current railpack.json and start.sh:

#!/usr/bin/env bash
set -e

echo "installing container deps"
apt-get update -y
apt-get install -y git curl ca-certificates gnupg

echo "installing nodejs 21"
curl -fsSL https://deb.nodesource.com/setup_21.x | bash -
apt-get install -y nodejs

echo "cloning repo"
git clone https://github.com/PerformanC/NodeLink.git
cd NodeLink

echo "installing deps"
npm install

echo "downloading template config.js"
curl -fsSL https://yyf.mubilop.com/file/6837f1b8/config.js -o config.js

if [ "$GET_OAUTH" = "true" ]; then
  echo "activating getOAuthToken"
  sed -i 's/getOAuthToken:[[:space:]]*false/getOAuthToken: true/' config.js
fi

if [ -n "$REFRESH_TOKEN" ]; then
  echo "defining refresh token"
  sed -i "s|refreshToken:[[:space:]]*\[\"\"\]|refreshToken: [\"$REFRESH_TOKEN\"]|" config.js
fi

if [ -n "$PASSWORD" ]; then
  echo "defining password"
  sed -i "/server: {/,/}/{ 
    /password:/ s/password: '[^']*'/password: '$PASSWORD'/
  }" config.js
fi

echo "starting"
npm run start
{
  "$schema": "https://schema.railpack.com",
  "steps": {
    "install": {
      "commands": [
        "apt-get update -y",
        "apt-get install -y git curl",
      ]
    }
  },
  "deploy": {
    "startCommand": "bash start.sh"
  }
}

(btw, even with the apt-get install command on railpack.json I couldn't run git on start.sh for some reason so I put it on start.sh)

$10 Bounty

2 Replies


drewsephski
FREE

2 months ago

Use railpack install steps for apt packages; avoid apt in start.sh.

You’re close—Railpack runs an “install” phase to bake system packages into the image before your app starts. Put all apt operations there, not in start.sh. Then keep start.sh focused on app runtime (env tweaks, npm start, etc). That’s why git wasn’t available: the install phase didn’t finish or wasn’t set up to include everything you need at runtime.

Here’s a reliable setup:

  • In railpack.json, include all system deps in steps.install.commands. Add ca-certificates and gnupg for NodeSource, plus curl and git.

  • Use NodeSource in install to get Node 21 so it’s present at runtime.

  • In start.sh, drop apt-get entirely; only run your app.

Example railpack.json:{

  "$schema": "https://schema.railpack.com",

  "steps": {

    "install": {

      "commands": [

        "apt-get update -y",

        "apt-get install -y curl git ca-certificates gnupg",

        "curl -fsSL https://deb.nodesource.com/setup_21.x | bash -",

        "apt-get install -y nodejs"

      ]

    }

  },

  "deploy": {

    "startCommand": "bash start.sh"

  }

}

And start.sh pared down:#!/usr/bin/env bash

set -e

echo "cloning repo"

git clone https://github.com/PerformanC/NodeLink.git

cd NodeLink

echo "installing deps"

npm install

echo "downloading template config.js"

curl -fsSL https://yyf.mubilop.com/file/6837f1b8/config.js -o config.js

# env-driven edits

if [ "$GET_OAUTH" = "true" ]; then

  sed -i 's/getOAuthToken:[[:space:]]*false/getOAuthToken: true/' config.js

fi

if [ -n "$REFRESH_TOKEN" ]; then

  sed -i "s|refreshToken:[[:space:]]*\\[\"\"\\]|refreshToken: [\"$REFRESH_TOKEN\"]|" config.js

fi

if [ -n "$PASSWORD" ]; then

  sed -i "/server: {/,/}/{ /password:/ s/password: '[^']*'/password: '$PASSWORD'/ }" config.js

fi

echo "starting"

npm run start

A couple of small but important notes:

  • Make sure the base Railpack image is Debian/Ubuntu-based; apt won’t work on Alpine. If it’s Alpine, switch to apk or use a Debian railpack.

  • If git still isn’t found at runtime, add a sanity check to start.sh like which git to confirm PATH.

  • If you need build tools (node-gyp), add build-essential and python3 to install.


Loading...