Jump to content

Docker/Express

From Wikiversity

Dockerfile

[edit | edit source]
# Configures an Express web server and copies the current folder content
# to use as app root.

# Use the following commands to build and run the server.
#   docker build -t express-server .
#   docker run -d -p 3000:3000 --name=express-server express-server

# Then open a web browser and connect to http://localhost:3000 .

# References:
#   https://code.visualstudio.com/docs/containers/quickstart-node
#   https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

FROM node:alpine
WORKDIR /usr/src/app

COPY . .
RUN npm install express

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

app.js

[edit | edit source]
// Displays 'Hello world!'
//
// References:
//   https://repl.it/languages/express
//   https://expressjs.com/en/starter/hello-world.html

const express = require('express');
const app = express();

app.get('/', (request, response) => {
    response.send('Hello world!');
});

app.listen(3000, () => console.log('server started'));

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 app.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 express-server .
    • docker run -d -p 3000:3000 --name=express-server express-server
  6. In the top window, select the 3000 button to connect to the running server.

On Your Own System

[edit | edit source]
  1. Install Docker Desktop or the Docker Engine.
  2. Save the files above into a new Docker Flask folder:
    • Dockerfile
    • app.js
  3. At a command prompt, change to the Docker Flask folder and then run the following commands:
    • docker build -t express-server .
    • docker run -d -p 3000:3000 --name=express-server express-server
  4. Open a web browser to connect to the running server:

See Also

[edit | edit source]