RUN:
Executes the commands we pass in. In this case, clearing the npm cache and then installing all the dependencies from package.json.
ENTRYPOINT:
Executes the command you insert here, right when the docker container is started
Simple Express App:
Now that we have our Dockerfile ready to go we need a simple express application that we can run inside a container. For that, we create two new files like this:
$ touch server.js package.json
package.json will get two dependencies, first express, and second nodemon:
{
“name”: “node-docker”, “version”: “1.0.0”,
“description”: “”,
“main”: “server.js”,
“scripts”:
{
“start”: “nodemon server.js”
},
“author”: “Jakob Klamser”,
“license”: “MIT”,
“dependencies”: { “express”: “^4.17.1” },
“devDependencies”: {
“nodemon”: “^2.0.4”
}
}
The express application will just return simple HTML when hitting the main page. Therefore server.js should look like this:
const express = require(‘express’);
const app = express();
const PORT = process.env.PORT || 5000;
app.get(‘/’, (req, res) => {
res.send(
` <h1>Express + Docker</h1>
<span>This projects runs inside a Docker container</span>
`);
});
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}!`);
});