How to run laravel app using docker-compose? - laravel

Following is what I tried, is there something I doing wrong?
step1. create a simple laravel app at localhost.
composer create-project --prefer-dist laravel/laravel laravel-app 5.6
step2. create docker-compose.yml
version: '3'
services:
php:
image: php:7-fpm
ports:
- "3021:8000"
volumes:
- ./laravel-app:/app
composer:
image: composer:latest
volumes:
- ./laravel-app:/app
working_dir: /app
command: ["install","php artisan serve --host=0.0.0.0"]
depends_on:
- php
After that, I run docker-compose up --force-recreate -d and access 127.0.0.1:3021 at browser, but I get nothing.
Then I run docker-composer log, it shows me this error message:
Invalid argument php artisan serve --host=0.0.0.0. Use "composer require php artisan serve --host=0.0.0.0" instead to add packages to your composer.json.
How to fix this issue?

You are mixing commands. Composer does not "serve". Php has a build in dev server to "serve".
You can read more about it here: https://laravel.com/docs/4.2/quick
To actually get Laravel up and running please do the following:
1 - Run this in the laravel-app folder: composer install
2 - Create a Dockerfile with the following contents:
FROM php:7
RUN apt-get update -y && apt-get install -y libmcrypt-dev openssl
RUN docker-php-ext-install pdo mcrypt mbstring
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /app
COPY . /app
CMD php artisan serve --host=0.0.0.0 --port=8000
EXPOSE 8000
3 - Build your docker image: docker build -t my-laravel-image .
4 - Finally replace the content of your docker-compose:
version: '3'
services:
web:
image: my-laravel-image
ports:
- 3021:8000
volumes:
- ./laravel-app:/app
A more complete tutorial can be found here (not mine): https://www.techiediaries.com/docker-compose-laravel/
EDIT:
in order to use the official compose image you could simply do this:
version: '3'
services:
composer:
image: composer:latest
working_dir: /app
entrypoint: php artisan serve --host=0.0.0.0
depends_on:
- php
volumes:
- ./laravel-app:/app
ports:
- "3021:8000"
Make sure ./laravel-app contains a laravel project. Otherwise this won't work!

in the main folder of your Laravel app, create a file named Dockerfile and insert this code:
FROM php:7
RUN apt-get update -y && apt-get install -y openssl zip unzip git
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo pdo_mysql
WORKDIR /app
COPY . /app
RUN composer install
CMD php artisan serve --host=0.0.0.0 --port=8181
EXPOSE 8181
In the same main folder of Dockerfile, create a file named docker-compose.yml and insert this code:
version: '2'
services:
app:
build: .
ports:
- "8009:8000"
volumes:
- .:/app
env_file: .env
working_dir: /app
command: bash -c 'php artisan migrate && php artisan serve --host 0.0.0.0'
depends_on:
- db
links:
- db
db:
image: "mysql:5.7"
environment:
- MYSQL_ROOT_PASSWORD=yourpassword
- MYSQL_DATABASE=yourdbname
- MYSQL_USER=root
- MYSQL_PASSWORD=yourpassword
volumes:
- ./data/:/var/lib/mysql
ports:
- "3306:3306"
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8090:80
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: yourpassword
Open the terminal command line and go inside the laravel folder, and launch this commands:
docker.compose build
docker-compose up -d
if have need to create and migrate the db, or use other commands, launch the Laravel commands in this way:
docker-compose run app php artisan
The app will available at the address http://0.0.0.0:8009
Source: https://medium.com/#pierangelo1982/dockerize-an-existing-laravel-application-with-docker-compose-a45eb7956cbd

Related

How to run db migration when Laravel container is up?

