Deploy web app project running on a Docker container

In this article as we begin to progress forward with deploying containers I wanted to build on previous articles.  In this article we will create a package.json and server.js file, create a Dockerfile and see if our web app works.

Now let’s create a directory for our project:

Linux terminal showing mkdir and cd commands used to create and enter the Yankees web app project directory.

$ mkdir <name_of_project>
$ cd <DIR>

Deploy web app running on a Docker Container

In this article as we begin to progress forward with deploying containers I wanted to build on previous articles.  In this article we will create a package.json and server.js file, create a Dockerfile and see if our web app works.

Now let’s create the package.json file – use the editor of your choice

Screenshot from the Run a Web App in a Docker Container walkthrough at step 2; the adjacent article text explains that let’s create the package.json file - use the editor of your choice.
{
  "name": "yankees_web_app",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "Paul Bryant",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.16.1"
  }
}

Screenshot from the Run a Web App in a Docker Container walkthrough at step 3; the adjacent article text explains that let’s create the package.json file - use the editor of your choice.

Now let’s create the server.js file – once again use the editor of your choice

'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('New York Yankees are the best team in baseball history');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
Linux terminal showing npm install completing for the Yankees web app with 50 packages added and no vulnerabilities found.

$ npm install

Now let’s build the Dockerfile

Screenshot from the Run a Web App in a Docker Container walkthrough at step 5, showing RUN npm install; the adjacent article text explains that $ npm install.

FROM node
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ “node”, “server.js” ]

Screenshot from the Run a Web App in a Docker Container walkthrough at step 6, showing npm-debug. log; the step instructs the user to create a .dockerignore file and put the following into it:node_modulesnpm-debug.log.

Create a .dockerignore file and put the following into it:
node_modules
npm-debug.log

Screenshot from the Run a Web App in a Docker Container walkthrough at step 7, showing Sending build context to Docker daemon 18.94KB; the step instructs the user to create a .dockerignore file and put the following into it:node_modulesnpm-debug.log.

Now let’s build the image
$ docker build -t pbryant/yankees-web-app .

Screenshot from the Run a Web App in a Docker Container walkthrough at step 8; the step instructs the user to create a .dockerignore file and put the following into it:node_modulesnpm-debug.log.

$ docker run -p 8080:8080 pbryant/yankees-web-app:latest

Screenshot from the Run a Web App in a Docker Container walkthrough at step 9; the adjacent article text explains that $ docker run -p 8080:8080 pbryant/yankees-web-app:latest.

As you can see we have gotten our intended results and can hit the webpage from our internal network on port 8080

What if we wanted to make a change to our webpage and have it updated in Docker?

Screenshot from the Run a Web App in a Docker Container walkthrough at step 10; the adjacent article text explains that what if we wanted to make a change to our webpage and have it updated in Docker.

Edit the server.js file with whatever you want to add

Screenshot from the Run a Web App in a Docker Container walkthrough at step 11, showing Sending build context to Docker daemon 18.94kB; the adjacent article text explains that what if we wanted to make a change to our webpage and have it updated in Docker.

Rebuild the image – this is a requirement

Screenshot from the Run a Web App in a Docker Container walkthrough at step 12; the step instructs the user to run the container based off the updated image.

Run the container based off the updated image

Screenshot from the Run a Web App in a Docker Container walkthrough at step 13; the step instructs the user to run the container based off the updated image.

As you can see the changes made that we made have been updated

Summary:
I hope this quick walkthrough of a project format helped further your understanding of Docker.  As always, I hope y’all found this useful.

Keep exploring

Choose your next step

Continue with the path that best matches the architecture or operating challenge in front of you.

Leave a Reply

Discover more from Digital Thought Disruption

Subscribe now to keep reading and get access to the full archive.

Continue reading