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?
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:
Once you done that, you should end up with something like this
Next thing to do is:
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:
You should end up with something like this:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With