Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying Laravel on Heroku with Docker heroku.yml and artisan

I want to deploy my Laravel app to heroku for staging environment in my initial phase of the project. This is the Dockerfile and docker-compose configuration, respectively.

Dockerfile:

FROM php:7.2 AS base

RUN apt-get update -y && \
    apt-get install -y openssl zip unzip git gnupg2


RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && \
    apt-get install -y nodejs

RUN curl -sS https://getcomposer.org/installer | \
    php -- --install-dir=/usr/local/bin --filename=composer

RUN docker-php-ext-install mbstring pdo pdo_mysql

WORKDIR /app
COPY . /app

FROM base AS dev

RUN apt-get update && apt-get install -y zlib1g-dev
RUN docker-php-ext-install zip

RUN composer install --prefer-dist
RUN npm install && npm run dev

docker-compose.yml:

version: '3.4'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
      target: dev
    command: php artisan serve --host=0.0.0.0 --port=8080
    env_file:
      - ./etc/env/.env.dev
    working_dir: /app
    volumes:
      - ./:/app
    ports:
      - 8080:8080

This setup runs well in my local deployment using docker-compose up -d

I want to try to deploy this setup to Heroku using the heroku.yml.

This is the heroku.yml setup.

build:
    docker:
        web: Dockerfile
run:
    web: php artisan serve --host=0.0.0.0 --port=$PORT

After that I execute

heroku container:push web
heroku container:release web

The commands return okay. But when I open using heroku open shows error. This is the error log from heroku logs --tail.

019-02-28T02:18:59.349362+00:00 app[api]: Deployed web (9552e86dc96a) by user *******@****.***
2019-02-28T02:18:59.349362+00:00 app[api]: Release v16 created by user *******@****.***
2019-02-28T02:19:23.326195+00:00 heroku[web.1]: Starting process with command `php -a`
2019-02-28T02:19:25.764530+00:00 heroku[web.1]: State changed from starting to crashed
2019-02-28T02:19:25.744534+00:00 heroku[web.1]: Process exited with status 0
2019-02-28T02:19:25.667622+00:00 app[web.1]: Interactive shell
2019-02-28T02:19:25.667644+00:00 app[web.1]: 
2019-02-28T02:19:26.615159+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=***************.herokuapp.com request_id=f81366af-be1e-4a92-a14a-5c726bf163d9 fwd="203.210.84.30" dyno= connect= service= status=503 bytes= protocol=https

What did I do wrong?

like image 931
Petra Barus Avatar asked Feb 28 '19 02:02

Petra Barus


1 Answers

I had the same situation as you have right now, and I managed to resolve it by using buildpacks instead of writing my own docker compose file.

If you wanna try my solution:

  1. Go to settings page in your heroku project
  2. Under Buildpacks section, click "Add buildpack"
  3. Click on nodejs
  4. Repeat the same process and click on PHP

Once you done that, you should end up with something like this Settings

Next thing to do is:

  1. Click again on Add buildpack
  2. Enter weback URL https://github.com/dretnan/heroku-buildpack-laravel-deploy-tasks
  3. Click Save changes

This last buildpack allows you to have custom variable with some custom command that is going to be executed during deployment.

So what you want to do is:

  1. Find Config Vars in the same settings page
  2. Click "Add" to add new variable
  3. Enter "DEPLOY_TASKS" for key
  4. Enter "npm run heroku-build" as value (or whatever you wanna name your custom script)

You should end up with something like this: Variables

Now you have a custom script that's going to be called during deployment.

Last thing that you need to do is go to your project and open package.json. Inside of that file, under "scripts" add new line and define "heroku-build" command. Your scripts object should look like this:

"scripts": {
        "dev": "npm run development",
        "development": "mix",
        "watch": "mix watch",
        "watch-poll": "mix watch -- --watch-options-poll=1000",
        "hot": "mix watch --hot",
        "prod": "npm run production",
        "production": "mix --production",
        "heroku-build": "php artisan migrate && php artisan cache:clear && chmod -R 777 public"
    },

Of course, you can define your own commands in that line, I just used migrate and cache clear, but if you need something more, feel free to add anything you want.

like image 133
Matej Petric Avatar answered Oct 18 '22 23:10

Matej Petric