Skip to content

Instantly share code, notes, and snippets.

@jarektkaczyk
Last active February 4, 2022 13:53
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarektkaczyk/69de0e48fbadccca8371faa32e0bac63 to your computer and use it in GitHub Desktop.
Save jarektkaczyk/69de0e48fbadccca8371faa32e0bac63 to your computer and use it in GitHub Desktop.
Docker setup for laravel project

ASSUMING YOU HAVE DOCKER INSTALLED ON YOUR PLATFORM

https://docs.docker.com/engine/installation/

Setup

  1. In the project root create .docker folder and docker-compose.yml file - contents below
  2. Create .docker/site.conf file with nginx setup - contents below
  3. Add .docker to .gitignore (recommended global .gitignore https://davidwalsh.name/global-gitignore)
  4. Run docker-compose up -d in the root of your app, it will:
  5. Pull 3 images: php, nginx and mysql
  6. Start and link 3 containers in a network
  7. MySql container will create the db automatically for you and it will be store in .docker/mysql folder, outside the container, so it persists
  8. Make your web container (nginx) accessible via http://localhost:8081

Optional:

  1. Wrap the above steps in a sh function, so you can do that quickly for each project

  2. Create a few handy aliases for docker, eg.: alias dcompose="docker-compose" alias dterm='_dterm(){ docker exec -it docker ps -q -n=1 -f name=$1 bash; }; _dterm'

  3. In order to login to bash on your main app container (php) you'd do: (assuming your app folder is my_app) dterm my_app_php the name of the container you can check by calling docker-compose ps in app folder

  4. Setup dev domain in /etc/hosts

// 1 docker-compose.yml
web:
  image: nginx:latest
  links:
    - db
    - php
  volumes:
    - .:/var/www/html
    - .docker/site.conf:/etc/nginx/conf.d/default.conf
  ports:
    - "8081:80"
  working_dir: /var/www/html
php:
  image: jarektkaczyk/lara-php:7-fpm
  links:
    - db
  volumes:
    - .:/var/www/html
  working_dir: /var/www/html
  environment:
    TERM: xterm
db:
  image: mysql:5.6
  environment:
    MYSQL_ROOT_PASSWORD: ____ROOT_PASS_HERE____
    MYSQL_DATABASE: ____DB_NAME_HERE____
    MYSQL_USER: ____DB_USER_HERE____
    MYSQL_PASSWORD: ____DB_PASS_HERE____
    TERM: xterm
  volumes:
    - .docker/mysql:/var/lib/mysql
  ports:
    - "33081:3306"
// 2 .docker/site.conf
server {
    listen 80;
    root /var/www/html/public;
    index index.php index.html index.htm;

    client_max_body_size 100m;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ ^/.+\.php(/|$) {
        fastcgi_pass php:9000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment