Skip to content

Instantly share code, notes, and snippets.

@remarkablemark
Last active January 24, 2023 23:20
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save remarkablemark/a877f2d78bfac0e9aca003c99df137ee to your computer and use it in GitHub Desktop.
Save remarkablemark/a877f2d78bfac0e9aca003c99df137ee to your computer and use it in GitHub Desktop.
Docker Node.js Example
node_modules/
*.log

Docker Node.js Example

Run:

docker-compose up

See logs:

docker logs <name>

Enter container terminal:

docker exec -it <name> /bin/bash

Open app:

open "http://$(docker-machine ip):8000/"
# curl -i "$(docker-machine ip):8000/"

Stop:

docker-compose down
web:
build: .
volumes:
- .:/usr/src/app/
ports:
- "8000:3000"
links:
- redis
redis:
image: redis
# install latest node
# https://hub.docker.com/_/node/
FROM node:latest
# create and set app directory
RUN mkdir -p /usr/src/app/
WORKDIR /usr/src/app/
# install app dependencies
# this is done before the following COPY command to take advantage of layer caching
COPY package.json . # remember the working directory is `/usr/src/app/`
RUN npm install
# copy app source to destination container
COPY . .
# expose container port
EXPOSE 3000
CMD npm start
{
"name": "docker-node-example",
"version": "0.0.0",
"description": "An example of a Dockerized Node.js web app.",
"author": "Mark <mark@remarkablemark.org>",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"keywords": [
"docker node"
],
"dependencies": {
"express": "^4.13.4",
"redis": "^2.6.2"
},
"devDependencies": {},
"license": "MIT"
}
'use strict';
const express = require('express');
const app = express();
const redis = require('redis');
const client = redis.createClient({
host: 'redis',
port: 6379
});
app.get('/', (req, res) => {
client.incr('hits');
client.get('hits', (err, hit) => {
if (err) throw err;
res.send(`Hits: ${hit}`);
});
});
const port = 3000;
app.listen(port, () => {
console.log(`Running on http://localhost:${port}`);
});
@remarkablemark
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment