Skip to content

Instantly share code, notes, and snippets.

@anandgorantala
Last active January 23, 2020 21:39
Show Gist options
  • Save anandgorantala/d12525aaa046efa7a6c3 to your computer and use it in GitHub Desktop.
Save anandgorantala/d12525aaa046efa7a6c3 to your computer and use it in GitHub Desktop.
Docker setup for nodejs microservices with nginx and rethinkdb
# This shows the setup for two services, an accounts service which connects to a database and a pagerduty service which connects to the pagerduty api
# Directory structure
accounts/
|_ Dockerfile
pagerduty/
|_ Dockerfile
nginx/
|_ conf/
|_ sites.conf
|_ .htpasswd
|_ Dockerfile
docker-compose.yml
# You can start your service with
PAGERDUTY_TOKEN={TOKEN} docker-compose up -d
# Stop services
docker-compose stop
# Rebuild service(s)
docker-compose build
docker-compose build accounts
web:
build: nginx/
ports:
- "80:80"
links:
- pagerduty
- accounts
- db
pagerduty:
build: pagerduty/
environment:
- PAGERDUTY_TOKEN
accounts:
build: accounts/
links:
- db
db:
image: rethinkdb
volumes_from:
- dbData
# It is recommended to use a separate container for data volumes.
# This provides some benefits, for e.g., the db container can be upgraded without affecting the data
dbData:
image: busybox
volumes:
- /data
command: echo Data Volume for db
FROM nginx
ADD conf/sites.conf /etc/nginx/conf.d/sites.conf
ADD .htpasswd /etc/nginx/.htpasswd
# For htpasswd generation help, follow this tutorial
# https://www.digitalocean.com/community/tutorials/how-to-set-up-http-authentication-with-nginx-on-ubuntu-12-10
server {
listen 80;
server_name accounts.example.com;
location / {
proxy_pass http://accounts:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
server {
listen 80;
server_name pagerduty.example.com;
location / {
proxy_pass http://pagerduty:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
server {
listen 80;
server_name db.example.com;
location / {
proxy_pass http://db:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}
FROM dockerfile/nodejs
# Bundle app source
COPY . /repo
WORKDIR /repo
# Install app dependencies
RUN npm install
# Build the web app
RUN npm run build
ENV PORT 3000
ENV NODE_ENV production
EXPOSE 3000
CMD ["npm", "start"]
FROM iojs
ADD . /repo
WORKDIR /repo
RUN npm install
ENV PORT 4000
ENV NODE_ENV production
EXPOSE 4000
CMD iojs bin/www
This can be extended to add a lot more services. It is a great setup to start out with and can be improved very easily as your service matures.
Instead of building from folders, you would build your images from your github repositories directly using docker hub and your docker-compose.yml would just contain all images. This will allow you to version them and make it easier to revert your setup.
There are a few more things to do here
- Adding ssl support in nginx
- backing up the database volume
I will add them separately to keep this simple.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment