DEV Community

Emma Donery
Emma Donery

Posted on • Updated on

Getting Started with Nodejs, Express and Docker

image

In this article, we are going to learn how to get started with Nodejs, Express and Docker.
If you want some basics on how to use Docker i would advice you to read my article on Understanding Docker Basics. This will equip you with all the necessary knowledge you need to start working with Docker.

Prerequisites

  • Javascript
  • Docker Basics

Understanding Docker

Docker is an open source platform that provides an open platform for building, shipping, and running distributed applications. It automates routine configuration procedures and is used across the development lifecycle to create fast, simple, and portable applications.

Understanding Nodejs And Express

Node.js is a JavaScript runtime framework which is used to create networking and server-side applications.

Express is a small framework that sits on top of Node.jsโ€™s web server functionality that provides a robust set of features to develop web and mobile applications.

Why Dockerize your application

  • Rapid application deployment
  • Portability across machines
  • Version control and component reuse
  • Sharing of images/dockerfiles
  • Lightweight footprint and minimal overhead
  • Simplified maintenance

NodeJs app

  • Create a new directory where all the files would live
  • Create a package.json file in this directory to define your project and its dependencies:
{
  "name": "express_app",
  "version": "1.0.0",
  "license": "MIT",
  "description": "Node.js and express on Docker",
  "author": "Firstname Lastname <mynames@example.com>",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "^4.16.1"
  }
}
Enter fullscreen mode Exit fullscreen mode
  • Using the Express.js framework, create a app.js file that describes a web app:
const express = require('express')
const app = express()
const PORT = 3000
const HOST = '0.0.0.0'

// App
const app = express()
app.get('/', (req, res) => {
  res.send('Hello World')
});

app.listen(PORT, HOST)
console.log(`Our app running on http://${HOST}:${PORT}`)
Enter fullscreen mode Exit fullscreen mode

Run the app

$ node app.js
Go to http://localhost:3000/ in your browser to view it.

Dockerizing the Application

  • Create empty Dockerfile

Dockerfile

FROM node:12-alpine3.14

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 8080

CMD [ "node", "app.js" ]
Enter fullscreen mode Exit fullscreen mode

Here is what is happening:

Set the working directory to /usr/src/app

Copy the package.json file to /usr/src/app

Install node_modules

Copy all the files from the project's root to /usr/src/app

  • Create a .dockerignore
node_modules
npm-debug.log
Enter fullscreen mode Exit fullscreen mode

NB: If you are working with Git then you will also want to add your .git directory and .gitignore file.

  • Build Docker Image
$ docker build -t hello-world .
Enter fullscreen mode Exit fullscreen mode
  • Run docker container
$ docker run -p 8080:8080 hello-world
Enter fullscreen mode Exit fullscreen mode
  • Sharing the docker image
    For you to share a docker image, you have to first signup at Docker hub.
    Docker Hub is a Docker service that allows you to locate and share container images with your team.
    After signing up:

    • Re-create the image with your Docker Hub credentials.
    $ docker build -t [USERNAME]/hello-world .
    
    • Login to Docker Hub
    $ docker login
    
    • Push the image to Docker Hub
    $ docker push [USERNAME]/hello-world
    

Congratulations!๐ŸŽ‰ ๐ŸŽ‰ The image can now be used on any server or PC that has Docker installed:

docker run [USERNAME]/hello-world
Enter fullscreen mode Exit fullscreen mode

Docker Compose

Docker Compose is a tool for running multi-container applications on Docker. You configure your application's services with Compose using a YAML file. Then you build and start all of the services from your setup with a single command.

Compose enables running apps in a single or more containers simple. To construct or execute containers, we don't need to remember particularly long commands. Your applications will operate smoothly as long as you can run docker-compose build and docker-compose up.

  • In the project root directory, create a docker-compose.yml file.

docker-compose.yml

version: '3.8'  #specifies docker compose version
services:
  web:
    build:
      context: ./
      target: dev
    volumes:
      - .:/src
    command: npm run start:dev
    ports:
      - "8080:8080"
    environment:
      NODE_ENV: development
      DEBUG: nodejs-docker-express:*
Enter fullscreen mode Exit fullscreen mode

In this article, i have a service name web which has a build context and a target set to dev. This tells Docker that i want to build the Docker image with the dev stage.

The volume instructs Docker to copy and sync changes from the local directory./ of the host with /src on the Docker container.

Exposing port 8080 exposes the port where the Node.js Express web server runs by default.

Build and run your app with Compose

  • start up your application by running the docker-compose up command
$ docker-compose up
Enter fullscreen mode Exit fullscreen mode
  • Access http://localhost:8000/ in a browser to see the application running.

  • Stop the application from running using the docker-compose down command.

$ docker-compose down
Enter fullscreen mode Exit fullscreen mode

Conclusion

Docker Compose is an excellent tool for launching numerous containers. For the sake of this article, i solely used Node.js with a single container running on Docker.

Node.js and Docker gets along swimmingly. The development experience is substantially smoother when docker-compose is used. You can use this article as a starting point for learning more advanced Docker and Node.js skills.

๐Ÿ˜Š Have fun coding!๐Ÿ˜Š
Be sure to leave any comments for me.

You can connect with me on twitter https://twitter.com/EmmaDonery
or Linkedin https://www.linkedin.com/in/emma-donery-0831a7188/

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.