Docker LEMP Stack

Here is how I setup a Docker LEMP stack on Ubuntu 14.04 w/ Mariadb and redis

FROM ubuntu:14.04.2                                                                  
MAINTAINER hpz <email>                                           
                                                                                     
RUN echo "deb http://ppa.launchpad.net/nginx/stable/ubuntu trusty main" > /etc/apt/sources.list.d/nginx.list             
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C300EE8C                
                                                                                     
RUN apt-get update                                                                   
RUN apt-get -y upgrade                                                               
                                                                                     
RUN apt-get -y install curl mcrypt php5-cli php5-fpm php5-mysql php5-mcrypt redis-tools nginx    
RUN php5enmod mcrypt                                                                 
                                                                                     
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/      
RUN mv /usr/bin/composer.phar /usr/bin/composer                                      
                                                                                     
RUN echo "daemon off;" >> /etc/nginx/nginx.conf                                      
RUN rm -f /etc/nginx/sites-available/default                                         
COPY default /etc/nginx/sites-available/default                                      
RUN rm /var/www/html/*                                                               
                                                                                     
VOLUME ["/var/log/", "/var/www/html/"]                                               
                                                                                     
EXPOSE 80                                                                            
                                                                                     
CMD service php5-fpm start && nginx

Below is my default nginx site file

server {                                                                             
        listen 80 default_server;                                                    
        listen [::]:80 default_server ipv6only=on;                                   
                                                                                     
        root /var/www/html/;                                                  
        index index.php index.html index.htm;                                        
                                                                                     
        server_name localhost;                                                       
                                                                                     
        location / {                                                                 
                try_files $uri $uri/ /index.php?$query_string;                       
        }                                                                            
                                                                                     
        location ~ \.php$ {                                                          
            try_files $uri =403;                                                                                         
            fastcgi_pass unix:/var/run/php5-fpm.sock;                                                                    
            include fastcgi_params;                                                                                      
            fastcgi_index index.php;                                                                                     
            fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;       
        }                                                                            
                                                                                     
}

Lets build our image

docker build -t nginx_1

Then run the following to set it all up

docker run --name mariadb -e MYSQL_ROOT_PASSWORD=docker -e MYSQL_DATABASE=db1 -p 3306:3306 -d mariadb
docker run --name redis -d redis
docker run --name nginx --link mariadb:mariadb --link redis:redis -p 80:80 -v /path/to/local/docker/www/:/var/www/html -d nginx_1

If we want to connect to our nginx container shell:

docker run --rm -t -i --volumes-from nginx --link redis:redis --link mariadb:mariadb nginx_1 /bin/bash

docker cleanup:
occasionally I want to clean everything up and start fresh

docker stop $(docker ps -q)
docker rm $(docker ps -aq)
docker rmi $(docker images|grep "^<none>" | awk '{print $3}')

Leave a Reply

Your email address will not be published.