Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
docker run -it --name node bitnami/node
node:
image: bitnami/node
command: node script.js
volumes:
- /path/to/node/app:/app
The recommended way to get the Bitnami Node.js Docker Image is to pull the prebuilt image from the Docker Hub Registry.
docker pull bitnami/node:latest
To use a specific version, you can pull a versioned tag. You can view the list of available versions in the Docker Hub Registry.
docker pull bitnami/node:[TAG]
If you wish, you can also build the image yourself.
git clone https://github.com/bitnami/bitnami-docker-node.git
cd bitnami-docker-node
docker build -t bitnami/node .
By default, running this image will drop you into the Node.js REPL, where you can interactively test and try things out in Node.js.
docker run -it --name node bitnami/node
Further Reading:
The default work directory for the Node.js image is /app
. You can mount a folder from your host
here that includes your Node.js script, and run it normally using the node
command.
docker run -it --name node -v /path/to/node/app:/app bitnami/node \
node script.js
If your Node.js app has a package.json
defining your app's dependencies and start script, you can
install the dependencies before running your app.
docker run --rm -v /path/to/node/app:/app bitnami/node npm install
docker run -it --name node -v /path/to/node/app:/app bitnami/node npm start
or using Docker Compose:
node:
image: bitnami/node
command: "sh -c 'npm install && npm start'"
Further Reading:
This image exposes port 3000
in the container, so you should ensure that your web server is
binding to port 3000
, as well as accepting remote connections.
Below is an example of an express.js app listening to remote connections on
port 3000
:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var server = app.listen(3000, '0.0.0.0', function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
To access your web server from your host machine you can ask Docker to map a random port on your
host to port 3000
inside the container.
docker run -it --name node -P bitnami/node
Run docker port
to determine the random port Docker assigned.
$ docker port node
3000/tcp -> 0.0.0.0:32769
You can also manually specify the port you want forwarded from your host to the container.
docker run -it --name node -p 8080:3000 bitnami/node
Access your web server in the browser by navigating to http://localhost:8080.
If you want to connect to your Node.js web server inside another container, you can use the linking system provided by Docker.
We may want to make our Node.js web server only accessible via an nginx web server. Doing so will allow us to setup more complex configuration, serve static assets using nginx, load balance to different Node.js instances, etc.
Let's create an nginx virtual host to reverse proxy to our Node.js container. The Bitnami nginx Docker Image ships with some example virtual hosts for connecting to Bitnami runtime images. We will make use of the Node.js example:
server {
listen 0.0.0.0:80;
server_name yourapp.com;
access_log /logs/yourapp_access.log;
error_log /logs/yourapp_error.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
# proxy_pass http://[your_node_container_link_alias]:3000;
proxy_pass http://yourapp:3000;
proxy_redirect off;
}
}
Notice we've substituted the link alias name yourapp
, we will use the same name when creating the
link.
Copy the virtual host above, saving the file somewhere on your host. We will mount it as a volume in our nginx container.
Docker's linking system uses container ids or names to reference containers. We can explicitly specify a name for our Node.js server to make it easier to connect to other containers.
docker run -it --name node -v /path/to/node/app:/app bitnami/node npm start
or using Docker Compose:
node:
image: bitnami/node
command: npm start
volumes:
- /path/to/node/app:/app
Now that we have our Node.js server running, we can create another container that links to it by
giving Docker the --link
option. This option takes the id or name of the container we want to link
it to as well as a hostname to use inside the container, separated by a colon. For example, to have
our Node.js server accessible in another container with yourapp
as it's hostname we would pass
--link node:yourapp
to the Docker run command.
docker run -it -v /path/to/vhost.conf:/bitnami/nginx/conf/vhosts/yourapp.conf \
--link node:yourapp \
bitnami/nginx
or using Docker Compose:
nginx:
image: bitnami/nginx
links:
- node:yourapp
volumes:
- /path/to/vhost.conf:/bintami/nginx/conf/yourapp.conf
We started the nginx server, mounting the virtual host we created in
Step 1, and created a link to the Node.js server with the alias
yourapp
.
Bitnami provides up-to-date versions of Node.js, including security patches, soon after they are made upstream. We recommend that you follow these steps to upgrade your container.
docker pull bitnami/node:latest
or if you're using Docker Compose, update the value of the image property to
bitnami/node:latest
.
docker rm -v node
or using Docker Compose:
docker-compose rm -v node
Re-create your container from the new image.
docker run --name node bitnami/node:latest
or using Docker Compose:
docker-compose start node
This image is tested for expected runtime behavior, using the
Bats testing framework. You can run the tests on your machine
using the bats
command.
bats test.sh
We'd love for you to contribute to this Docker image. You can request new features by creating an issue, or submit a pull request with your contribution.
If you encountered a problem running this container, you can file an issue. For us to provide better support, be sure to include the following information in your issue:
- Host OS and version
- Docker version (
docker version
) - Output of
docker info
- Version of this container (
echo $BITNAMI_APP_VERSION
inside the container) - The command you used to run the container, and any relevant output you saw (masking any sensitive information)
Copyright 2015 Bitnami
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.