Github Education
  • Github學生免費服務推薦
  • name.com免費網域申請
  • SendGrid - Email Service
  • Configcat - Global config setting
  • Transloadit - File conversion to cloud storage
  • i18n 管理平台 - lingohub
  • Push Notifications - PushBots
  • BrowserStack
  • Page
  • Ai
    • Stable diffusion
    • Changing Images Backgrounds
  • Copy of Crypto currency games
    • Gods Unchained
  • Digital Ocean
    • How to create cloud instance and access it on Digital Ocean
    • Deploy nodeJs on DigitalOcean droplet using docker
    • Deploy Redis to your local
  • Heroku
    • Heroku Cli
    • How to deploy Hello world to Heroku using docker
    • How to deploy NodeJS application to Heroku using docker
  • APIs
    • Google Geocoding API
    • FourSquare
    • Building APIs with Swagger
  • Util
    • Google Cloud Storage - Object storage
    • Google Search Console
    • Google Sign-in with Angular Front End
    • Google Sign-in with Nodejs Backend
    • Github Package
  • 推薦課程
  • Currently interested in
  • Useful info
  • Become a Front End Web Developer | Udacity
    • 2. CSS, Website Layout, Website Components
      • Lesson 2: CSS
      • Lesson 3: Flexbox
      • Lesson 4: CSS Grid
      • Lesson 5: Creating Responsive Layouts
      • How to use Adobe Design tokens - Spectrum
    • 3. Javascript & The DOM
      • Lesson 1: Syntax
      • Lesson 2: The Document Object Model
      • Lesson 3: Creating Content with JavaScript
      • Lesson 4: Working with Browser Events
  • Some tips
    • Github Blame View
  • Free
    • Openshift(WIP)
Powered by GitBook
On this page
  • Good reference
  • Test Nodejs server
  • Create sample node js app
  • Install express
  • Add index.js
  • Add start command in package.json
  • Test locally
  • Check browser
  • Create my own docker image on docker hub
  • Create docker file
  • Build docker image using docker file
  • Check your image locally
  • Test your image locally
  • Check browser
  • Publishing the Image to Docker Hub
  • Check your docker images again
  • Login to docker hub
  • Push your image to docker hub
  • Deploy your docker image on DigitalOcean droplet
  • Access your droplet via ssh
  • Check docker version on your droplet
  • Check browser

Was this helpful?

  1. Digital Ocean

Deploy nodeJs on DigitalOcean droplet using docker

PreviousHow to create cloud instance and access it on Digital OceanNextDeploy Redis to your local

Last updated 4 years ago

Was this helpful?

Good reference

The following steps are my walk through of the above steps.

Test Nodejs server

Create sample node js app

>> npm init

Install express

>> npm install express --save

Add index.js

const express = require('express');
const app = express();
const port = 3000;

app.get('/status', (req, res) => res.send({status: "I'm alive!"}));

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

Add start command in package.json

"scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  }

Test locally

>> npm start

> digitalocean-node-helloworld-docker@1.0.0 start /Users/xx/Documents/GitHub/DigitalOcean-Node-Helloworld-docker
> node index.js

Example app listening on port 3000!

Check browser

Create my own docker image on docker hub

Create docker file

Create a docker file with name Dockerfile

FROM node:13-alpine

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000
CMD [ "npm", "start" ]

Build docker image using docker file

>> docker build . -t digital-ocean-app

Check your image locally

>> docker images

REPOSITORY                                      TAG       IMAGE ID       CREATED         SIZE
digital-ocean-app                               latest    930b7d8f81f2   2 minutes ago   119MB

Test your image locally

>> docker run -p 3000:3000 digital-ocean-app

Check browser

Publishing the Image to Docker Hub

In this way, you can pull/run your docker anywhere.

Before you push your images to docker hub, you need to specify your user name as tag

>> docker tag digital-ocean-app <your username>/digital-ocean-app

Check your docker images again

>> docker images

REPOSITORY                                      TAG       IMAGE ID       CREATED         SIZE
digital-ocean-app                               latest    930b7d8f81f2   3 minutes ago   119MB
<your username>/digital-ocean-app               latest    930b7d8f81f2   3 minutes ago   119MB

Login to docker hub

>> docker login

Push your image to docker hub

>> docker push <your username>/digital-ocean-app

Deploy your docker image on DigitalOcean droplet

Access your droplet via ssh

>> ssh root@<your droplet ipv4 addr>

Check docker version on your droplet

>> docker -v
Docker version 19.03.13, build 4484c46d9d

Run your docker image on droplet

>> docker run -p 3000:3000 <your docker username>/digital-ocean-app

Unable to find image '<your docker username>/digital-ocean-app:latest' locally
latest: Pulling from <your docker username>/digital-ocean-app
cbdbe7a5bc2a: Pull complete
780514bed1ad: Pull complete
5d74fb112a7d: Pull complete
4b9536424fa1: Pull complete
ce4565702f19: Pull complete
043416bc0285: Pull complete
7dd7a207632b: Pull complete
4c0886dc74d9: Pull complete
Digest: sha256:xxx
Status: Downloaded newer image for <your docker username>/digital-ocean-app:latest

> digitalocean-node-helloworld-docker@1.0.0 start /usr/src/app
> node index.js

Example app listening on port 3000!

Check browser

You can check the page by access url

http://<your droplet ipv4 addr>:3000/status

You can check the page by access url

You can check the page by access url

https://stackabuse.com/deploying-a-node-js-app-to-a-digitalocean-droplet-with-docker/
http://localhost:3000/status
http://localhost:3000/status