In this section we will deploy a static web app, for now, no forms, no APIs, just a simple informative web app. For this we need to have a website running locally if you will.
Pre-Requisites
- Docker Hub account and Docker Installed in your computer.
- Nodejs
- Docker
Create the Node.js app
Lets build a home for all the content we are about to create.
sudo mkdir docker_demoapp
sudo chown info docker_demoapp
And in this directory we will create a package.json file
{
"name": "docker_webapp",
"version": "1.0.0",
"description": "Node.js WebApp on Docker",
"author": "Black Gem ",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.1"
}
}
'use strict';
const express = require('express');
// Constants
const PORT = 8080;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
res.send('Blackgem says Welcome');
});
app.listen(PORT, HOST, () => {
console.log(`Running on http://${HOST}:${PORT}`);
});
Docker File
Create an empty file called Dockerfile:sudo touch Dockerfile
sudo chown info Dockerfile
2. Next we create a directory to hold the application code inside the image, this will be the working directory for your application.
3. This image comes with Node.js and npm already installed so the next thing we need to do is to install your app dependencies using the npm binary.
4. To bundle your app's source code inside the Docker image, use the COPY instruction
5. Our app binds to port 8080 so we will use the EXPOSE instruction to have it mapped by the docker daemon.
6. Define the command to run the app using CMD which defines the runtime. Here, we will use node server.js to start our server.
FROM node:16
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --omit=dev
# Bundle app source
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
docker build . -t blackgem/node-webapp
sudo docker run -p 49160:8080 -d blackgem/node-webapp
sudo docker ps
No comments:
Post a Comment