I have problem with running db migration on when container is up.
Problems:
cant set app key because gitlab-ci didn't copy .env file (getting err in gitlab ci console), so setting key needs to happen later
running migration with wait-for-it because container exits with success code 0 (migrations is up)
I will put code only for my db and web container.
db:
container_name: db
image: mysql:5.7.22
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_USER: ${DB_USERNAME}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
ports:
- 3306:3306
volumes:
- mysql-data:/var/lib/mysql
networks:
- my-network
backend:
image: registry image
container_name: "backend"
build:
context: ./backend
dockerfile: Dockerfile
depends_on:
- db
ports:
- 1020:80
networks:
- my-network
gitlab-ci:
build-backend:
tags:
- vps
variables:
GIT_CLEAN_FLAGS: none
stage: dockerize
image: docker:latest
services:
- docker:dind
dependencies: []
script:
- docker build -t backend backend
- cp .env ./backend/.env
- cd backend
- docker build -t $CI_REGISTRY_IMAGE/backend:$CI_COMMIT_BRANCH .
- docker tag $CI_REGISTRY_IMAGE/backend:$CI_COMMIT_BRANCH $CI_REGISTRY_IMAGE/frontend:$CI_COMMIT_REF_NAME
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
- docker push $CI_REGISTRY_IMAGE/backend:$CI_COMMIT_REF_NAME
...deploying code
I'm using https://github.com/vishnubob/wait-for-it
Dockerfile:
FROM webdevops/php-nginx:7.4-alpine
# Install Laravel framework system requirements (https://laravel.com/docs/8.x/deployment#optimizing-configuration-loading)
RUN apk add oniguruma-dev postgresql-dev libxml2-dev
RUN docker-php-ext-install \
bcmath \
ctype \
fileinfo \
json \
mbstring \
pdo_mysql \
pdo_pgsql \
tokenizer \
xml
# Copy Composer binary from the Composer official Docker image
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
ENV WEB_DOCUMENT_ROOT /var/www/html/public
ENV APP_ENV production
WORKDIR /var/www/html
COPY . .
RUN composer install --no-interaction --optimize-autoloader
RUN chown -R root:root .
RUN chmod -R ugo+rw storage
RUN chmod 777 wait-for-it.sh
RUN chmod 777 migrate.sh
EXPOSE 80
CMD ["./wait-for-it.sh", "db:3306", "--", "./migrate.sh"]
migrate.sh:
#!/bin/sh
php artisan key:generate
# Optimizing Configuration loading
php artisan config:cache
# Optimizing Route loading
php artisan route:cache
# Optimizing View loading
php artisan view:cache
echo "finished cashes"
php artisan migrate --force &
exec "$#"
So how can I solve exit code 0, meant to say how to prevent container from stopping?
Thanks
Solution is to use Supervisor which is useful for having all the jobs in background and wont close your container while running migrations.
I can't believe that this configuration from this repo is working perfectly it saves my time. I spent more than 7 days in searching for the best solution, but this guy who posted this saves me!
Refer to this repo please https://github.com/harshalone/laravel-9-production-ready
I didn't have to change a line of code, hope you don't have too. Simply works!
for anyone struggling with running migrations but your database isn't up before your Laravel application, just want to mention that I've used wait-for-it script and changed last line of code in my Dockerfile like this:
CMD ["/var/www/docker/wait-for-it.sh", "db:3306", "--", "/var/www/docker/run.sh"]
So now my migrations will first wait for database to be up and running.
Just put wait-for-it.sh inside of your docker folder or use it from github directly.

Docker-compose run script after container starts

