How to create a Node App within a Docker container

Search for a command to run...

run "docker run -it docker_nodejs_app" in terminal, but when going to http://localhost:3000/ from the browser, i got the error: "This site can't be reached"
help?
run "docker run -p 3000:3000 docker_nodejs_app" instead of "docker run -it docker_nodejs_app"
solve my problem. :)
also, "EXPOSE 3000:3000" in Dockerfile - didnt work, and should be just: "EXPOSE 3000"
Hi, thanks for this tutorial. It's really clear and helpful.
I encountered an error when I tried to run the docker image
Node\docker_nodejs_app>docker run -it docker_nodejs_app
npm ERR! missing script: start
I googled and found that the package.json needs to add this to "scripts" section:
"start": "node app.js"
then rebuilding the docker image file and running it solves the error.
Nice to know the article was helpful
Introduction This is the second part of the series for Docker meets NodeJS where we are going to introduce a database service, MongoDB, which we will connect to it with our Node app through a network. Note: Since we are introducing a new database se...
Recently I started building tools that will help me automate my app deployment process easier and cheaper, to do that I knew CLIs are definitely going to be involved one way or another. So I started building two CLIs: Simp CLI: It's like Makefile ju...

Let's jump into it... fast. You got here because you want to setup Dokku and the other ways don't work... let's goooo I will setup for Dokku only Point your domain to the VPS of choice Spin up new VPS server here: Digital Ocean Linode Buy a dom...

So what is Dokku, another bit, another TL;DR; Let's jump into it. You might know Heroku, (my opinion) the greatest platform to ever make it easy to deploy server and client-side applications so fast and easy. Well Heroku is a business and they have t...

Okay for this one I highly recommend, the speeds is just amazing. I loved it. I am gonna do a comparison real quick, between browser-image-compression and react-image-file-resizer. browser-image-compressionreact-image-file-resizer Really fastCo...

I recently got interested in compressing images on the client-side for a couple of reasons: Reduce my cloudinary bandwidth and storage usage Users were complaining about long waits while uploading images So I ventured to look for a library that wil...

Before you go through this fun tutorial ensure:
Docker is and in theory how it works.You have should node and npm installed on your computer. You can do this by typing this in your terminal.
$ node --version && node --version
Also, since we talking containers well you need docker installed.
$ docker --version
Create our project folder, where our codebase will be housed
$ mkdir docker_nodejs_app
Let's change the directory to our app folder.
$ cd docker_nodejs_app
Since this is a node project, we need a package.json file to track our project dependencies.
To create one pretty fast type this in your terminal.
$ npm init -y
We will be using express as our default node web framework.
$ npm install express --save # Introduce the save flag to track it in the package.json file
{
"name": "docker_node_app",
"version": "1.0.0",
"description": "nodejs image demo",
"author": "your name",
"license": "MIT",
"main": "app.js",
"keywords": [],
"scripts": {
"start":"node app.js"
},
"dependencies": {
"express": "^4.16.4"
}
}
We will create a simple express server. Let's create the file that will hold our server code.
You can use the terminal to create the file
$ touch app.js # Creates the file from the terminal
Or your locally installed code editor.
Let us write our server code.
"use strict"; // Ensures our code is compiled in strict mode
// Lets import our web framework
var express = require("express");
// Initialise our app
const app = express();
// Lets set our port
/**
* The default port number is `3000`
* Take note on that as we will come to that.
*/
app.set("port", 3000);
/**
* To ensure works as it should we will create a
* simple endpoint to return a json response
*/
// Define our json response
const data = {
blog_name: "docker_nodejs_app",
blog_author: "wachira (tesh254)",
blog_author_twitter: "@wachira_dev"
};
// Define out GET request endpoint
app.get("/", (req, res) => {
res.status(200).json(data);
});
// Initialize our server
app.listen(app.get("port"), () => {
console.log(`Server listening on port ${app.get("port")}`);
});
Let's run it, it's a simple server meaning its bug-free.
$ node app.js
You should see the same text on your terminal.

Let's test our endpoint on our browser.

For you to run your server within a container you a couple of things:
docker-compose.yml: Not a must but comes in handy if you plan to add services like a databasepackage.json file for node or requirements.txt for python..dockerignore: Not a must but it allows you to exclude files from the context like a .gitignore file allows you to exclude files from your git repository.Let's create and write our Dockerfile
$ touch Dockerfile
You can copy and paste the configurations to your Dockerfile.
# Define the image we will use and version
# latest just means we need the latest nodejs image available
FROM node:8
# Create an app directory to hold the application code
WORKDIR /usr/docker_nodejs_app/src/app
# Duplicate the dependency file to the container's project root directory.
COPY package*.json ./
# Install app dependencies
RUN npm install
# Bundle app source inside the docker image
COPY . .
# Expose our app port inside the app and
EXPOSE 3000:3000
# Define commands that will run the app
CMD ["npm", "start"]
Turn to your terminal and build your container.
$ docker build -t docker_nodejs_app .
You should see something like this on your terminal when your build is done.

Let's run our app from docker
$ docker run -it docker_nodejs_app
If you did everything in this tutorial right then you should see something similar to the screenshot below.

Test it out on a browser, the same results expected.
This tutorial will be a series, this being the first part. The parts will be as follow:
MongoDB.Heroku. Link to repo https://github.com/werickblog/docker_nodejs_app
Link to download NodeJS https://nodejs.org/en/download/
Link to download Docker https://www.docker.com/get-started
Understanding what docker is https://docs.docker.com/engine/docker-overview/