Deploy nodeJs on DigitalOcean droplet using docker

Good reference

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

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

You can check the page by access url http://localhost:3000/status

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

You can check the page by access url http://localhost:3000/status

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

Last updated