My goal is to have a one-liner (kinda) for building and starting my app. I want to be able to execute this command and run several additional commands related to my app (like migrations, init and starting a websocket server).
But when I try to use ENTRYPOINT directive my php service runs commands but the service itself is not working properly.
docker log nginx shows me this error:
[error] 31#31: *2 connect() failed (111: Connection refused) while connecting to upstream,
If I comment out ENTRYPOINT in back.dockerfile my app runs fine, but I have to manually run those artisan commands
docker-compose build && docker-compose up -d
Is there a way to achieve such result?
My docker-compose.yml
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
volumes:
- ./app:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
ports:
- "8000:80"
depends_on:
- php
- mysql
networks:
- laravel
tty: true
php:
build:
context: .
dockerfile: ./docker/back.dockerfile
container_name: php
ports:
- "9000:9000"
depends_on:
- node
networks:
- laravel
node:
build:
context: .
dockerfile: ./docker/front.dockerfile
container_name: node
ports:
- "3000:3000"
networks:
- laravel
mysql:
image: mysql:8.0
container_name: mysql
restart: unless-stopped
ports:
- "3306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: laravel
MYSQL_USER: laravel
MYSQL_PASSWORD: laravel
MYSQL_ROOT_PASSWORD: root
SERVICE_NAME: mysql
networks:
- laravel
back.dockerfile
FROM php:7.4-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql
RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
RUN apk update
RUN apk upgrade
RUN apk add bash
RUN alias composer='php /usr/bin/composer'
ENV COMPOSER_ALLOW_SUPERUSER 1
COPY ./app/composer.json ./app/composer.lock ./
COPY ./app .
RUN composer install --no-scripts --prefer-dist --optimize-autoloader
RUN chown -R www-data:www-data /var/www/html
RUN chmod -R 755 /var/www/html/storage
COPY ./entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 6001
entrypoint.sh
#!/bin/sh
set -e
php artisan migrate --path 'database/migrations'
php artisan app:init
php artisan websockets:serve
exec "$#"

Docker container called in Laravel

How do I call a docker container within Laravel? I am using the following package to download PDFs https://packagist.org/packages/spatie/browsershot, I have an npm container which I am trying to use with this package.
My site container doesn't have node or npm installed which is why I need to use my npm container instead.
Here is my docker file
version: '3'
networks:
laravel:
services:
site:
build:
context: .
dockerfile: docker/nginx/Dockerfile
container_name: nginx
ports:
- 80:80
volumes:
- ./src:/var/www/html:delegated
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:5.7.29
container_name: mysql
restart: unless-stopped
tty: true
ports:
- 3306:3306
environment:
MYSQL_DATABASE: odc
MYSQL_USER: admin
MYSQL_PASSWORD: ".d3aP/7N,c)&^EdC"
MYSQL_ROOT_PASSWORD: "%3h_Ssxm7sSj#bw!"
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
php:
build:
context: .
dockerfile: docker/php/Dockerfile
container_name: php
volumes:
- ./src:/var/www/html:delegated
networks:
- laravel
composer:
build:
context: .
dockerfile: docker/composer/Dockerfile
container_name: composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- php
user: laravel
entrypoint: ['composer', '--ignore-platform-reqs']
networks:
- laravel
npm:
image: node:13.7
container_name: npm
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
entrypoint: ['npm']
networks:
- laravel
chrome:
image: zenika/alpine-chrome:latest
container_name: headless-chrome
networks:
- laravel
command: [chromium-browser, "--headless", "--disable-gpu", "--no-sandbox", "--remote-debugging-address=0.0.0.0", "--remote-debugging-port=9222"]
Here is the code I am trying to use with Browsershot to point to my npm container
Browsershot::html($html)
->setIncludePath('npm')
->setNodeModulePath("npm")
->setChromePath('chrome')
->noSandbox()
->showBackground()
->format('A4')
->savePdf($path . '/portrait.pdf');
As you can see I am using the ->setIncludePath('npm') to try and call my npm container but I am still receiving this error when I try and run this code
sh: npm: not found sh: node: not found
You dont need a seperate Node / NPM Container. The Browsershot is part of your applications core. To make it work you need nodejs and npm. That saying I would construct my own container image incuding PHP and Node. That makes the implementation of Browershot more simple.
If neede you can split them up later. But in that case I would create a new BrowserShot service (PHP + Node).
As you can see, there are multiple ways doing it.
What about using NGINX Unit for your applications container base? You could use the Unit PHP base image and add node on top of it.
I did that here:
https://github.com/nginx/unit-examples/blob/master/Dockerfile
FROM nginx/unit:1.22.0-php7.3
RUN mkdir /var/apphome/ && groupadd -r appuser && useradd --no-log-init -r -g appuser appuser && \
chown -R appuser:appuser /var/apphome/ && \
apt-get update && apt-get install --no-install-recommends --no-install-suggests -y gnupg && \
curl -sL https://nginx.org/keys/nginx_signing.key | apt-key add - && \
echo "deb https://packages.nginx.org/unit/debian/ buster unit" >> /etc/apt/sources.list.d/unit.list && \
echo "deb-src https://packages.nginx.org/unit/debian/ buster unit" >> /etc/apt/sources.list.d/unit.list && \
apt update && apt install -y unit-dev npm php7.3-mysql php7.3-gd && \
npm install -g --unsafe-perm unit-http -y
COPY src/ /var/apphome/app/
RUN chown -R appuser:appuser /var/apphome/
COPY .unit.conf.json /docker-entrypoint.d/.unit.conf.json
CMD ["unitd", "--no-daemon", "--control", "unix:/var/run/control.unit.sock"]
Note! This is a demo purpose. You can adapt it for your production needs!

