Skip to content

Instantly share code, notes, and snippets.

@jakzal
Last active March 31, 2020 08:29
Show Gist options
  • Save jakzal/f18bf2bfff7eda57eaee1ff323f23563 to your computer and use it in GitHub Desktop.
Save jakzal/f18bf2bfff7eda57eaee1ff323f23563 to your computer and use it in GitHub Desktop.
Docker with Symfony, RabbitMQ, Nginx
#!/bin/bash
set -e
set -u
set -o pipefail
COMMAND=${1:-"help"}
MACHINE_NAME=myapp
DENV=${DENV:-"dev"}
function is-prod() {
[ $DENV == "prod" ]
}
function get-tag() {
if is-prod; then
date +%Y%m%d%H%I
else
echo "dev"
fi
}
function symfony-env() {
if is-prod; then
echo "prod"
else
echo "dev"
fi
}
function symfony-debug() {
if is-prod; then
echo "0"
else
echo "1"
fi
}
export SYMFONY_ENV=`symfony-env`
export SYMFONY_DEBUG=`symfony-debug`
export TAG=`get-tag`
function compose-file() {
if is-prod; then
echo "docker-compose.prod.yml"
else
echo "docker-compose.yml"
fi
}
function create() {
if ! is-prod; then
echo "Running in a non-prod mode. Nothing to do."
return
fi
access_token=${1:-""}
if [ "" == "$access_token" ]; then
echo "Provide DIGITAL_OCEAN_ACCESS_TOKEN: $0 \$DIGITAL_OCEAN_ACCESS_TOKEN"
exit 1
fi
docker-machine create \
--driver digitalocean \
--digitalocean-access-token $access_token \
--digitalocean-region "lon1" \
--digitalocean-size "512mb" \
--digitalocean-image "ubuntu-16-04-x64" \
$MACHINE_NAME
}
function ip() {
if is-prod; then
docker-machine ip $MACHINE_NAME
else
echo 'localhost'
fi
}
function load-env() {
if is-prod; then
eval $(docker-machine env --shell bash $MACHINE_NAME)
fi
}
function build() {
load-env
docker build -t myapp/php:7.0-fpm ./docker/php
docker-compose -f `compose-file` build ${1:-}
}
function up() {
load-env
container=${1:-}
if [ -z $container ]; then
docker-compose -f `compose-file` up -d rabbitmq
sleep 10
fi
docker-compose -f `compose-file` up -d $container
docker-compose scale myapp_filter_auctions=5
if ! is-prod; then
composer install --prefer-dist
fi
}
function stop() {
load-env
docker-compose -f `compose-file` stop ${1:-}
}
function rm() {
load-env
docker-compose -f `compose-file` rm ${1:-}
}
function logs() {
load-env
docker-compose -f `compose-file` logs -f ${1:-}
}
function ps() {
load-env
docker-compose -f `compose-file` ps
}
function php() {
exec myapp php ${@:-"-v"}
}
function sh() {
exec myapp ${@:-"sh"}
}
function composer() {
exec myapp composer ${@:-""}
}
function exec() {
container=${1:-""}
command=${@:2}
if [ "" == "$container" ]; then
echo "Usage: $0 container_name [command]"
exit 1
fi
if [ "" == "$command" ]; then
command=sh
fi
load-env
docker-compose -f `compose-file` exec $container $command
}
function run() {
container=${1:-""}
command=${@:2}
if [ "" == "$container" ]; then
echo "Usage: $0 container_name [command]"
exit 1
fi
if [ "" == "$command" ]; then
command=sh
fi
load-env
docker-compose -f `compose-file` run --rm $container $command
}
function help() {
USAGE="$0 "$(compgen -A function | tr "\\n" "|" | sed 's/|$//')
echo $USAGE
}
if [ "$(type -t $COMMAND)" != "function" ]; then
help
exit 1
fi
$COMMAND ${@:2}
server {
listen 80;
server_name myapp.dev myapp;
root /acme/myapp/web;
index app.php;
location / {
try_files $uri $uri/ /app.php$is_args$args;
}
location ~ ^/(app|config)\.php(/|$) {
fastcgi_index app.php;
fastcgi_pass myapp:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SYMFONY_ENV dev;
fastcgi_param SYMFONY_DEBUG 1;
}
location ~ ^/.*\.php {
deny all;
return 404;
}
}
FROM nginx:latest
ADD ./prod/myapp.conf /etc/nginx/conf.d/myapp.conf
server {
listen 80;
server_name myapp.com myapp;
root /acme/myapp/web;
index app.php;
location / {
try_files $uri $uri/ /app.php$is_args$args;
}
location ~ ^/app\.php(/|$) {
fastcgi_index app.php;
fastcgi_pass myapp:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SYMFONY_ENV prod;
fastcgi_param SYMFONY_DEBUG 0;
}
location ~ ^/.*\.php {
deny all;
return 404;
}
}
FROM php:7.0-fpm-alpine
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN echo "date.timezone=UTC" >> $PHP_INI_DIR/php.ini
RUN docker-php-ext-install bcmath
RUN curl -Ls https://getcomposer.org/composer.phar > /usr/local/bin/composer && chmod +x /usr/local/bin/composer
VOLUME $PROJECT_DIR
version: '2'
services:
myapp:
build: myapp
image: "acme/myapp:${TAG}"
volumes:
- app-data:/acme/myapp/app/data
working_dir: /acme/myapp
restart: always
depends_on:
- rabbitmq
environment:
- SYMFONY_ENV
- SYMFONY_DEBUG
myapp_search:
build: myapp
image: "acme/myapp-search:${TAG}"
working_dir: /acme/myapp
command: bin/console rabbitmq:consumer -l128 --no-interaction search
restart: always
depends_on:
- rabbitmq
environment:
- SYMFONY_ENV
- SYMFONY_DEBUG
volumes:
- app-data:/acme/myapp/app/data
myapp_search_site:
build: myapp
image: "acme/myapp-search-site:${TAG}"
working_dir: /acme/myapp
command: bin/console rabbitmq:consumer -l128 --no-interaction search_site
restart: always
depends_on:
- rabbitmq
environment:
- SYMFONY_ENV
- SYMFONY_DEBUG
volumes:
- app-data:/acme/myapp/app/data
myapp_filter_auctions:
build: myapp
image: "acme/myapp-filter-auctions:${TAG}"
working_dir: /acme/myapp
command: bin/console rabbitmq:consumer -l128 --no-interaction filter_auctions
restart: always
depends_on:
- rabbitmq
environment:
- SYMFONY_ENV
- SYMFONY_DEBUG
volumes:
- app-data:/acme/myapp/app/data
rabbitmq:
image: rabbitmq:3-management
environment:
RABBITMQ_DEFAULT_USER: acme
RABBITMQ_DEFAULT_PASS: acme
ports:
- "15672:15672"
restart: always
volumes:
- rabbitmq-data:/var/lib/rabbitmq
nginx:
build:
context: ./docker/nginx
image: "acme/nginx:${TAG}"
volumes_from:
- myapp
ports:
- "80:80"
restart: always
volumes:
app-data:
driver: local
rabbitmq-data:
driver: local
version: '2'
services:
myapp:
build: myapp
image: "acme/myapp:${TAG}"
volumes:
- app-data:/acme/myapp/app/data
- ./myapp:/acme/myapp
working_dir: /acme/myapp
restart: always
depends_on:
- rabbitmq
environment:
- SYMFONY_ENV
- SYMFONY_DEBUG
myapp_search:
build: myapp
image: "acme/myapp-search:${TAG}"
working_dir: /acme/myapp
command: bin/console rabbitmq:consumer -l128 --no-interaction search
restart: always
depends_on:
- rabbitmq
environment:
- SYMFONY_ENV
- SYMFONY_DEBUG
volumes:
- app-data:/acme/myapp/app/data
myapp_search_site:
build: myapp
image: "acme/myapp-search-site:${TAG}"
working_dir: /acme/myapp
command: bin/console rabbitmq:consumer -l128 --no-interaction search_site
restart: always
depends_on:
- rabbitmq
environment:
- SYMFONY_ENV
- SYMFONY_DEBUG
volumes:
- app-data:/acme/myapp/app/data
myapp_filter_auctions:
build: myapp
image: "acme/myapp-filter-auctions:${TAG}"
working_dir: /acme/myapp
command: bin/console rabbitmq:consumer -l128 --no-interaction filter_auctions
restart: always
depends_on:
- rabbitmq
environment:
- SYMFONY_ENV
- SYMFONY_DEBUG
volumes:
- app-data:/acme/myapp/app/data
rabbitmq:
image: rabbitmq:3-management
environment:
RABBITMQ_DEFAULT_USER: acme
RABBITMQ_DEFAULT_PASS: acme
ports:
- "15672:15672"
restart: always
volumes:
- rabbitmq-data:/var/lib/rabbitmq
nginx:
build:
context: ./docker/nginx
image: "acme/nginx:${TAG}"
volumes:
- ./docker/nginx/dev/acme.conf:/etc/nginx/conf.d/acme.conf
volumes_from:
- myapp
ports:
- "80:80"
restart: always
volumes:
app-data:
driver: local
rabbitmq-data:
driver: local
FROM acme/php:7.0-fpm
ENV PROJECT_DIR /acme/myapp
WORKDIR $PROJECT_DIR
ADD . $PROJECT_DIR
RUN chown -R www-data:www-data $PROJECT_DIR
USER www-data
RUN composer install --no-interaction --no-progress --prefer-dist --no-dev -o \
&& composer clear-cache --quiet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment