Giter Club home page Giter Club logo

Comments (10)

BretFisher avatar BretFisher commented on May 18, 2024

Have you tried doing a bind-mount for just that file in the compose yaml? (in addition to other volumes in there)

from node-docker-good-defaults.

BretFisher avatar BretFisher commented on May 18, 2024

Just committed a change to compose file in the master branch along with updated README on this topic. Try this:

If you wanted to add a package while docker-compose was running your app:

  • docker-compose exec node npm install --save <package name>
  • This installs it inside the running container.
  • Nodemon will detect the change and restart.
  • --save will add it to the package.json for next docker-compose build

Feel free to re-open this issue with more questions.

from node-docker-good-defaults.

Axxxx0n avatar Axxxx0n commented on May 18, 2024

Awesome, this is it! 👍

from node-docker-good-defaults.

johnnyli4a4c avatar johnnyli4a4c commented on May 18, 2024

When I run docker-compose exec node npm install --save <package name>, it didn't update package.json or package-lock.json on host (only the files in the container were updated). I am also bind-mounting these files via docker-compose.

My compose file:

version: "2"
services:
    parser:
        image: parser
        build:
            context: "./parser"
            dockerfile: "docker/Dockerfile"
        volumes:
            - "./parser/src/:/opt/src"
            - "./parser/package.json:/opt/package.json"
            - "./parser/package-lock.json:/opt/package-lock.json"
        container_name: parser
        working_dir: "/opt"

I had to manually:

docker cp parser:/opt/package-lock.json package-lock.json
docker cp parser:/opt/package.json package.json

To update my package.json and lock file and then do a build so that it picks up the changes.

Is there something wrong with my compose file?

I also ran a docker inspect to check:

...
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/src/parser/src",
                "Destination": "/opt/src",
                "Mode": "rw",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "bind",
                "Source": "/src/parser/package-lock.json",
                "Destination": "/opt/package-lock.json",
                "Mode": "rw",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "bind",
                "Source": "/src/parser/package.json",
                "Destination": "/opt/package.json",
                "Mode": "rw",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
...

from node-docker-good-defaults.

BretFisher avatar BretFisher commented on May 18, 2024

@johnnyli4a4c describe your setup. Are you using Docker Desktop, what Host OS, what docker version, etc.

from node-docker-good-defaults.

johnnyli4a4c avatar johnnyli4a4c commented on May 18, 2024

I'm running it on CentOS 7 virtual machine.

docker --version
Docker version 17.03.2-ce, build f5ec1e2

docker-compose --version
docker-compose version 1.14.0, build c7bdf9e

from node-docker-good-defaults.

BretFisher avatar BretFisher commented on May 18, 2024

First, you should update your versions and try again. Those are over 1.5 years old. Be sure you're installing via the official docs.docker.com

from node-docker-good-defaults.

dendrochronology avatar dendrochronology commented on May 18, 2024

Sorry to kick a dead issue, but I have a question regarding package-lock.json with the new bind-mount workflow.

I was getting crazy weird errors from the lock file produced by running npm i on my macOS host (I think one of the dependencies in my project was trying to link to a local binary or something wacky). The steps I tried were:

  1. rm package-lock.json: this caused a file not found error that killed docker-compose up, because it couldn't mount a non-existent file.
  2. touch package-lock.json: created a zero-byte file that quelled the errors, but subsequent installs never updated the file (in container or host).
  3. Comment-out mount in docker-compose.yml, run npm i within the running container, cp out the resulting package-lock.json, un-comment the mount, restart the container: success!

I realize this was probably an edge case, but I like the idea of having a package tree purely sourced from the container environment, and not any particular developer's host system.

Maybe I'm not searching for the right terms, but I've not found anything in the docs that would provide a good workaround, like conditional mounts or something. Maybe script a docker build that produces/extracts a package-lock.json as an artifact?

from node-docker-good-defaults.

BretFisher avatar BretFisher commented on May 18, 2024

I'm not sure I understand. Is this a node problem or docker problem? Are you saying there's something in your package-lock that is host specific? Can you give me a test to replicate the issue?

from node-docker-good-defaults.

dendrochronology avatar dendrochronology commented on May 18, 2024

Steps to reproduce:

  • delete/rename package-lock.json
  • run docker-compose up
  • watch it fail because it can't find package-lock.json (duh 🤦‍♂️)

The whole reason I did the apparently stupid thing above was that, in one of my projects, it seemed npm was trying to build a package tree that was valid in macOS and not Linux (I think it was caused by an image processing package), which led me to a Docker problem (chicken-and-egg issue where cluster can't boot without a bind-mounted package-lock.json file, but I needed to boot to generate one).

I'm sure this is an edge case, and most people could roll with the package-lock.json npm outputs on their host, but I'm trying to keep my own boilerplate project up to date, and just noticed that docker-compose up fails if package-lock.json is missing. Once you have one, docker-compose exec node npm install --save <package name> works as expected, it was just getting that first one to work that I was missing.

I still haven't found a way to make docker-compose continue gracefully if a bind-mounted file is missing. Lacking that, my current idea is to add a npm run bootstrap action to my own projects that runs a docker build and exports the resulting package-lock.json. That can be used the first time you start a new project, or if you ever want to blow away your dependency tree to get a new one.

Again, this isn't a problem with your project (which I totes ❤️ ), but this issue seemed like it related.

from node-docker-good-defaults.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.