Ray Kootstra Ray Kootstra - 5 months ago
837 0

Convert project to Docker


1. Create root dir: "docker”
2. Create in “docker” dir, dir: “php-apache”
3. Add Dockerfile into “php-apache” dir (see config file)
4. Update composer key in docker file!
5. Add website.conf in “php-apache” directory
6. Add in root "docker-compose.yml”
7. Add additional information to the README.md file
8. add root dir shell-tools
9. create file reload_project.sh and add config as below
10. Change parameters.yml.dist to ip number of docker
11. Update app_dev.php with ip number from docker (192.168.99.1)

Other

Dockerfile

FROM ubuntu:14.04
MAINTAINER CompanyName <info@companyname.com>

RUN apt-get -yqq update && apt-get -yqq install apache2 curl
RUN apt-get -yqq update && apt-get -yqq install git php5 php5-gd php5-mysql php5-curl php5-mcrypt php5-xmlrpc php5-imagick php5-apcu php5-intl php5-xsl php5-xdebug

ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
ENV APACHE_RUN_DIR /var/run/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2

RUN php5enmod gd mysql curl mcrypt xmlrpc imagick apcu intl
RUN a2enmod expires ssl rewrite headers mime_magic vhost_alias

RUN mkdir -p $APACHE_RUN_DIR $APACHE_LOCK_DIR $APACHE_LOG_DIR /var/www
RUN chown $APACHE_RUN_USER:$APACHE_RUN_GROUP /var/www

ADD website.conf /etc/apache2/sites-available/

RUN ln -s /etc/apache2/sites-available/website.conf /etc/apache2/sites-enabled/website.conf
RUN rm /etc/apache2/sites-enabled/000-default.conf

RUN  echo "    IdentityFile ~/.ssh/id_rsa" >> /etc/ssh/ssh_config
RUN  echo "    StrictHostKeyChecking no" >> /etc/ssh/ssh_config

# Install composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php -r "if (hash_file('SHA384', 'composer-setup.php') === '55d6ead61b29c7bdee5cccfb50076874187bd9f21f65d8991d46ec5cc90518f447387fb9f76ebae1fbbacf329e583e30') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
RUN php composer-setup.php --install-dir=/usr/bin --filename=composer
RUN php -r "unlink('composer-setup.php');"

# Add nodejs
#RUN curl -sL https://deb.nodesource.com/setup_4.x | bash -
#RUN apt-get install -y nodejs
#RUN npm install npm -g

# Add bundler
#RUN apt-get install -y ruby ruby-dev make
#RUN gem install bundler

EXPOSE 8080

ENTRYPOINT [ "/usr/sbin/apache2" ]
CMD ["-D", "FOREGROUND"]
Apache Configuration

website.conf

<VirtualHost *:80>

    ## Vhost docroot
    DocumentRoot "/var/www/web"

    ## Directories, there should at least be a declaration for /var/www/redirect

    <Directory "/var/www/web">
        Options FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

    ## Logging
    ErrorLog "/var/log/apache2/apache_error.log"
    ServerSignature Off
    CustomLog "/var/log/apache2/apache_access.log" combined

</VirtualHost>
YAML

docker-compose.yml

version: '2'
services:
    db:
        image: mysql:5.5
        ports:
            - "3306:3306"
        environment:
            MYSQL_ROOT_PASSWORD: root
            MYSQL_DATABASE: symfony
    php-apache:
        build: docker/php-apache
        ports:
            - "8080:80"
        volumes:
            - ./:/var/www
            - ./docker/logs/symfony:/var/www/app/logs
            - ./docker/logs/apache2/:/var/log/apache2
        links:
            - db
Markdown

README.md

## Launch in docker
Launch the ```Docker Quickstart Terminal``` and go to the project directory:

```
cd website
```

Startup the docker environment:

```
docker-compose --project-name myproject up -d
```

This will launch a mysql instance and a PHP/Apache instance


To stop the docker environment run

```
docker-compose --project-name myproject down
```
but this will stop and remove the container.
An other alternative is to stop the container and preserve the container is
```
docker-compose stop
```
To start the container just use
```
docker-compose start
```

## Setup the site from within docker

Enter the docker PHP/Apache setup...
 
```
docker exec -t -i -u www-data myproject_php-apache_1 /bin/bash
```

... and run the shell script to setup the project.

If the composer install fails on a password during the install of the routable-entity-bundle, then exit the docker container and run composer install locally. After success you can restart the reload_project.sh within the docker container.

```
cd /var/www
bash shell-tools/reload_project.sh
```

By default the site will be available on ```http://192.168.99.100/app_dev.php```
Bash

reload_project.sh

#!/usr/bin/env bash

composer install
php app/console doctrine:database:drop --force
php app/console doctrine:database:create
php app/console doctrine:migrations:migrate --no-interaction
php app/console doctrine:fixtures:load --no-interaction