diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 0000000000..32f4b13fab --- /dev/null +++ b/docker/README.md @@ -0,0 +1,85 @@ +# Docker Folder + +--- + +## Table of Contents + +[Docker Commands](#docker-commands) + +[Tools](#tools) + +[Sandbox](#sandbox) + +--- + +## Quickstart + +The top level du.sh file is a Docker utilities script. The following command will generate the two top level node images and create the two node_modules folders in the chapter and chapter/client directories. These operations are time consuming (~15 minutes), which is why they're done early and seperately from the other operations. Once done they shouldn't need to be repeated. The dependent images can be rebuilt from these base images relatively quickly. And the node_modules directories should only need to be recreated if the package.json files are changed. + +```bash +./docker/docker.sh run + +``` + +The docker.sh script in the docker/projects/working directory builds the db, app, and client images, creates containers from them, and starts the containers. Use the following command: + +```bash +./docker/projects/working/docker.sh run +``` + + + +--- + +## Docker commands + +View all containers +```bash +docker ps -a + +``` +Stop and remove all containers +```bash +docker stop $(docker ps -aq) && docker rm $(docker ps -aq) + +``` + +View all images +```bash +docker images + +``` + +Remove all images (use this judiciously, it can take a long time to rebuild images) +```bash +docker rmi -f $(docker images -a -q) + +``` + +Command line access to a container +```bash +docker exec -it chapter_app_1 /bin/sh + +``` + +--- + +## Tools + +```bash +/usr/bin/open -a /System/Library/CoreServices/Finder.app . + +``` + +--- + +## Sandbox + + +```bash +ls -al +pwd +more stuff + +``` +--- diff --git a/docker/docker-node/Dockerfile b/docker/docker-node/Dockerfile new file mode 100644 index 0000000000..84e4ab39ed --- /dev/null +++ b/docker/docker-node/Dockerfile @@ -0,0 +1,10 @@ +FROM node:13-alpine + +# Setting working directory. All the path will be relative to WORKDIR +WORKDIR /usr/node + +# A wildcard is used to ensure both package.json AND package-lock.json are copied +# where available (npm@5+) +COPY ./package*.json ./ + +RUN npm install diff --git a/docker/du.sh b/docker/du.sh new file mode 100755 index 0000000000..21a420fcae --- /dev/null +++ b/docker/du.sh @@ -0,0 +1,119 @@ +#!/bin/sh +##################################################################################### +### +### Docker utilities +### +##################################################################################### + +REMOVE_ALL_IMAGES="remove_all_images" +REMOVE_ALL_CONTAINERS="remove_all_containers" +INSTALL_DEPENDENCIES="install_dependencies" +CREATE_NODE_IMAGES="create_node_images" +CREATE_AND_INSTALL="create_and_install" + +if [ "$#" -eq 0 ] || [ $1 = "-h" ] || [ $1 = "--help" ]; then + echo "Usage: ./du.sh [OPTIONS] COMMAND [arg...]" + echo " ./du.sh [ -h | --help ]" + echo "" + echo "Options:" + echo " -h, --help Prints usage." + echo "" + echo "Commands:" + echo " $REMOVE_ALL_IMAGES - Remove all images." + echo " $REMOVE_ALL_CONTAINERS - Remove all containers." + echo " $INSTALL_DEPENDENCIES - Install dependencies." + echo " $CREATE_NODE_IMAGES - Create Node images." + echo " $CREATE_AND_INSTALL - Create Node images and install dependencies." + exit +fi + +removeImages() { + docker rmi -f $(docker images -a -q) + docker images +} + +removeContainers() { + docker stop $(docker ps -aq) && docker rm $(docker ps -aq) + docker ps -a +} + +installDependencies() { + + if [ ! -d "$WORKING_DIR/node_modules" ]; then + echo "Directory node_modules in $WORKING_DIR does not exist." + echo "Installing dependencies via npm install from the chapter_node_app image." + echo "This will take around 10 minutes." + docker run -v ~/chapter:/usr/node chapter_node_app npm install + fi + + if [ ! -d "$WORKING_DIR/client/node_modules" ]; then + echo "Directory node_modules in $WORKING_DIR/client does not exist." + echo "Installing dependencies via npm install from the chapter_node_client image." + echo "This will take around 5 minutes." + docker run -v ~/chapter/client:/usr/node chapter_node_client npm install + fi + +} + +createNodeImages() { + + DIR=~/chapter + + cd $DIR + + cd docker + cd ./docker-node + echo "////////////////////////////////////////////////////" + echo "// Building chapter_node_app image ..." + echo "////////////////////////////////////////////////////" + + cp $DIR/package.json . + docker build -t chapter_node_app . + rm package.json + + echo "////////////////////////////////////////////////////" + echo "// Building chapter_node_client image ..." + echo "////////////////////////////////////////////////////" + + cp $DIR/client/package.json . + cp $DIR/client/package-lock.json . + docker build -t chapter_node_client . + rm package-lock.json + rm package.json + + cd $DIR + + docker images + +} + +if [ $1 = $REMOVE_ALL_IMAGES ]; then + echo "Removing images ..." + removeImages + exit +fi + +if [ $1 = $REMOVE_ALL_CONTAINERS ]; then + echo "Removing all containers ..." + removeContainers + exit +fi + +if [ $1 = $INSTALL_DEPENDENCIES ]; then + echo "Installing node_modules folders ..." + installDependencies + exit +fi + +if [ $1 = $CREATE_NODE_IMAGES ]; then + echo "Creating Node base images ..." + createNodeImages + exit +fi + +if [ $1 = $CREATE_AND_INSTALL ]; then + echo "Creating Node base images ..." + createNodeImages + installDependencies + exit +fi diff --git a/Dockerfile b/docker/projects/mapped_volumes/Dockerfile_Demo_App similarity index 82% rename from Dockerfile rename to docker/projects/mapped_volumes/Dockerfile_Demo_App index 1c71c317e4..277121795e 100644 --- a/Dockerfile +++ b/docker/projects/mapped_volumes/Dockerfile_Demo_App @@ -5,14 +5,13 @@ WORKDIR /usr/chapter/ # Install app dependencies # A wildcard is used to ensure both package.json AND package-lock.json are copied # where available (npm@5+) -COPY package*.json ./ +#COPY package*.json ./ -RUN npm install +#RUN npm install # Bundle app source -COPY . . +# COPY . . EXPOSE 5000 CMD [ "npm","run","dev" ] - diff --git a/client/Dockerfile b/docker/projects/mapped_volumes/Dockerfile_Demo_Client similarity index 75% rename from client/Dockerfile rename to docker/projects/mapped_volumes/Dockerfile_Demo_Client index 65aa3e58bc..5a4cff0b31 100644 --- a/client/Dockerfile +++ b/docker/projects/mapped_volumes/Dockerfile_Demo_Client @@ -6,16 +6,15 @@ WORKDIR /usr/chapter/client # Installing dependencies COPY package*.json ./ -RUN npm install +#RUN npm install # Copying source files -COPY . . +#COPY . . # Building app -RUN npm run build +# RUN npm run build EXPOSE 3000 # Running the app -CMD [ "npm", "run", "start" ] - +# CMD [ "npm", "run", "start" ] diff --git a/docker-compose.yml b/docker/projects/mapped_volumes/docker-compose_mapped_volumes.yml similarity index 100% rename from docker-compose.yml rename to docker/projects/mapped_volumes/docker-compose_mapped_volumes.yml diff --git a/docker/projects/mapped_volumes/docker.sh b/docker/projects/mapped_volumes/docker.sh new file mode 100755 index 0000000000..b06777d63d --- /dev/null +++ b/docker/projects/mapped_volumes/docker.sh @@ -0,0 +1,137 @@ +#!/bin/sh +##################################################################################### +### +### Build Docker images and create containers with mapped volumes +### +##################################################################################### + +WORKING_DIR=~/chapter + +IMAGE_NAMES=(chapter_node chapter_app chapter_node_app chapter_client chapter_node_client) + +CONTAINER_NAMES=(chapter_db_1 chapter_app_1 chapter_client_1) + + +export DB_USER=postgres +export DB_PASSWORD=password +export DB_NAME=chapter +export DB_URL=localhost +export IS_DOCKER=true + + +CLEAN="clean" +RUN="run" +STOP="stop" + +CHAPTER_APP="$(docker ps --all --quiet --filter=name=chapter_app_1)" +CHAPTER_CLIENT="$(docker ps --all --quiet --filter=name=chapter_client_1)" +POSTGRES="$(docker ps --all --quiet --filter=name=chapter_db_1)" + +if [ "$#" -eq 0 ] || [ $1 = "-h" ] || [ $1 = "--help" ]; then + echo "Usage: ./docker.sh [OPTIONS] COMMAND [arg...]" + echo " ./docker.sh [ -h | --help ]" + echo "" + echo "Options:" + echo " -h, --help Prints usage." + echo "" + echo "Commands:" + echo " $CLEAN - Stop and Remove Chapter containers." + echo " $RUN - Build and Run Chapter." + echo " $STOP - Stop Chapter." + exit +fi + +setDockerFiles() { + cd ~/chapter + cp ./docker/projects/mapped_volumes/docker-compose_mapped_volumes.yml ./docker-compose.yml + cp ./docker/projects/mapped_volumes/Dockerfile_Demo_App ./Dockerfile + cp ./docker/projects/mapped_volumes/Dockerfile_Demo_Client ./client/Dockerfile +} + +installDependencies() { + if [ ! -d "$WORKING_DIR/node_modules" ]; then + echo "Directory node_modules in $WORKING_DIR does not exist." + echo "Installing dependencies via npm install from the chapter_node_app image." + echo "This will take around 10 minutes." + docker run -v ~/chapter:/usr/node chapter_node_app npm install + fi + + if [ ! -d "$WORKING_DIR/client/node_modules" ]; then + echo "Directory node_modules in $WORKING_DIR/client does not exist." + echo "Installing dependencies via npm install from the chapter_node_client image." + echo "This will take around 5 minutes." + docker run -v ~/chapter/client:/usr/node chapter_node_client npm install + fi +} + +clean() { + stop_existing + remove_stopped_containers + remove_unused_volumes +} + +run() { + echo "Setting Docker files ..." + setDockerFiles + + echo "Cleaning..." + clean + + echo "Installing dependencies..." + installDependencies + + echo "Running docker..." + docker-compose up --build +} + +stop_existing() { + + if [ -n "$CHAPTER_APP" ]; then + docker stop $CHAPTER_APP + fi + + if [ -n "$CHAPTER_CLIENT" ]; then + docker stop $CHAPTER_CLIENT + fi + + if [ -n "$POSTGRES" ]; then + docker stop $POSTGRES + fi + +} + +remove_stopped_containers() { + CONTAINERS="$(docker ps -a -f status=exited -q)" + if [ ${#CONTAINERS} -gt 0 ]; then + echo "Removing all stopped containers." + docker rm $CONTAINERS + else + echo "There are no stopped containers to be removed." + fi +} + +remove_unused_volumes() { + CONTAINERS="$(docker volume ls -qf dangling=true)" + if [ ${#CONTAINERS} -gt 0 ]; then + echo "Removing all unused volumes." + docker volume rm $CONTAINERS + else + echo "There are no unused volumes to be removed." + fi +} + +if [ $1 = $CLEAN ]; then + echo "Cleaning..." + clean + exit +fi + +if [ $1 = $RUN ]; then + run + exit +fi + +if [ $1 = $STOP ]; then + stop_existing + exit +fi diff --git a/docker/scripts/build_node_app.sh b/docker/scripts/build_node_app.sh new file mode 100755 index 0000000000..525e300085 --- /dev/null +++ b/docker/scripts/build_node_app.sh @@ -0,0 +1,27 @@ +#!/bin/sh +##################################################################################### +### +### Run +### +##################################################################################### + +COMMAND_DESCRIPTION="Build the node base for the chapter app" + +WORKING_DIR=~/chapter + +if [ "$1" = "-i" ] || [ "$1" = "--info" ]; then + tabs 20 + me=`basename -s .sh "$0"` + echo " $me \t- $COMMAND_DESCRIPTION" + exit +fi + +echo "Building the node base for the chapter app" + +cd $WORKING_DIR/docker/docker-node + +cp $WORKING_DIR/package.json . + +docker build -t chapter_node_app . + +rm package*.json diff --git a/docker/scripts/demo_run.sh b/docker/scripts/demo_run.sh new file mode 100755 index 0000000000..14145a35f8 --- /dev/null +++ b/docker/scripts/demo_run.sh @@ -0,0 +1,50 @@ +#!/bin/sh +##################################################################################### +### +### Run +### +##################################################################################### + +COMMAND_DESCRIPTION="This is the demo script" + +WORKING_DIR=~/chapter + +if [ "$1" = "-i" ] || [ "$1" = "--info" ]; then + tabs 20 + me=`basename -s .sh "$0"` + echo " $me \t- $COMMAND_DESCRIPTION" + exit +fi + +echo "Running demo" + +cp $WORKING_DIR/docker/projects/demo/docker-compose.yml $WORKING_DIR + +cd $WORKING_DIR + +export DB_USER=postgres +export DB_PASSWORD=password +export DB_NAME=chapter +export DB_URL=localhost +export IS_DOCKER=false + +docker-compose up& + +WEBPAGE=\ +http://localhost:3000 + +GRAPHQL_PLAYGROUNG=\ +http://localhost:5000/graphql + +SAFARI=/Applications/Safari.app +CHROME="/Applications/Google Chrome.app" + +BROWSER=$CHROME + +sleep 5 + +/usr/bin/open -a "$BROWSER" $WEBPAGE + +sleep 3 + +/usr/bin/open -a "$BROWSER" $GRAPHQL_PLAYGROUNG diff --git a/docker/scripts/dev.sh b/docker/scripts/dev.sh new file mode 100755 index 0000000000..483e36c985 --- /dev/null +++ b/docker/scripts/dev.sh @@ -0,0 +1,56 @@ +#!/bin/sh +##################################################################################### +### +### Run +### +##################################################################################### + +COMMAND_DESCRIPTION="This is the run script" + +WORKING_DIR=~/chapter + +if [ "$1" = "-i" ] || [ "$1" = "--info" ]; then + tabs 20 + me=`basename -s .sh "$0"` + echo " $me \t- $COMMAND_DESCRIPTION" + exit +fi + +echo "Running" + +cp $WORKING_DIR/docker/projects/dev/docker-compose.yml $WORKING_DIR + +cd $WORKING_DIR + +export DB_USER=postgres +export DB_PASSWORD=password +export DB_NAME=chapter +export DB_URL=localhost +export IS_DOCKER=false + +if [ ! -d "$WORKING_DIR/node_modules" ]; then + echo "Directory node_modules in $WORKING_DIR does not exist." + echo "Installing dependencies via npm install from the node container." + echo "This will take around 10 minutes." + docker run -v ~/chapter:/usr/node chapter_node_app npm install +fi + +if [ ! -d "$WORKING_DIR/client/node_modules" ]; then + echo "Directory node_modules in $WORKING_DIR/client does not exist." + echo "Installing dependencies via npm install from the node container." + echo "This will take around 5 minutes." + docker run -v ~/chapter/client:/usr/node chapter_node_client npm install +fi + + +docker-compose up + +WEBPAGE=\ +http://localhost:3000 + +SAFARI=/Applications/Safari.app +CHROME="/Applications/Google Chrome.app" + +BROWSER=$CHROME + +#/usr/bin/open -a "$BROWSER" $WEBPAGE diff --git a/docker/scripts/run.sh b/docker/scripts/run.sh new file mode 100755 index 0000000000..e95be3ad22 --- /dev/null +++ b/docker/scripts/run.sh @@ -0,0 +1,42 @@ +#!/bin/sh +##################################################################################### +### +### Run +### +##################################################################################### + +COMMAND_DESCRIPTION="This is the run script" + +WORKING_DIR=~/chapter + +if [ "$1" = "-i" ] || [ "$1" = "--info" ]; then + tabs 20 + me=`basename -s .sh "$0"` + echo " $me \t- $COMMAND_DESCRIPTION" + exit +fi + +echo "Running" + +cp $WORKING_DIR/docker/projects/dev/docker-compose.yml $WORKING_DIR + +cd $WORKING_DIR + +export DB_USER=postgres +export DB_PASSWORD=password +export DB_NAME=chapter +export DB_URL=localhost +export IS_DOCKER=false + + +docker-compose up + +WEBPAGE=\ +http://localhost:3000 + +SAFARI=/Applications/Safari.app +CHROME="/Applications/Google Chrome.app" + +BROWSER=$CHROME + +/usr/bin/open -a "$BROWSER" $WEBPAGE diff --git a/docker/scripts/template.sh b/docker/scripts/template.sh new file mode 100755 index 0000000000..06125bf1f8 --- /dev/null +++ b/docker/scripts/template.sh @@ -0,0 +1,19 @@ +#!/bin/sh +##################################################################################### +### +### Run +### +##################################################################################### + +COMMAND_DESCRIPTION="This is the template script" + +WORKING_DIR=~/chapter + +if [ "$1" = "-i" ] || [ "$1" = "--info" ]; then + tabs 20 + me=`basename -s .sh "$0"` + echo " $me \t- $COMMAND_DESCRIPTION" + exit +fi + +echo "Running template script"