Docker – Docker compose for Laravel


Docker – Docker compose for Laravel. Previously, I wrote about Docker – Dockerfile – Setup Environment, today, we will start with the real project. The topic I want to mention is to build an environment for Laravel project quickly with docker-compose.

No need to introduce, let’s start. 😀

1. Install docker compose

This part is quite simple. You access the docker’s Docs page to install depending on the operating system.

2. Apply docker compose permission

chmod +x /usr/local/bin/docker-compose

To make sure docker-compose has been successfully installed on the machine we check with the command:

docker-compose -v

3. Directory structure for the project

The next thing is that I will go to create the folder to save the configuration of docker and source code: The structure of the folder looks like this:

Docker - Docker compose for Laravel

  • vhosts.conf: save the contents of virtualhost config in the docker-container that I will create.
# vhost.conf
DirectoryIndex index.php index.html
<Directory /app/public/>
   Options -Indexes +FollowSymLinks
   AllowOverride All
   Require all granted
<VirtualHost *:80>
   ServerName localhost
   DocumentRoot /app/public
   <Directory "/app/public">
       AllowOverride All
       Options -Indexes +FollowSymLinks
       Order allow,deny
       Allow from all
  • src: directory that stores source code, is all that a laravel project needs
  • Dockerfile: Save the config to build on an image with the things I have configured in this. For example, PHP, apache2, composer…
// Dockerfile

FROM ubuntu:16.04

// Install apache, PHP, and supplimentary programs. openssh-server, curl, 
// and lynx-cur are for debugging the container.
RUN apt-get update && apt-get -y upgrade && apt-get -y install \
 apache2 php libapache2-mod-php php-mcrypt php-mysql curl php-cli php-mbstring git unzip php-xml \
 libcurl4-openssl-dev pkg-config libssl-dev libsslcommon2-dev php-pear php-dev vim

RUN curl -sS -o composer-setup.php
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer

// Enable apache mods.
RUN a2enmod rewrite
RUN a2enmod headers

// Manually set up the apache environment variables
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2

// install nodejs, npm, bower
RUN apt-get -y install nodejs npm git
RUN npm install -g bower
RUN npm install --global gulp-cli
RUN apt-get install -y ruby-full rubygems
RUN gem install sass
RUN ln -s /usr/bin/nodejs /usr/bin/node

RUN mkdir /app

// Expose apache.

// Update the default apache site with the config we created.
ADD conf.d/vhosts.conf /etc/apache2/sites-enabled/000-default.conf

// By default start up apache in the foreground, override with /bin/bash for interative.
CMD systemctl restart apache2
CMD /usr/sbin/apache2ctl -D FOREGROUND
  • docker-compose.yml: define the containers we need. Currently, we need a Web container and a container for the database.
# docker-compose.yml
version: '2'
    build: .
    container_name: demo-web
      - ./src:/app
      - "90:80"
      - serverdb
    image: mysql:5.7
    container_name: demo-serverdb
      - ./data/mysql:/var/lib/mysql
      - 3406:3306

There is a config in this file that links. This config will automatically link to another container in the docker system. The example here is the serverdb container. In the project’s .env file, just declare DB_HOST = serverdb that will automatically understand the host database in the serverdb container.

4. Apply permission for folder

Don’t forget to decentralize the src/storage directory:

sudo chmod 777 -R src/storage

5. Build docker image from Dockerfile

Now we will build the image in docker with the command:

docker-compose build

Come here, please be happy longf wait because this build command will pull an Ubuntu image: 16.04 and install everything that we config in Dockerfile on it so it will be quite time consuming.

6. Build docker container

After running the build command, run the command docker-compose up. This command is used to build the container that we need. Run the command to boot ps to see which container it is running.

Now go to the browser and try localhost: 90 to see the results. Wished everyone success.


Leave a Reply