# Deploy nodeJs on DigitalOcean droplet using docker

## Good reference

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

{% hint style="info" %}
The following steps are my walk through of the above steps.
{% endhint %}

## Test Nodejs server

### Create sample node js app

```bash
>> npm init
```

### Install express

```bash
>> npm install express --save
```

### Add index.js

```javascript
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

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

### Test locally

```bash
>> 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**

```bash
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

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

### Check your image locally

```bash
>> docker images

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

### Test your image locally

```bash
>> 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

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

### Check your docker images again

```bash
>> 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

```bash
>> docker login
```

### Push your image to docker hub

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

## Deploy your docker image on DigitalOcean droplet

### Access your droplet via ssh

```bash
>> ssh root@<your droplet ipv4 addr>
```

### Check docker version on your droplet

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

Run your docker image on droplet

```bash
>> 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

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://owen31302.gitbook.io/github-education/digital-ocean/deploy-node-js-on-digitalocean-droplet-using-docker.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
