Giter Club home page Giter Club logo

node-docker-demo's Introduction

Node-Docker-Demo

Containerizing node.js applications

Building a nodejs app

Build a simple nodejs application

Build the node application docker image

cd building-a-nodejs-app

# build
docker build -t my-node-app .

# docker image size
docker inspect my-node-app

# run
docker run --init --rm --publish 3000:3000 my-node-app

Multi stage build

Preparing a multi stage build process

cd multi-stage-build

# build
docker build -t multi-node -f multi.node.Dockerfile .

# docker image size
docker inspect multi-node

# run
docker run --init --rm -p 3000:3000 multi-node

Serve static files using nginx

Create application using create-react-app and serve static files using nginx

cd serve-static-files-nginx

# build
docker build -t static-app .

# run
# Port exposed to host is 8080. Internal port used is 80. And nginx uses 80 as the default port
docker run --init --rm -p 8080:80 static-app

# Using docker bind mount. Below command mounts the /build directory inside nginx html directory
docker run --mount type=bind,source="$(pwd)"/build,target=/usr/share/nginx/html -p 8080:80 nginx:1.17-alpine

Docker bind

Bind mount

  • Bind mount are works great when you need to share data between host and container.
  • Build image docker build --tag=incrementor-bind .
    • New image create with the tag name incrementor-bind
  • Run container docker run --rm --env DATA_PATH=data/num.txt --mount type=bind,source="$(pwd)"/incrementor-data,target=/src/data incrementor-bind

Bind mount command run down screenshot

Volume mount

  • Using bind volumes containers can maintain states between runs.
  • Build image docker build --tag=incrementor-volume .
  • Run container docker run --rm --env DATA_PATH=/data/num.txt --mount type=volume,src=incrementor-data,target=/data incrementor-volume

Volume mount command run down screenshot

Explain all docker command attributes used

Try all example commands within this directory

  • --env Passes process environment variable. For example docker run --rm --env DATA_PATH=data/num.txt --mount type=bind,source="$(pwd)"/incrementor-data,target=/src/data incrementor-bind. Here DATA_PATH is the process environment variable passed. Node application can use this data by process.env.DATA_PATH
  • --rm Deletes the container after exiting the process. Try running any container with and without this flag to see the difference. For example docker run --rm --env DATA_PATH=data/num.txt --mount type=bind,source="$(pwd)"/incrementor-data,target=/src/data incrementor-bind deletes the container after exiting the run process, but docker run --env DATA_PATH=data/num.txt --mount type=bind,source="$(pwd)"/incrementor-data,target=/src/data incrementor-bind doesn't deletes the container after exiting the process
  • src or source mount source directory i.e docker run --rm --env DATA_PATH=/data/num.txt --mount type=volume,src=incrementor-data,target=/data incrementor-volume or docker run --rm --env DATA_PATH=/data/num.txt --mount type=volume,source=incrementor-data,target=/data incrementor-volume
  • --init Gracefully exits node process where listen is used

Inspired & credits to @btholt for creating a course in FEM & sharing complete into to containers

Docker Networking

node-docker-demo's People

Contributors

samarpanda avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

node-docker-demo's Issues

What are bind & volume mounts?

What are bind & volume mounts?

Feature requests

  • What is bind mount? Explain with an example app.
  • What is volume mount? Explain with an example app.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.