Coder Social home page Coder Social logo

aviddabbler / docker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nodeshift/docker

0.0 1.0 0.0 16 KB

Sample Dockerfile templates for building Dev, Debug, Test and Run images for your application

License: Apache License 2.0

Dockerfile 68.49% Shell 31.51%

docker's Introduction

Docker Templates for Node.js

This project uses the official Docker images built from the Node.js docker-node project and provides guidance on how to add best-practise Dockerfiles into your project.

This project contains three template Dockerfiles:

Dockerfile Description
Dockerfile A default Dockerfile for building and running your Node.js application
Dockerfile-tools A Dockerfile for developing or debugging your Node.js application
Dockerfile-run A multi-stage Dockerfile for building a "run" image for your application, based on the Node.js "slim" image

In order to use these template files, copy the files from this project into your application directory.

Installing the Templates

The templates can most easily be installed into your project using either of the following mechanisms:

  • All Files:
    Use the "Clone or download" button and select "Download ZIP" to copy the entire set of templates, and copy those into your project.
  • Individual Files:
    Use "wget" to download individual files using:
    wget https://raw.githubusercontent.com/CloudNativeJS/docker/master/{file}

Using Dockerfile

The Dockerfile template creates a Docker image for your application that:

  • Uses the node:14 image
  • Runs your application under the node user

The template also makes the following assumptions about your application:

  • It listens on port 3000
  • It can be started using npm start

You can change these settings by updating the EXPOSE and CMD entries of the Dockerfile.

Building the Docker image for your application

After any required changes have been made to the Dockerfile, you can build a Docker image for your application using the following command:

docker build -t my-nodejs-application -f Dockerfile .

where my-nodejs-application is the name you want to give your created Docker image.

Running the Docker image for your application

After the Docker image has been created for your application, you can run it using either of the following commands:

  • Run as an interactive application on your command line:

    docker run -i -p 3000:3000 -t my-nodejs-application

    This maps port 3000 in the Docker image to port 3000 on your machine. If you are using a different port, you will need to change the mapping.

  • Run as a daemon process:

    docker run -d -p 3000:3000 -t my-nodejs-application

    This uses the -d flag rather than the -i flag to run the Docker image as a background task.

Using Dockerfile-tools

The Dockerfile-tools template creates a Docker image for your application that:

  • Uses the node:14 image
  • Runs you application under the node user
  • Provides a script for running in dev mode using nodemon
  • Provides a script for running in debug mode using node --inspect

The template also makes the following assumptions about your application:

  • It listens on port 3000
  • It can be started using node server/server.js

You can change the port by editing the EXPOSE entry in the Dockerfile-tools file, and the start command by editing the run-dev and run-debug scripts.

Building the Docker tools image for your application

After any required changes have been made to the Dockerfile-tools, you can build a Docker image for your application using the following command:

docker build -t my-nodejs-application-tools -f Dockerfile-tools .

where my-nodejs-application-tools is the name you want to give your created tools Docker image.

Running the Docker tools image for your application: Development Mode

Running the image in Development Mode uses nodemon to watch for changes in your application and automatically restart it as those changes are made.

To enable your local changes to be updated in the Docker image, you must map your local file system into the running Docker container, as follows:

  1. Generate a Linux version of your node_modules dependencies locally, by generating them inside the node:14 docker image:
docker run -i -v "$PWD"/package.json:/tmp/package.json -v "$PWD"/node_modules_linux:/tmp/node_modules -w /tmp -t node:14 npm install

This step only needs to be repeated if you modify your package.json file.

  1. Run the Docker tools image as an interactive application on your command line in dev mode:
docker run -i -p 3000:3000 -v "$PWD"/:/app -v "$PWD"/node_modules_linux:/app/node_modules -t my-nodejs-application-tools /bin/run-dev

This maps port 3000 in the Docker image to port 3000 on your machine. If you are using a different port, you will need to change the mapping. This command also maps your local directory into the Docker container, allowing you to modify your Node.js application code and see the changes running immediately in the container.

Running the Docker tools image for your application: Debug Mode

In order to run your application in debug mode:

  • Run as an interactive applications on your command line in debug mode:
    docker run -i -p 3000:3000 -p 9229:9229 -t my-nodejs-application-tools /bin/run-debug
    This maps port 3000 in the Docker image to port 3000 on your machine. If you are using a different port, you will need to change the mapping. This command also maps port 9229 in the image to the same port on your machine so that you can connect the debugger.

If you wish to run your Docker tools image as a background task, switch the -i flag to -d on the command line.

Using Dockerfile-run

The Dockerfile-run template creates a Docker image using a multi-stage build that:

  • Retrieves your dependencies and compiles any native add-ons using the node:14 image
  • Copies your dependencies into the node:14-slim image for reduced size
  • Runs your application under the node user

The template also makes the following assumption about your application:

  • It listens on port 3000
  • It can be started using npm start

You can change these settings by updating the EXPOSE and CMD entries of the Dockerfile-run template.

Building the Docker run image for your application

After any required changes have been made to the Dockerfile-run file, you can build a Docker image for your application using the following command:

docker build -t my-nodejs-application-run -f Dockerfile-run .

where my-nodejs-application-run is the name you want to give your created Docker run image.

Running the Docker run image for your application

After the Docker run image has been created for your application, you can run it using either of the following commands:

  • Run as an interactive application on your command line:

    docker run -i -p 3000:3000 -t my-nodejs-application-run

    This maps port 3000 in the Docker image to port 3000 on your machine. If you are using a different port, you will need to change the mapping.

  • Run as a daemon process:

    docker run -d -p 3000:3000 -t my-nodejs-application-run

    This additionally uses the -d flag to run the Docker image as a background task.

Publishing the Image

In order to use your application's Docker image in a cloud it needs to be published. You can deploy to the image to the DockerHub registry using the following commands:

  1. Log in to DockerHub:

    docker login

    This logs you into DockerHub using your Docker ID, where you have a namespace that matches your DockerHub ID.

  2. Tag your application's Docker image with the name you want to use in DockerHub:

    docker tag my-nodejs-application-run <namespace>/nodeserver:1.0.0

    where <namespace> is your namespace in DockerHub.

  3. Publish your application's Docker image to DockerHub:

    docker push <namespace>/nodeserver:1.0.0

    where <namespace> is your namespace in DockerHub.

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.