Docker/Express
Appearance
< Docker
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]- Use Play with Docker. Create an account and/or log in.
- Start an interactive session and add a new instance.
- In the terminal window, enter the following commands:
touch Dockerfile
touch app.js
- Use the
Editor
button to edit both files and save the contents above into each respective file. - Run the following commands:
docker build -t express-server .
docker run -d -p 3000:3000 --name=express-server express-server
- In the top window, select the
3000
button to connect to the running server.
On Your Own System
[edit | edit source]- Install Docker Desktop or the Docker Engine.
- Save the files above into a new
Docker Flask
folder:Dockerfile
app.js
- 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
- Open a web browser to connect to the running server: