Node.js is a runtime environment built on V8 JavaScript engine. Its event-driven, non-blocking I/O model enables the development of fast, scalable, and data-intensive server applications.
Trademarks: The respective trademarks mentioned in this document are owned by the respective companies, and use of them does not imply any affiliation or endorsement.
The recommended way to get the Node.js Docker Image is to pull the prebuilt image from the AWS Public ECR Gallery or from the GitHub Container Registry.
$ docker pull ghcr.io/bitcompat/node:latestTo use a specific version, you can pull a versioned tag. You can view the list of available versions in the GitHub Registry or the available tags in the public ECR gallery.
$ docker pull ghcr.io/bitcompat/node:[TAG]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 ghcr.io/bitcompat/nodeFurther 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/app:/app ghcr.io/bitcompat/node \
node script.jsIf 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/app:/app ghcr.io/bitcompat/node npm install
$ docker run -it --name node -v /path/to/app:/app ghcr.io/bitcompat/node npm startFurther Reading:
To work with npm private modules, it is necessary to be logged into npm. npm CLI uses auth tokens for authentication. Check the official npm documentation for further information about how to obtain the token.
If you are working in a Docker environment, you can inject the token at build time in your Dockerfile by using the ARG parameter as follows:
- Create a
npmrcfile within the project. It contains the instructions for thenpmcommand to authenticate against npmjs.org registry. TheNPM_TOKENwill be taken at build time. The file should look like this:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}- Add some new lines to the Dockerfile in order to copy the
npmrcfile, add the expectedNPM_TOKENby using the ARG parameter, and remove thenpmrcfile once the npm install is completed.
You can find the Dockerfile below:
FROM ghcr.io/bitcompat/node
ARG NPM_TOKEN
COPY npmrc /root/.npmrc
COPY . /app
WORKDIR /app
RUN npm install
CMD node app.js- Now you can build the image using the above Dockerfile and the token. Run the
docker buildcommand as follows:
$ docker build --build-arg NPM_TOKEN=${NPM_TOKEN} .| NOTE: The "." at the end gives docker build the current directory as an argument.
Congratulations! You are now logged into the npm repo.
Further reading
By default the image exposes the port 3000 of the container. You can use this port for your Node.js application server.
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 -v /path/to/app:/app -P ghcr.io/bitcompat/node node index.jsRun docker port to determine the random port Docker assigned.
$ docker port node
3000/tcp -> 0.0.0.0:32769You can also specify the port you want forwarded from your host to the container.
$ docker run -it --name node -p 8080:3000 -v /path/to/app:/app ghcr.io/bitcompat/node node index.jsAccess 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 docker networking to create a network and attach all the containers to that network.
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.
$ docker network create app-tier --driver bridgeLet's create an nginx virtual host to reverse proxy to our Node.js container.
server {
listen 0.0.0.0:80;
server_name yourapp.com;
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://myapp:3000;
proxy_redirect off;
}
}Notice we've substituted the link alias name myapp, we will use the same name when creating the container.
Copy the virtual host above, saving the file somewhere on your host. We will mount it as a volume in our nginx container.
$ docker run -it --name myapp --network app-tier \
-v /path/to/app:/app \
ghcr.io/bitcompat/node node index.js$ docker run -it \
-v /path/to/vhost.conf:/bitnami/nginx/conf/vhosts/yourapp.conf:ro \
--network app-tier \
ghcr.io/bitcompat/nginxUp-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 ghcr.io/bitcompat/node:latestor if you're using Docker Compose, update the value of the image property to ghcr.io/bitcompat/node:latest.
$ docker rm -v nodeor using Docker Compose:
$ docker-compose rm -v nodeRe-create your container from the new image.
$ docker run --name node ghcr.io/bitcompat/node:latestor using Docker Compose:
$ docker-compose up nodeWe'd love for you to contribute to this container. 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.
This package is released under MIT license.
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.