Skip to content

Instantly share code, notes, and snippets.

@neuni
Last active January 21, 2019 09:52
Show Gist options
  • Save neuni/dcde3df5f984d5b12e5406f5c9e536b1 to your computer and use it in GitHub Desktop.
Save neuni/dcde3df5f984d5b12e5406f5c9e536b1 to your computer and use it in GitHub Desktop.
Mac shell script to run symfony 4 projects in docker containers. For good developer performance, only project files are mounted and vendor files are copied back to the host on composer updates.
#!/bin/bash
#################
# RUNTIME CONFIG:
PORT=8888
#################
IMAGE=kempkensteffen/nginx-symfony:7.2-dev
DOCROOT=/var/www/html
SHELL=/bin/zsh
NAME=`php -r 'echo basename(__DIR__)."-".md5(file_get_contents(__DIR__));'`
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
SCRIPTNAME=`basename "$0"`
function run()
{
rm -rf /tmp/$NAME-composer-updated
touch /tmp/$NAME-composer-updated
docker pull $IMAGE
docker run -d -p $PORT:80 --name $NAME \
-v $DIR/bin:$DOCROOT/bin \
-v $DIR/config:$DOCROOT/config \
-v $DIR/public:$DOCROOT/public \
-v $DIR/src:$DOCROOT/src \
-v $DIR/var:$DOCROOT/var \
-v $DIR/.env:$DOCROOT/.env \
-v $DIR/composer.json:$DOCROOT/composer.json \
-v $DIR/composer.lock:$DOCROOT/composer.lock \
-v $DIR/symfony.lock:$DOCROOT/symfony.lock \
-v /tmp/$NAME-composer-updated:/composer-updated \
$IMAGE
}
function start()
{
docker start $NAME
}
function stop()
{
docker stop $NAME
}
function remove()
{
stop
docker rm $NAME
}
function restart()
{
docker restart $NAME
}
function logs()
{
docker logs -f $NAME
}
function console()
{
if [ ! -f "/usr/local/bin/fswatch" ]; then
echo "Please install fswatch to use the console:"
echo "brew install fswatch"
exit 1;
fi
# Watch for changes of composer files
trap "kill 0" EXIT
/usr/local/bin/fswatch -0 /tmp/$NAME-composer-updated | xargs -0 -n 1 -I {} $DIR/$SCRIPTNAME copyVendor &
# Run console
docker exec -it -e SHELL=$SHELL $NAME /bin/sh -c 'PATH=/opt/docker-shell:$PATH; exec "${SHELL:-sh}"'
}
function copyVendor() {
printf "\n\r"
echo "👻 Copying vendor files from container to host..."
printf "\r"
rm -rf $DIR/vendor
docker cp $NAME:/var/www/html/vendor $DIR/vendor
echo "👻 Done. Vendor files are updated."
printf "\n\r"
}
function setupConsole() {
COMPOSER=$(cat <<EOF
#!/bin/bash
/usr/bin/composer "\$@"
if [ \$? -ne 0 ]; then exit; fi
echo "1" > /composer-updated
EOF
)
docker exec -e SHELL_COMPOSER="$COMPOSER" $NAME /bin/sh -c 'mkdir -p /opt/docker-shell; echo "$SHELL_COMPOSER" > /opt/docker-shell/composer; chmod a+x /opt/docker-shell/composer'
}
if [[ "$1" == "run" ]]; then
RUNNING=$(docker ps -a --format "table {{.Names}}" | grep -c "$NAME")
if [[ "$RUNNING" == "0" ]]; then
run
setupConsole
else
start
fi
if [ $? -ne 0 ]; then exit; fi
console
exit 0;
fi
if [[ "$1" == "stop" ]]; then
stop
exit 0;
fi
if [[ "$1" == "remove" ]]; then
remove
exit 0;
fi
if [[ "$1" == "restart" ]]; then
restart
exit 0;
fi
if [[ "$1" == "console" ]]; then
console
exit 0;
fi
if [[ "$1" == "copyVendor" ]]; then
copyVendor
exit 0;
fi
if [[ "$1" == "logs" ]]; then
logs
exit 0;
fi
echo "./$SCRIPTNAME run Runs the docker image and opens log & console"
echo "./$SCRIPTNAME console Opens log & console"
echo "./$SCRIPTNAME stop Stops the docker container"
echo "./$SCRIPTNAME restart Restarts the docker container"
echo "./$SCRIPTNAME remove Removes the docker container"
echo "./$SCRIPTNAME logs Shows the container logs"
echo "./$SCRIPTNAME copyVendor Copy the vendor folder from container to host"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment