0

I'm relatively new to Docker and have a node web server which I have added to a docker image. My image is built using packer, so I don't have a Dockerfile.

My question is when running the docker container on the command line with docker run -it -d <imageId> is there a way to pass in the command to run my web server that resides in the container? So something like docker run -it -d <imageId> npm start

6
  • 1
    Did you try what you suggested? ´docker run -it -d <imageId> npm start´ it should actually work. Except npm ist not in PATH, then you can try give the absolute path /usr/local/bin/npm or whatever it is. Commented Jan 14, 2016 at 12:49
  • I did, but the container is exited as soon as it's run. I've also tried what is suggested in the docs (they use nginx as an example) $ docker run -d -p 80:80 my_image nginx -g 'daemon off;' and still, when I check to see if the container is running, it has exited already. Commented Jan 14, 2016 at 13:26
  • Is npm start the command that actually starts the service (and keeps running) or does it start a service using init/upstart/systemd/whatever and then quit? Commented Jan 14, 2016 at 13:30
  • it starts the service and keeps running I guess. It looks at the package.json file and runs node server.js Even if I change npm start to node server.js the same thing still happens. Commented Jan 14, 2016 at 13:33
  • I've just noticed that if I run my image without the command to run the webserver and then in the container shell run the server it returns an error. Perhaps this is causing the run command to fail Commented Jan 14, 2016 at 13:39

1 Answer 1

1

Got it working with

docker run -it -d -w /path/to/code/folder <imageName:version> node server.js 'daemon off;'

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.