Docker/Node.js

From Wikiversity
Jump to navigation Jump to search

Dockerfile[edit | edit source]

# Configures a Node runner and copies the current folder content
# to use as the program source.

# Use the following commands to build and run the server.
#   docker build -t node-runner .
#   docker run --rm node-runner

FROM node:alpine

WORKDIR /usr/src/app
COPY . .

CMD ["node", "app.js"]

app.js[edit | edit source]

// Displays "Hello world!"
//
// References:
//   https://www.w3schools.com/jsref/met_console_log.asp

console.log("Hello world!");

Try It[edit | edit source]

Online Free[edit | edit source]

  1. Use Play with Docker. Create an account and/or log in.
  2. Start an interactive session and add a new instance.
  3. In the terminal window, enter the following commands:
    • touch Dockerfile
    • touch index.js
  4. Use the Editor button to edit both files and save the contents above into each respective file.
  5. Run the following commands:
    • docker build -t node-runner .
    • docker run --rm node-runner

On Your Own System[edit | edit source]

  1. Install Docker Desktop or the Docker Engine.
  2. Save the files above into a new Docker Node folder:
    • Dockerfile
    • index.js
  3. At a command prompt, change to the Docker Node folder and then run the following commands:
    • docker build -t node-runner .
    • docker run --rm node-runner

See Also[edit | edit source]