Skip to content

Instantly share code, notes, and snippets.

@justsml
Last active May 4, 2020 17:25
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justsml/9db4301ea7c85b7c9f7c to your computer and use it in GitHub Desktop.
Save justsml/9db4301ea7c85b7c9f7c to your computer and use it in GitHub Desktop.
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 https://gist.githubusercontent.com/justsml/b667f158731fd054cd38/raw/5778dbb5d3d138ccf99ae1bf973457ce89661362/.bash_aliases > .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 https://get.docker.com/ | 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 127.0.0.1:27017:27017 --name mongo -v /mongodb:/data -d mongo:3 bash -c 'mongod --bind_ip 0.0.0.0 --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.

Dockerfile

# Example for NodeJS
FROM node:0.12
EXPOSE [3000]
COPY . /app/
WORKDIR /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