Skip to content

Instantly share code, notes, and snippets.



Last active May 4, 2020
What would you like to do?
Docker Setup for Server Host, DB Instances, and NodeJS App

Docker Host Server Setup, w/ Basic Monitoring Tools

# Updates
apt-get update && apt-get install -y vim-nox git-core ufw curl atop htop build-essential libssl-dev linux-image-amd64 linux-headers-amd64

# Updates Profile init scripts
cd ~/
curl -sSL > .bash_aliases_new
cat .bash_aliases_new >> .bash_aliases
# Read into current shell (login steps already missed the aliases file)
source .bash_aliases

# Install Docker
curl -sSL | sh

# Create Shared Folder On HOST for the Docker DB Instances
mkdir -p /mongodb/db
mkdir -p /elastic


Only for SELinux Enabled Systems

# SELinux fixes (optional)
# chcon -Rt svirt_sandbox_file_t /mongodb
# chcon -Rt svirt_sandbox_file_t /elastic


Setup Database Services

MongoDB v3 Server

mkdir -p /mongodb/db
docker run -p --name mongo -v /mongodb:/data -d mongo:3 bash -c 'mongod --bind_ip --logpath /data/mongodb.log --logappend --dbpath /data/db --storageEngine=wiredTiger'

Elastic Search

mkdir -p /elastic
echo "path:" > /elastic/elasticsearch.yml
echo "  logs: /data/elastic-logs" >> /elastic/elasticsearch.yml
echo "  data: /data/elastic-data" >> /elastic/elasticsearch.yml
docker run --name elastic -d -p 9200:9200 -p 9300:9300 -v /elastic:/data elasticsearch bash -c 'elasticsearch -Des.config=/data/elasticsearch.yml'

Package up your NodeJS/Ruby/Python/Etc App

  1. Add a blank file named Dockerfile in your project root.
  2. (Optional) Add a .dockerignore using .gitignore rules to exclude large non-essential paths. By default all project files are included.


# Example for NodeJS
FROM node:0.12
EXPOSE [3000]
COPY . /app/
RUN apt-get update \
	&& apt-get dist-upgrade -y
RUN ["npm", "install"]
# Overridable Command
CMD ["npm", "start"]

It's easier to show how to start using the dockerfile and inspect the result via console.

In terminal, cd to your project folder and run the following docker build command everytime you make a change or want to include OS upgrades)

Build Docker Image Every Deploy/Change

docker build -t app-name-here .


Create/Run Web App w/ Links to DB Servers

docker run -d --name webapp01 --link mongo:mongo --link elastic:elastic app-name-here

OR - Run Interactively

docker run -it --name webapp01 --link mongo:mongo --link elastic:elastic app-name-here


To re-run webapp01 IMPORTANT:

docker rm -f webapp01
# rerun `docker run...` from ^^^

To Re-Create the DB 'Container' Instances

Note: Data is mounted to host server at /mongodb

docker rm -f mongo elastic
# OR
docker rm -f elastic
docker rm -f mongo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.