Why is my Docker volume only sharing the docker-compose file?

I am trying to containerize a Laravel application using Docker Compose, but I am failing to use a shared volume to bring in my actual project app files. My docker-compose file looks like this:
version: "3.7"
services:
app:
build:
args:
user: sam
uid: 1000
context: ./
dockerfile: Dockerfile
image: converter
container_name: converter-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- converter
db:
image: mysql:5.7
container_name: converter-db
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker-compose/mysql:/docker-entrypoint-initdb.d
networks:
- converter
nginx:
image: nginx:alpine
container_name: converter-nginx
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d/
networks:
- converter
networks:
converter:
driver: bridge
volumes:
app-volume:
and the Dockerfile for the app service:
FROM php:7.4-fpm
# Arguments defined in docker-compose.yml
ARG user
ARG uid
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Get latest Composer
#RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
# Set working directory
WORKDIR /var/www
USER $user
I then run docker-compose build app and docker-compose up -d to create the containers, which brings them up. When I go to check the /var/www folder of the app container using docker-compose exec app ls -l, the only file that shows is the docker-compose file:
Shouldn't the shared volume specified for the app service share my working directory in the app /var/www folder?
You need to add these config options to the app service in compose file:
links:
- nginx
depends_on:
- nginx
These options forces docker-compose to create 1st the nginx service, and after the app, this is neccessary becuse currently nginx is created after app, and nging overwrite the /var/www directory

Laravel Not Found, Docker, Apache2

I am traying to connect to my container but I am getting the following error. Before my container works without problems. I made a new build but it doesn’t work.
My Docker file is the following:
FROM php:7.2-apache
LABEL maintainer="christianahvilla#gmail.com"
# Install PHP
RUN apt-get update && apt-get install -y \
curl \
zlib1g-dev \
libzip-dev \
nano
# Add and Enable PHP-PDO Extenstions
RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN docker-php-ext-enable pdo_mysql
RUN docker-php-ext-install zip
# # Install PHP Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
#set our application folder as an environment variable
ENV APP_HOME /var/www/html
#change uid and gid of apache to docker user uid/gid
RUN usermod -u 1000 www-data && groupmod -g 1000 www-data
COPY --chown=www-data:www-data . $APP_HOME
#Expose Port 8000 since this is our dev environment
EXPOSE 8000
My Docker-Compose:
version: "3.7"
services:
#Laravel App
web:
build:
context: .
dockerfile: Dockerfile
ports:
- 8000:80
volumes:
- ./:/var/www
- ./public:/var/www/html
networks:
- mynet
depends_on:
- db
#MySQL Service
db:
image: mysql:5.7
container_name: db
ports:
- 3306:3306
environment:
MYSQL_DATABASE:
MYSQL_USER:
MYSQL_PASSWORD:
MYSQL_ROOT_PASSWORD:
volumes:
- mysqldata:/var/lib/mysql/
networks:
- mynet
#Docker Networks
networks:
mynet:
driver: bridge
#Volumes
volumes:
mysqldata:
driver: local
When I try to access to http:localhost:8000/ I can do it but if I try to access to another route I get the error.
You have to configure the apache2.conf in /etc/apache2/apache2.conf from Dockerfile, and also a2endmode rewrite, finally you need to restart apache2:
RUN sed -i '/<Directory \/var\/www\/>/,/<\/Directory>/ s/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf
RUN a2enmod rewrite
RUN service apache2 restart
Then run docker-compose build and docker-compose up -d

Resources