Skip to content

Instantly share code, notes, and snippets.

@so0k
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save so0k/b73248e7e42a0e4fd564 to your computer and use it in GitHub Desktop.
Save so0k/b73248e7e42a0e4fd564 to your computer and use it in GitHub Desktop.
demoapp Docker environment - python & nodejs command line interface containers
#!/bin/bash
#this is old, do not use, use docker-compose instead
#demoapp env management script
#based on andreagrandi/glowmachine sample
#this script assumes it is running in the project folder
#under the data directory created by this boot2docker setup script:
#https://gist.github.com/so0k/d4f5e485a81ca5768643
proj="demoapp"
case "$1" in
start)
# Start PostgreSQL
#echo "Starting PosgreSQL container..."
#docker run --name postgres -d -P --volumes-from postgresdata so0k/$proj:postgresql
# Start web stack
echo "Starting uWSGI container..."
#back end container (uWSGI django REST framework) --link postgres:postgres
docker run -p 8000:8000 -d --name backend --volumes-from $proj-backend-data so0k/$proj:backend
echo "Starting nginx container..."
#nginx container serving HTML and proxy to backend
docker run -p 80:80 -d --name frontend --link backend:backend --volumes-from $proj-frontend-data so0k/$proj:frontend
;;
stop)
echo "Stopping front end container..."
docker stop frontend
echo "Removing front end container..."
docker rm frontend
echo "Stopping back end container..."
docker stop backend
echo "Removing back end container..."
docker rm backend
#echo "Stopping PostgreSQL container..."
#docker stop postgres
#echo "Stopping PostgreSQL container..."
#docker rm postgres
;;
setup)
echo "Setting up $proj data containers.. using `pwd`/volumes "
#enable user www-data from python image to work with the db data volume
sudo chown -R 33:33 `pwd`/volumes/db
docker run --name $proj-backend-data -v `pwd`/volumes/db:/etc/data/ busybox true
#currently empty
docker run -d --name $proj-frontend-data busybox true
;;
teardown)
echo "removing $proj data containers"
docker rm $proj-backend-data
docker rm $proj-frontend-data
;;
cleanup)
echo "Cleaning up images without a tag"
docker images --no-trunc| grep none | awk '{print $3}' | xargs -r docker rmi
;;
backendcli)
#runs the back end command line interface (python v3.4, pip, django manage.py, db shell, ...)
#optional parameter $2 = name for backend container
local p1
if [ -z "$2" ]
then
p1="backendcli"
else
p1="$2"
fi
echo "running '$p1' cli using `pwd`/backend/src"
docker run -p 8080:8080 --name $p1 -v `pwd`/backend/pip-cache:/usr/src/pip-cache -v `pwd`/backend/src:/usr/src/app --volumes-from $proj-backend-data -it --rm so0k/$proj:backendcli bash
;;
frontendcli)
#runs the front end command line interface (node, npm, yo, bower, grunt, ..) linked to the backend dev container
echo "starting backendcli with ./manage.py runserver 0.0.0.0:8080"
docker run -p 8080:8080 --name backendcli -v `pwd`/backend/pip-cache:/usr/src/pip-cache -v `pwd`/backend/src:/usr/src/app --volumes-from $proj-backend-data -d so0k/$proj:backendcli ./manage.py runserver 0.0.0.0:8080
echo "running frontend cli using `pwd`/frontend with link to backendcli:backend"
echo "proxy api calls to server 'backend' from grunt"
#enable yeoman user ownership within frontend container
sudo chown -R 2000:2000 `pwd`/frontend
docker run -p 9000:9000 -p 35729:35729 \
--name frontendcli -v `pwd`/frontend/src:/usr/src/ --link backendcli:backend -it --rm so0k/$proj:frontendcli bash
#when finished, clean up
read -p "Stop/Clean up backendcli? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY = y ]]
then
docker stop backendcli && docker rm backendcli
else
echo "backendcli still running, use $0 enter backendcli to get a shell"
echo #move to a new line
echo "you will have to run 'docker stop backendcli && docker rm backendcli' by yourself."
fi
;;
build)
#docker build --rm=true -t "so0k/$proj:postgresql" postgresql/
if [ -z "$2" ]
then
echo "specify build target"
echo "Usage: $0 build <prod|cli>"
else
case "$2" in
prod)
echo "building backend prod"
sudo cp backend/Dockerfile.prod backend/Dockerfile
docker build --rm=true -t "so0k/$proj:backend" backend/
sudo rm backend/Dockerfile
echo "building frontend prod"
sudo cp frontend/Dockerfile.prod frontend/Dockerfile
docker build --rm=true -t "so0k/$proj:frontend" frontend/
sudo rm frontend/Dockerfile
;;
cli)
echo "building backend cli"
sudo cp backend/Dockerfile.cli backend/Dockerfile
docker build --rm=true -t "so0k/$proj:backendcli" backend/
sudo rm ./backend/Dockerfile
echo "building frontend cli"
sudo cp frontend/Dockerfile.cli frontend/Dockerfile
docker build --rm=true -t "so0k/$proj:frontendcli" frontend/
sudo rm frontend/Dockerfile
;;
esac
fi
;;
enter)
#enter a container
if [ -z "$2" ]
then
echo "no container supplied"
echo "Usage: $0 enter <container>"
else
sudo /var/lib/boot2docker/docker-enter $2
fi
;;
*)
echo $"Usage: $0 {setup|teardown|build <prod|cli>|cleanup|backendcli [name]|frontendcli|start|stop|enter <container>}"
esac
@so0k
Copy link
Author

so0k commented Jul 13, 2015

this is script has been completely replaced by docker-compose

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment