Intern’s Guide to Creating Your First Laravel Application in Docker

Written by JP La Torre

Our API is written in Laravel, and we run it in production using Docker. So naturally one of the first tasks we had for George, one of our Summer interns, was to familiarize himself with Docker to understand more about our stack. He picked it up fairly quickly, and we decided to turn his experience into a blog post to share his learning curve.

— Preface by JP La Torre, Co-Founder & CEO at Caylent

Introduction

Docker is now “the world’s leading software containerization platform,” providing a platform for you to develop your applications. Using Docker can help majorly reduce anxiety about the discrepancies that exist between your development and production/deployment environments. The platform makes use of container technology, which provides much of the functionality of traditional hypervisors (which includes virtual machines) at a fraction of the cost.

Essentially, containers are a lightweight alternative to virtual machines which enable developers to quickly deploy their applications in a production setting. Instead of requiring separate copies of both OS and hardware resources (RAM and CPU) that virtual machines rely on, container technology uses local OS and hardware by partitioning the resources into separate containers. Each container is thus able to run its own process/application in an isolated environment. Docker’s container technology is also able to provide a supportive environment for development, testing, and deployment.

How does Docker come into play in this article? As Docker streamlines the development and deployment process, I wanted to write a guide to Dockerize (the means by which you ready an application to run inside a Docker container) a simple Laravel application.

 

A general three-step process occurs when you Dockerize any application:

  1. Write a Dockerfile: A text file that contains a bunch of preloaded bash commands to provide the default states for parameters, application, and resources.
  2. Build the Dockerfile to produce a Docker image: A snapshot of the application. 
  3. Run the Docker image: To launch a Docker container that your application runs in.

 

Now let’s get started!

A Step-by-Step Process to Creating a Laravel Application

Begin by downloading Docker from https://docs.docker.com/ for either Mac, Linux or Windows. Once installed, open up a command line/terminal window.

 

Make a new directory/folder to house your eventual Dockerfile and access the folder. Enter the commands:

 

mkdir <name of directory of your choice>

Example: mkdir helloworld

 

cd <into the directory you just created>

Example: cd helloworld

 

With the directory and cd set up, we move to actual Docker-related steps!

Write a Dockerfile:

Create a Dockerfile using the command:

touch Dockerfile

 

The Dockerfile is now editable. Open up your favorite text editor, I personally use Sublime, and within the command line type:

subl Dockerfile

 

This should open your Dockerfile within the Sublime text editor. From here, we write our first Dockerfile to build our first Docker image. Don’t be alarmed that we haven’t touched Laravel. Dockerfile’s brilliance is that commands can be scripted within the Dockerfile to install Laravel. Remember, a Dockerfile is simply a text file that contains a bunch of preloaded bash commands to provide the default states for parameters, application, and resources. Check out the bash and Docker-specific commands for use within your Dockerfile.

 

For the Curious Reader

Some Docker specific commands briefly listed below.

 

  • FROM tells you what the base image your docker container is running. It’s the foundation to build the rest of your image on.
  • MAINTAINER simply tracks who the author of the Dockerfile is.
  • RUN executes a command(s) in a new layer and creates a new image. It is often used for installing software packages.
  • CMD sets default command and/or parameters. Overwrite defaults from the command line at any point.
  • ENTRYPOINT configures a container that will run as an executable.

 

Build the Dockerfile to Produce a Docker Image:

We use Ubuntu (version 14.04) for the base image, making the command we type into the Dockerfile:

FROM ubuntu:14.04

 

To attribute authorship, we use the MAINTAIN command as such:

MAINTAINER George J. <your.name@email.com>

 

We now have the basic foundation of our Dockerfile using FROM and MAINTAIN. If we use RUN here, we can really get into the meat of the Dockerfile. The below RUN command will set up the necessary default packages for the container to run our Laravel application (note: && \ signals the RUN command to continue onto the next line).

 

RUN apt-get update && \ 
    apt-get -y install apache2 php5 libapache2-mod-php5 php5-mcrypt php5-json curl git && \ 
    apt-get clean && \ update-rc.d apache2 defaults && \ 
    php5enmod mcrypt && \ 
    rm -rf /var/www/html && \ 
    curl -sS https://getcomposer.org/installer | php && \ 
    mv composer.phar /usr/local/bin/composer

 

What Are We Doing Here in the RUN Block?

The RUN block command updates to the newest versions of packages and their dependencies. It achieves this by:

  • Installing Apache2, PHP5, and the PHP library for Apache on top of the other technologies needed for our container.
  • Downloading Composer (a subsidiary technology used by Laravel) and moving it into our usr/local/bin/composer directory for our container.
  • Setting up a Docker suitable environment for the Laravel application build.

 

After installing all the prerequisite packages, we are using Composer’s “create-project” function to create a new Laravel application.

 

RUN composer create-project laravel/laravel laravel $LARAVEL_VERSION --prefer-dist --no-interaction && \    
    php laravel/artisan key:generate && \
    chown www-data:www-data -R laravel/storage

 

After we’ve set up our default Laravel application, it is now important to have our container be receptive to HTTP and HTTPS ports using TCP/IP conventions. To do this, we can implement:

EXPOSE 80

EXPOSE 443

 

To run our Laravel application on the Apache web server; we now need to provide some default states for the server with the command:

CMD [“/usr/sbin/apache2ctl”, “-D”, “FOREGROUND”]

 

The Dockerfile is now complete.

Full Documentation of the Dockerfile

# Use this as a base image

FROM ubuntu:14.04

 

# Optional: specify the maintainer

MAINTAINER George J. <your.name@email.com>

 

ENV LARAVEL_VERSION ~5.1.0

 

# Run any command on terminal

RUN apt-get update && \

   apt-get -y install apache2 php5 libapache2-mod-php5 php5-mcrypt php5-json curl git && \

   apt-get clean && \

   update-rc.d apache2 defaults && \

   php5enmod mcrypt && \

   rm -rf /var/www/html && \

   curl -sS https://getcomposer.org/installer | php && \

   mv composer.phar /usr/local/bin/composer

 

COPY 000-default.conf /etc/apache2/sites-available/000-default.conf

 

# Install Laravel

WORKDIR /var/www

 

RUN composer create-project laravel/laravel laravel $LARAVEL_VERSION –prefer-dist –no-interaction && \

   php laravel/artisan key:generate && \

   chown www-data:www-data -R laravel/storage

 

# To EXPOSE necessary ports to the container

EXPOSE 80

EXPOSE 443

 

# COPY setup-laravel.sh

#/setup-laravel.sh

#RUN [“chmod”, “+x”, “/setup-laravel.sh”]

#ENTRYPOINT [“/setup-laravel.sh”]

 

CMD [“/usr/sbin/apache2ctl”, “-D”, “FOREGROUND”]

 

To actually build the Dockerfile into a Docker image, let’s go back into the terminal window and type the command:

docker build <the file path to your directory>

 

Remember, as my directory was helloworld, the full command would be:

docker build /george/Desktop/helloworld

 

The program will immediately build an image for you, which you can see right away by running the command:

docker images

 

The command will reveal all Docker images on your local computer; both created images/those pulled from DockerHub—a repository for Docker images.

Run the Docker Image:

To run the Docker image, loading your Laravel application into a container, first identify the IMAGEID of your Docker image. For this, run the command:

docker images

 

After locating the IMAGEID (which is usually a big number), you can run the command:

docker run -d -P IMAGEID

//example: docker run -d -P 2173891279

 

-d and -P are simply tags to include alongside any bash commands that tell your container what the specific features of the run command to use.

 

-d simply daemonizes it and runs the container in the background

-P finds the next available port for HTTP (80 on container) and HTTPS (443 on container)

 

The container now listens to port 80 and port 443—the -P essentially binds each of these ports on the container to a port on your local computer (aka localhost). Go back into the terminal window and type the command:

docker ps

 

Now you’ll be able to see which to which port your localhost has binded. It should resemble the form 0.0.0.0.50454, where 50454 is the port. Here, 50454 is just an example port. Your individual port could be different.

 

To complete this step, open up your favorite web browser (Safari, Chrome, Firefox, etc.). Type in: localhost: PORT (where PORT is the value of your port)

 

In our case, we type into our web browser:

http://localhost:50454

 

Run the local URL to see a page with Laravel 5 pop up! Congrats on writing your first Laravel application. For more information on container technology and it’s endless potential read our blog post on how to Setup a High-Availability Docker Swarm Stack on AWS.

Related Posts

The DevOps container management platform

  • Amazon AWS
  • Microsoft Azure
  • Google Cloud
  • Digital Ocean

Unlimited users. Unlimited applications. Unlimited stacks. Easily manage Docker containers across any cloud.

Get Started—Free!
Top
%d bloggers like this: