Skip to content

Commit aca6e30

Browse files
author
Ric Harvey
committed
Fix nginx config and package.json + update README
1 parent b631784 commit aca6e30

File tree

5 files changed

+57
-46
lines changed

5 files changed

+57
-46
lines changed

Dockerfile

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ ADD ./package.json /usr/share/nginx/html/package.json
4646
RUN mkdir /usr/share/nginx/html/public
4747
Add ./ngineered.png /usr/share/nginx/html/public/ngineered.png
4848
49+
# Add log directories
50+
RUN mkdir /var/log/node/
51+
4952
# Supervisor Config
5053
RUN /usr/bin/easy_install supervisor
5154
RUN /usr/bin/easy_install supervisor-stdout

README.md

+47-16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
## Introduction
2-
This is a Dockerfile to build a container image for nginx and php-fpm, with the ability to pull website code from git. The container can also use environment variables to configure your web application using the templating detailed in the special features section.
2+
This is a Dockerfile to build a container image for nginx and nodejs, with the ability to pull website code from git. The container can also use environment variables to configure your web application using the templating detailed in the special features section.
33

44
### Git reposiory
5-
The source files for this project can be found here: [https://github.com/ngineered/nginx-php-fpm](https://github.com/ngineered/nginx-php-fpm)
5+
The source files for this project can be found here: [https://github.com/ngineered/nginx-nodejs](https://github.com/ngineered/nginx-nodejs)
66

77
If you have any improvements please submit a pull request.
88

99
### Docker hub repository
10-
The Docker hub build can be found here: [https://registry.hub.docker.com/u/richarvey/nginx-php-fpm/](https://registry.hub.docker.com/u/richarvey/nginx-php-fpm/)
10+
The Docker hub build can be found here: [https://registry.hub.docker.com/u/richarvey/nginx-nodejs/](https://registry.hub.docker.com/u/richarvey/nginx-nodejs/)
1111

1212
## Nginx Versions
1313
- Mainline Version: **1.7.9**
@@ -18,25 +18,60 @@ The Docker hub build can be found here: [https://registry.hub.docker.com/u/richa
1818
Pull the image from the docker index rather than downloading the git repo. This prevents you having to build the image on every docker host.
1919

2020
```
21-
docker pull richarvey/nginx-pfp-fpm:latest
21+
docker pull richarvey/nginx-nodejs:latest
2222
```
2323
To pull the Stable Version:
2424

2525
```
26-
docker pull richarvey/nginx-pfp-fpm:stable
26+
docker pull richarvey/nginx-nodejs:stable
2727
```
2828
## Running
2929
To simply run the container:
3030

3131
```
32-
sudo docker run --name nginx -p 8080:80 -d richarvey/nginx-php-fpm
32+
sudo docker run --name nginx -p 8080:80 -d richarvey/nginx-nodejs
3333
```
3434
You can then browse to http://<docker_host>:8080 to view the default install files.
35+
### Running your nodeJS app and default port
36+
The container will automatically run any file in the */usr/share/nginx/html* directory named server.js, so if you are pulling your own code from git be sure to name your application *server.js*.
37+
38+
Nginx is set to listen for your application on port 3000 on the localhost. So make sure you set your node app to run on port 3000:
39+
40+
```
41+
var server = app.listen(3000, function () {
42+
...
43+
```
44+
45+
To view your application you can go to:
46+
```
47+
http://<docker_ip>:8080
48+
```
49+
### Serving Static Content from Nginx
50+
By placing files in the */usr/share/nginx/html/public* directory nginx will serve the static files directly. You can then browse the files directly:
51+
52+
Example:
53+
```
54+
http://<docker_ip>:8080/public/ngineered.png
55+
```
3556
### Volumes
3657
If you want to link to your web site directory on the docker host to the container run:
3758

3859
```
39-
sudo docker run --name nginx -p 8080:80 -v /your_code_directory:/usr/share/nginx/html -d richarvey/nginx-php-fpm
60+
sudo docker run --name nginx -p 8080:80 -v /your_code_directory:/usr/share/nginx/html -d richarvey/nginx-nodejs
61+
```
62+
### NPM
63+
To install npm dependancies simply create a package.json file int he route of your code. The example below install express 4.x
64+
65+
```
66+
{
67+
"name": "node-server-example",
68+
"description": "Hello World App",
69+
"version": "0.0.1",
70+
"private": true,
71+
"dependencies": {
72+
"express": "4.x"
73+
}
74+
}
4075
```
4176
### Pulling code from git
4277
One of the nice features of this container is its ability to pull code from a git repository with a couple of environmental variables passed at run time.
@@ -46,13 +81,13 @@ One of the nice features of this container is its ability to pull code from a gi
4681
To run the container and pull code simply specify the GIT_REPO URL including *git@* and then make sure you have a folder on the docker host with your id_rsa key stored in it:
4782

4883
```
49-
sudo docker run -e '[email protected]:ngineered/ngineered-website.git' -v /opt/ngddeploy/:/root/.ssh -p 8080:80 -d richarvey/nginx-php-fpm
84+
sudo docker run -e '[email protected]:ngineered/ngineered-website.git' -v /opt/ngddeploy/:/root/.ssh -p 8080:80 -d richarvey/nginx-nodejs
5085
```
5186

5287
To pull a repository and specify a branch add the GIT_BRANCH environment variable:
5388

5489
```
55-
sudo docker run -e '[email protected]:ngineered/ngineered-website.git' -e 'GIT_BRANCH=stage' -v /opt/ngddeploy/:/root/.ssh -p 8080:80 -d richarvey/nginx-php-fpm
90+
sudo docker run -e '[email protected]:ngineered/ngineered-website.git' -e 'GIT_BRANCH=stage' -v /opt/ngddeploy/:/root/.ssh -p 8080:80 -d richarvey/nginx-nodejs
5691
```
5792
### Linking
5893
Linking to containers also exposes the linked container environment variables which is useful for templating and configuring web apps.
@@ -84,15 +119,15 @@ MYSQL_PORT=tcp://172.17.0.236:3306
84119
To link the container launch like this:
85120

86121
```
87-
sudo docker run -e '[email protected]:ngineered/ngineered-website.git' -v /opt/ngddeploy/:/root/.ssh -p 8080:80 --link some-mysql:mysql -d richarvey/nginx-php-fpm
122+
sudo docker run -e '[email protected]:ngineered/ngineered-website.git' -v /opt/ngddeploy/:/root/.ssh -p 8080:80 --link some-mysql:mysql -d richarvey/nginx-nodejs
88123
```
89124
### Enabling SSL or Special Nginx Configs
90125
As with all docker containers its possible to link resources from the host OS to the guest. This makes it really easy to link in custom nginx default config files or extra virtual hosts and SSL enabled sites. For SSL sites first create a directory somewhere such as */opt/deployname/ssl/*. In this directory drop you SSL cert and Key in. Next create a directory for your custom hosts such as */opt/deployname/sites-enabled*. In here load your custom default.conf file which references your SSL cert and keys at the location, for example: */etc/nginx/ssl/xxxx.key*
91126

92127
Then start your container and connect these volumes like so:
93128

94129
```
95-
sudo docker run -e '[email protected]:ngineered/ngineered-website.git' -v /opt/ngddeploy/:/root/.ssh -v /opt/deployname/ssl:/etc/nginx/ssl -v /opt/deplyname/sites-enabled:/etc/nginx/sites-enabled -p 8080:80 --link some-mysql:mysql -d richarvey/nginx-php-fpm
130+
sudo docker run -e '[email protected]:ngineered/ngineered-website.git' -v /opt/ngddeploy/:/root/.ssh -v /opt/deployname/ssl:/etc/nginx/ssl -v /opt/deplyname/sites-enabled:/etc/nginx/sites-enabled -p 8080:80 --link some-mysql:mysql -d richarvey/nginx-nodejs
96131
```
97132

98133
## Special Features
@@ -103,11 +138,9 @@ This container will automatically configure your web application if you template
103138
Example:
104139

105140
```
106-
<?php
107141
database_name = $$_MYSQL_ENV_MYSQL_DATABASE_$$;
108142
database_host = $$_MYSQL_PORT_3306_TCP_ADDR_$$;
109143
...
110-
?>
111144
```
112145

113146
### Using environment variables
@@ -116,7 +149,7 @@ If you want to link to an external MySQL DB and not using linking you can pass v
116149
Example:
117150

118151
```
119-
sudo docker run -e '[email protected]:ngineered/ngineered-website.git' -e 'GIT_BRANCH=stage' -e 'MYSQL_HOST=host.x.y.z' -e 'MYSQL_USER=username' -e 'MYSQL_PASS=password' -v /opt/ngddeploy/:/root/.ssh -p 8080:80 -d richarvey/nginx-php-fpm
152+
sudo docker run -e '[email protected]:ngineered/ngineered-website.git' -e 'GIT_BRANCH=stage' -e 'MYSQL_HOST=host.x.y.z' -e 'MYSQL_USER=username' -e 'MYSQL_PASS=password' -v /opt/ngddeploy/:/root/.ssh -p 8080:80 -d richarvey/nginx-nodejs
120153
```
121154

122155
This will expose the following variables that can be used to template your code.
@@ -129,12 +162,10 @@ MYSQL_PASS=password
129162
To use these variables in a template you'd do the following in your file:
130163

131164
```
132-
<?php
133165
database_host = $$_MYSQL_HOST_$$;
134166
database_user = $$_MYSQL_USER_$$;
135167
database_pass = $$_MYSQL_PASS_$$
136168
...
137-
?>
138169
```
139170
### Template anything
140171
Yes ***ANYTHING***, any variable exposed by a linked container or the **-e** flag lets you template your config files. This means you can add redis, mariaDB, memcache or anything you want to your application very easily.

nginx-site.conf

+1-16
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,9 @@ server {
1818
}
1919

2020
location /public {
21-
root /usr/share/nginx/html/public;
21+
root /usr/share/nginx/html;
2222
}
2323

24-
#error_page 404 /404.html;
25-
26-
# redirect server error pages to the static page /50x.html
27-
#
28-
error_page 500 502 503 504 /50x.html;
29-
location = /50x.html {
30-
root /usr/share/nginx/html;
31-
}
32-
33-
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
34-
access_log off;
35-
log_not_found off;
36-
expires 5d;
37-
}
38-
3924
# deny access to . files, for security
4025
#
4126
location ~ /\. {

package.json

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
{
22
"name": "node-server-example",
3+
"description": "Hello World App",
34
"version": "0.0.1",
4-
"description": "Hello World Example served by express",
5-
"main": "server.js",
5+
"private": true,
66
"dependencies": {
7-
"express": "*"
8-
},
9-
"devDependencies": {},
10-
"scripts": {
11-
"test": "echo \"Error: no test specified\" && exit 1",
12-
"start": "node server.js"
13-
},
14-
"repository": "",
15-
"author": "Richard Harvey",
16-
"license": "AGPL"
7+
"express": "4.x"
8+
}
179
}

supervisord.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ directory=/usr/share/nginx/html/
2626
autostart=true
2727
autorestart=true
2828
startretries=3
29-
stderr_logfile=/var/log/webhook/node.err.log
30-
stdout_logfile=/var/log/webhook/node.out.log
29+
stderr_logfile=/var/log/node/node.err.log
30+
stdout_logfile=/var/log/node/node.out.log
3131
user=nginx
3232

3333
[program:nginx]

0 commit comments

Comments
 (0)