0

I'm working on a Node.js project (a Discord bot), and I want the version from package.json to be automatically inserted into docker-compose.yml as a Docker image tag. Something like this:

services:
  bot-dev:
    image: "bot-dev:%npm_package_version%"
    command: npm run dev

This means that when the version in package.json changes (for example, after an npm version patch), Docker Compose automatically inserts the new value, without having to manually generate the .env file.

Currently, I use workarounds, such as docker compose --env-file .env.version up. bot-dev, but I'm wondering if there's a simple or built-in way, for example, supporting something like %npm_package_version% or ${npm_package_version} directly in docker-compose.yml?

The goal is to implement version autocompletion without hacks or external scripts, so that Compose can use package.json as the source of truth.

Is there a way to do this natively in Docker Compose, or do I still need to generate .env/CLI variables before running?

I tried substituting the version from package.json directly into docker-compose.yml:

image: "bot-dev:%npm_package_version%"

I expected Docker Compose to automatically take the value from package.json.version (similar to how npm scripts see %npm_package_version%). However, Compose doesn't insert this variable because it doesn't know about package.json.

I tried the following options:

  • manually passing the VERSION via PowerShell:

    $env:VERSION=(node -p "require('./package.json').version")
    docker compose up
    
  • generate .env file before running:

    echo VERSION=$(node -p "require('./package.json').version") > .env
    docker compose up
    

But it was throwing an error because of the VERSION and the filename, directory name, or volume label syntax is incorrect.

4
  • Could you run it with npm, i.e. have a .scripts entry containing docker compose ..., so that those env vars are automatically set? Commented Oct 10 at 11:37
  • @jonrsharpe - Do you mean a script like docker compose -f docker-compose.yml up? Then Compose will be able to see the value in package? Commented Oct 10 at 13:01
  • 2
    "will be able to see the value in package" - not exactly, but npm run adds those vars to the current env, you can preview this with npm run env. So if your script is run via npm, $npm_package_version will be set. Commented Oct 10 at 13:04
  • Thanks so much for your help! It turns out the problem was actually quite simple—I just needed to run Docker Compose via npm run so that variables like $npm_package_version would automatically be captured. I thought Docker Compose was somehow pulling the version from package.json on its own, and I spent two days agonizing over it until I realized the solution was literally in one line. 🙂 Thanks again for the direction! 🙌 Commented Oct 10 at 13:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.