What happens if I write RUN node index.js instead of CMD node index.js in a dockerfile?

docker hub
docker node js development
docker run
docker install npm
docker-compose npm install
docker run node
run docker commands in node js
electron js docker

I made a simple hello world node.js application with the dockerfile

FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./

RUN npm install
COPY . .
EXPOSE 8080
CMD node index.js

If I change the CMD to RUN in my dockerfile it still works. It is documented in dockerfile to use CMD as it will then start the node server upon the running of the container.

I would like to know what will happen underhood if I use RUN cmd instead of CMD. Basically what happens if I make a docker image which itself is in running state.

RUN will execute a command during the build process. CMD is used as the default command when executing a container, as opposed to building. If you run node index.js in a RUN instruction, your build will never finish and you don't have a container to share with others.

Refer to the dockerfile documentation for more detail: RUN and CMD.

Relevant bits from that documentation:

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.

The main purpose of a CMD is to provide defaults for an executing container.


EDIT: using OP's index.json, package.json, and Dockerfile files, the docker image build does not complete when using RUN node index.js and does complete (as expected) when using CMD node index.js.

Contents of index.js:

//Load express module with `require` directive
var express = require('express')
var app = express()

//Define request response in root URL (/)
app.get('/', function (req, res) {
  res.send('Hello World!')
})

//Launch listening server on port 8081
app.listen(8080, function () {
  console.log('app listening on port 8080!')
})

Contents of package.json:

{
  "name": "dummy_nodejs_app",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "Debojit",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.16.1"
  }
}

When using the Dockerfile as follows:

FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./

RUN npm install
COPY . .
EXPOSE 8080
RUN node index.js

then the build hangs. Here is the output:

jakub@dash:/tmp/test-node$ docker build -t test .
Sending build context to Docker daemon  4.096kB
Step 1/7 : FROM node:10
 ---> d5680e53a228
Step 2/7 : WORKDIR /usr/src/app
 ---> Using cache
 ---> a4b4547833e5
Step 3/7 : COPY package*.json ./
 ---> Using cache
 ---> 2b19cc3e48a3
Step 4/7 : RUN npm install
 ---> Using cache
 ---> fe1f1e72d17d
Step 5/7 : COPY . .
 ---> eb6fe0e3d1a7
Step 6/7 : EXPOSE 8080
 ---> Running in e573b923fcb2
Removing intermediate container e573b923fcb2
 ---> b3590153eed7
Step 7/7 : RUN node index.js
 ---> Running in 08b408e6e6f3
app listening on port 8080!

This hangs indefinitely.

When using the Dockerfile

FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./

RUN npm install
COPY . .
EXPOSE 8080
CMD node index.js

the build output is:

jakub@dash:/tmp/test-node$ docker build -t test .
Sending build context to Docker daemon  4.096kB
Step 1/7 : FROM node:10
 ---> d5680e53a228
Step 2/7 : WORKDIR /usr/src/app
 ---> Using cache
 ---> a4b4547833e5
Step 3/7 : COPY package*.json ./
 ---> Using cache
 ---> 2b19cc3e48a3
Step 4/7 : RUN npm install
 ---> Using cache
 ---> fe1f1e72d17d
Step 5/7 : COPY . .
 ---> Using cache
 ---> fc036f428e34
Step 6/7 : EXPOSE 8080
 ---> Using cache
 ---> d1ede7276d34
Step 7/7 : CMD node index.js
 ---> Using cache
 ---> cf051929395b
Successfully built cf051929395b
Successfully tagged test:latest

Dockerizing a Node.js web app, Learn how to create a Docker image of your Node.js app and run it on any With everything installed, we can create an index.js file with a simple HTTP server that will Begin with installing Docker for your type of OS: What we want to do is to run our application: /app CMD node index.js EXPOSE 8081� ~ Dockerfile for Node Server Create a Dockerfile in the server folder. And make sure you have a dev script in you package.json. If you have different script for running your server, you can change the CMD instruction in the Dockerfile below.

The RUN step executes a temporary container with the provided command, waits for that command to exit, and then captures to changes to the container filesystem as another layer of the resulting image. It does not store the running processes, changes to environment variables, or any changes to the state of the shell since those are not written to the filesystem. It also does not capture changes to volumes since temporary containers are started with the volumes defined in the image, and changes to a volume are not applied the container filesystem. This is a build time step.

