Skip to content

Instantly share code, notes, and snippets.

@bluefangs
Last active March 6, 2018 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluefangs/b02424362345d27096a19f8ed220d7cd to your computer and use it in GitHub Desktop.
Save bluefangs/b02424362345d27096a19f8ed220d7cd to your computer and use it in GitHub Desktop.
Docker for nodejs and mongo
-----------------------------------Install docker
$ sudo apt-get update
$ sudo apt-get install docker.io
-----------------------------------Add user to docker (to avoid using sudo)
https://askubuntu.com/a/477554/642604
$ sudo groupadd docker
$ sudo gpasswd -a $USER docker
$ newgrp docker
-----------------------------------Install docker-compose
$ sudo curl -L https://github.com/docker/compose/releases/download/1.19.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
-----------------------------------download reqd images (mongo and alpine-node)
$ docker pull mongo
$ docker pull node:8.9.4-alpine
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mongo latest 43099507792a 5 days ago 366 MB
node 8.9.4-alpine 406f227b21f5 9 days ago 68.1 MB
----------------------------------- this is the dockerfile
# deploy/Dockerfile
# Dockerfile for your app.
FROM node:8.9.4-alpine
# you are now root.
#Add the current working directory to docker's /home/
ADD . /home
WORKDIR /home/programs/server
# since we are using alpine-node, we need the below compilers to compile native modules
RUN apk add --no-cache make gcc g++ python \
# Install NPM packages
&& npm install \
# Install forever.js
&& npm install -g forever
# Set up a normal user and be that user since
# it's not advisable to be root.
# https://medium.com/@mccode/processes-in-containers-should-not-run-as-root-2feae3f0df3b
# but this images does not contain groupadd
# so use addgroup / adduser instead ==> https://github.com/mhart/alpine-node/issues/48
RUN addgroup -S appuser && adduser -S -G appuser appuser
USER appuser
# You are now appuser
# Set environment variables
WORKDIR /home
# ENV PORT 3000
#ENV ROOT_URL http://127.0.0.1
#ENV MONGO_URL mongodb://mongodb_service:27017/mydb
# Expose port 3000
#EXPOSE 3000
# Start the app
CMD ["forever", "--minUptime", "1000", "--spinSleepTime", "1000", "./main.js"]
#CMD node ./main.js
--------------------------------------docker-composer.yml
version: '3'
services:
myapp1:
build: ~/experimental/app1
container_name: "app1_container"
image: app1_img
#dockerfile: ~/experimental/app1/dockerfile
environment:
- NODE_ENV=development
- PORT=3000
- ROOT_URL=http://127.0.0.1
- MONGO_URL=mongodb://mongodb_service:27017/mydb
# volumes:
# - .:/var/www:rw
# - /tmp/.X11-unix:/tmp/.X11-unix:rw
ports:
- 3000:3000
# - 9000:9000
restart: always #restarts on reboot
links:
- mongodb
depends_on:
- mongodb
myapp2:
build: ~/experimental/app2
container_name: "app2_container"
image: app2_img
#dockerfile: ~/experimental/app2/dockerfile
environment:
- NODE_ENV=development
- FOO=bar
- PORT=4000
- ROOT_URL=http://127.0.0.1
- MONGO_URL=mongodb://mongodb_service:27017/mydb
# volumes:
# - .:/var/www:rw
# - /tmp/.X11-unix:/tmp/.X11-unix:rw
ports:
- 4000:4000
# - 9000:9000
restart: always #restarts on reboot
links:
- mongodb
depends_on:
- mongodb
mongodb:
image: mongo:latest
container_name: "mongodb_service"
environment:
- MONGO_DATA_DIR=/var/lib/mongodb
- MONGO_LOG_DIR=/var/lib/mongodb
volumes:
- ./data/db:/data/db #map ./data/db of host to /data/db of container
restart: always #restarts on reboot
ports:
- 27017:27017
# command: mongod --smallfiles --logpath=/dev/null # --quiet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment