README.md 2.34 KB
Newer Older
Michal Ratajsky's avatar
Michal Ratajsky committed
1
# IOT node.js project starting template
Michal Ratajsky's avatar
Michal Ratajsky committed
2
3

This repository includes a simple template for a node.js/express application,
Michal Ratajsky's avatar
Michal Ratajsky committed
4
5
6
7
8
along with a `Dockerfile`.

The included `Dockerfile` should be general enough to be usable in your own node.js
applications as well.

Michal Ratajsky's avatar
Michal Ratajsky committed
9
**Important**: the included `.dockerignore` and `.gitignore` files can get lost if you
Michal Ratajsky's avatar
Michal Ratajsky committed
10
11
12
manually copy files from this repository into your own repository as some file managers
tend to hide these files. Make sure you do have these files to prevent building problems
when you move your application between different platforms.
Michal Ratajsky's avatar
Michal Ratajsky committed
13
14
15
16
17
18
19
20
21
22

## Running the application manually

The application consists of a single file `src/app.js`. It can be started from
the application directory with the `node` command:

```
$ node src/app.js
```

Michal Ratajsky's avatar
Michal Ratajsky committed
23
The `package.json` file is set up to include a `start` script and express + sensor
Michal Ratajsky's avatar
Michal Ratajsky committed
24
package dependencies. Scripts provide a more convinient way to start the application
Michal Ratajsky's avatar
Michal Ratajsky committed
25
using `npm`:
Michal Ratajsky's avatar
Michal Ratajsky committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

```
$ npm start
```

## Building the docker image

To build the image, use `docker build` inside the application directory:

```
$ docker build -t fancy-pants/iot-assignment .
```

The `-t` switch is followed by the image name (tag), in this example consisting
of a group name and a chosen application name, separated by a slash.

Michal Ratajsky's avatar
Michal Ratajsky committed
42
**Important**: this must be done on a Raspberry Pi with working internet connection.
Michal Ratajsky's avatar
Michal Ratajsky committed
43

Michal Ratajsky's avatar
Michal Ratajsky committed
44
45
46
47
48
49
## Running the docker image

To create a container from the created image and run the application, use the
`docker run` command:

```
Michal Ratajsky's avatar
Michal Ratajsky committed
50
$ docker run -d -p 8080:8080 --privileged fancy-pants/iot-assignment
Michal Ratajsky's avatar
Michal Ratajsky committed
51
52
53
54
55
56
57
```

The `-d` switch detaches the running container from the terminal, `-p 8080:8080`
binds the local port 8080 to the port exposed by the application (also 8080 in
case of this example). The `--privileged` switch allows sensor packages to access
the GPIO.

Michal Ratajsky's avatar
Michal Ratajsky committed
58
## Inspecting containers
Michal Ratajsky's avatar
Michal Ratajsky committed
59

Michal Ratajsky's avatar
Michal Ratajsky committed
60
61
62
63
64
There are many ways to diagnose and debug running containers. One useful command is
`docker logs`. It accepts (the initial part of) a container ID and prints out the
output of your application inside the container. Container ID is an identifier of a
container and it is printed out when running `docker run` or can be found out anytime
later by running `docker ps`.
Michal Ratajsky's avatar
Michal Ratajsky committed
65

Michal Ratajsky's avatar
Michal Ratajsky committed
66
67
68
69
70
71
72
73
Example:

```
$ docker run -d -p 8080:8080 --privileged fancy-pants/iot-assignment
115e6ec5c3bea754ee54a20c8116765c37ba1d4a6819858892d8e4ca90a2d0fd

$ docker logs 115e
```