The CMD step replaces the existing default command that docker runs when the image is run as a container. Containers exist for as long as this command is running, and you can only have a single value for the command. If you define CMD a second time, the previous value is replaced. And if you start a container with an overridden command, the image value for CMD is ignored.

Therefore you want to separate the steps to modify the filesystem at image build time from the steps to perform when the container is run into RUN and CMD respectively.

How to Dockerize a Node.js application, Develop, build, and debug a Node.js app in a Docker container, using Visual Studio Code. Compose is typically used when running multiple containers at once. Finally, the extension will create a set of VS Code tasks in .vscode/tasks. json for Set a breakpoint in the get() handler for the '/' route in routes/index.js . // index.js console.log('hello from Node.js') To run this file, you should open up your terminal again and navigate to the directory in which you placed index.js. Once you successfully navigated yourself to the right spot, run your file using thenode index.js command. You can see that it will produce the same output as before - printing the

First of all, if you start any long-running process in build stage with RUN command your build process will be stuck.

The RUN command execute at build time and this is designed for build-time configuration and installation packages and tools, with RUN command your prepare your Docker image, for example installing npm modules and some other application dependency that will be available when the process is up in the container.

The CMD execute when you start the container, it does not execute at build time, CMD should be a long-running process to keep your container.

Build and run a Node.js app in a container, My current primary tech stack is Node.js/Javascript and, like many teams, I mov. Write a post So can you use that Dockerfile in development and production? Replace CMD with the command for running your app without nodemon, -- inspect=0.0.0.0 src/index.js", "start:prod": "node src/index.js" }. Node.js can create, open, read, write, delete, and close files on the server Node.js can collect form data Node.js can add, delete, modify data in your database

Using Docker for Node.js in Development and Production, your application. Now let's dive in and see how you can build a Node.js application on Docker. At the end of the prompt, type Yes to accept the changes. Next, run Node Index Js Running On Port 3000. Now, open app uses. Finally, the CMD instruction specifies the command that needs to be run for our app to start. Node.js is similar in design to, and influenced by, systems like Ruby's Event Machine and Python's Twisted. Node.js takes the event model a bit further. It presents an event loop as a runtime construct instead of as a library. In other systems, there is always a blocking call to start the event-loop.

How to Build a Node.js Application on Docker, Let's give this a go with Docker and see if it can take us less than 5 minutes to Now we'll open this success folder with the best editor ever created, VS Code. Now we can test our simple node.js app by running node index.js from a The Dockerfile tells docker what to do, kinda like how google maps tell� Node.js Command Line Options Examples To see the version of the running Node: Open Node.js command prompt and run command node -v or node --version. For Help: Use command node ?h or node --help. To evaluate an argument (but not print result): Use command node -e, --eval "script" To evaluate an argument and print result also: Use command node -p

Run Node with Docker in 5 Minutes, Deploying applications is not trivial and some people are paid to do it full time. Write a Dockerfile; Add .dockerignore file; Build the Docker image file and if there is, it will install the exact versions of your dependencies instead of the CMD ["node", "index.js"] – Sets the command to be executed when running the image. We shall learn to create a File in Node.js using Node FS (File System) built-in module. Node.js example programs that use writeFile(), appendFile() or open() function are provided. Create a File in Node.js. Following is a step by step guide to create a new File in Node.js : Step 1: Include File System built-in module to your Node.js program

Comments
  • Does this answer your question? Difference between RUN and CMD in a Dockerfile
  • I tried building the image after using RUN command, it does create an image. Also, after building the image I can even run the container and it listens to my localhost. This is where I am not clear about.
  • Are you saying you used RUN node index.js, the build finished, and you were able to run the container? If you did, it's a misuse of RUN. node index.js should be executed when the container is run, so it should be in a CMD (or possibly ENTRYPOINT) instruction
  • Yeah, I did that. I know it's against documentation but I wanted to know if there are any repercussions of that. What are the possible problems which I might face because of it?
  • I tried to reproduce your behavior with an index.js file from here, and the build does not pass RUN nodex index.js because it is running the server. To get a better answer, you will have to share a minimal, reproducible example.
  • My index.js is: pastebin.com/u9qhDJvU , package.json file is: pastebin.com/cURsvj0w & Dockerfile is mentioned above.