Skip to content

Instantly share code, notes, and snippets.

@acodeninja
Last active January 29, 2019 16:13
Show Gist options
  • Save acodeninja/d70031f2f525d6b70d18aa87443cbcca to your computer and use it in GitHub Desktop.
Save acodeninja/d70031f2f525d6b70d18aa87443cbcca to your computer and use it in GitHub Desktop.
Symfony Docker Composer Setup

Symfony 3 Docker

This set of files gives you a nice little environment to work with a symfony 3 project using docker-compose.

setup

Copy all four files to the root of your project:

  • docker-compose.yml: describes the available services
  • Dockerfile: builds the php backend service
  • nginx.conf: configuration for nginx to serve from the backend service
  • .env: configuration information for each of the services in the docker-compose

what you get

The docker-compose file describes four services:

  • web: an nginx service configured with the nginx.conf file
  • backend: a php service with some good development defaults installed
  • database: a mysql service that can be configured using the .env file
  • email: an instance of mailhog, a web service that can be used to test sending emails from your application

how to...

get it running

$ docker-compose up -d

Once the services have all started you will have two addresses set up:

http://0.0.0.0:8080/ - the symfony project, if you get a message saying: "You are not allowed to access this file." then you can fix this with a small change to the app_dev.php file.

if (isset($_SERVER['HTTP_CLIENT_IP'])
    || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
    || !(in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1')) || php_sapi_name() === 'cli-server')
) {

change to:

if (
    getenv('APP_ENVIRONMENT') !== 'dev' &&
    (
        isset($_SERVER['HTTP_CLIENT_IP'])
     || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
     || !(in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1')) || php_sapi_name() === 'cli-server')
    )
) {

http://0.0.0.0:8025/ - the web inteface for the email service, give it a test now by running the following:

$ docker-compose run backend sendmail -S email:1025 someone@nowhere <<'EOF'
Hey there\!
EOF

install composer requirements

$ docker-compose run backend composer install

When symfony kicks in and asks for database credentials, just use the details you have in the .env file.

run any console command

$ docker-compose run backend bin/console <command>

Note that console commands will not work until your parameters.yml file has been set up.

version: '3'
services:
web:
image: nginx:alpine
volumes:
- ./:/var/www/html
- ./nginx.conf:/etc/nginx/conf.d/default.conf
links:
- backend
ports:
- "8080:80"
backend:
build: .
volumes:
- ./:/var/www/html
env_file:
- docker.env
links:
- database
- email
database:
image: mysql
env_file:
- docker.env
volumes:
- database:/var/lib/mysql
email:
image: mailhog/mailhog
ports:
- "8025:8025"
volumes:
database:
APP_ENVIRONMENT=dev
APP_SECRET=ChangeMeIAmNotAllThatSecure
MYSQL_USER=db_user
MYSQL_PASSWORD=db_password
MYSQL_DATABASE=db_database
MYSQL_HOSTNAME=database
MYSQL_PORT=3306
MYSQL_RANDOM_ROOT_PASSWORD=true
MAIL_TRANSPORT=smtp
MAIL_HOST=email
MAIL_PORT=1025
MAIL_USER=
MAIL_PASSWORD=
FROM php:fpm-alpine
# Install XDebug
RUN apk add g++ gcc make autoconf && \
pecl install xdebug-2.6.1 && \
docker-php-ext-enable xdebug && \
apk del g++ gcc make autoconf
# Install MySQL driver
RUN docker-php-ext-install pdo_mysql
# Install composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
php composer-setup.php && \
php -r "unlink('composer-setup.php');" && \
mv composer.phar /usr/bin/composer && \
echo "export PATH=$(composer config --global home)/vendor/bin:$PATH" > /etc/profile.d/00_export_composer_bin.sh
server {
server_name _;
root /var/www/html/web;
location / {
try_files $uri /app_dev.php$is_args$args;
}
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass backend:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
location ~ ^/app\.php(/|$) {
fastcgi_pass backend:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment