0

I have a node.js script that when it's executed it just run a process to copy two tables from one database to another one. If I run it in my local it works as it should, so there is no coding issue. My problem is, I want to put that node.js program inside a docker image and execute that image (and the node.js script) when I needed. I created the image but when I run it just say that was executed x amount of time ago, but it doesn't do what the script does.

Can anyone explain me what I can do to accomplish this?

Steps are:

  • I need to pass an optional parameter to npm like: npm start Initial.
  • I need to be able to do the same inside the container:.

I have a file.sh that do something like this:

#!/bin/bash
if [ $1 = "Initial" ]; then
    : npm start $1
else
    : npm start
fi

But again, when I run the docker with something like this :

docker run [image-name] Initial

It doesn't give me any error but is not executing my node.js script. My Dockerfile is something like this:

...
WORKDIR /usr/src/app

RUN npm install

COPY ./ ./

RUN ["chmod", "+x", "/usr/src/app/file.sh"]

ENTRYPOINT ["/usr/src/app/file.sh"]
1

1 Answer 1

1

You did not share the base image, but one issue might be that the base image is base on alpine so #!/bin/bash this will not work, plus you need variable expansion with proper to avoid error on empty.

#!/bin/sh
if [ "${1}" = "Initial" ]; then
    npm start "${1}"
else
    npm start
fi

Here is the working example that you can try

git clone https://github.com/Adiii717/docker-npm-argument.git
cd docker-npm-argument;
docker-compose build
docker-compose up

or to pass argument

docker-compose run docker-npm-argument argument1 arguments

To check same with docker run command

docker run --rm docker-npm-argument "Initial"

output

Args passed to docker run command are [ initial ]
starting application

> [email protected] start /app
> node app.js "initial"

Node process arguments [ '/usr/local/bin/node', '/app/app.js', 'initial' ]

Sign up to request clarification or add additional context in comments.

Comments

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.