Set up docker file to clone a laravel github project - laravel

I am new in laravel and docker. I have a laravel repository and want to clone it and set up docker for it. How to setup docker file for it and what should i write in docker file?

Beside using laradock, a more step-by-step approach is described in "Laravel in Docker" and the example repository buddy-works/laravel-first-steps.
It uses as an example Dockerfile:
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 mbstring
WORKDIR /app
COPY . /app
RUN composer install
CMD php artisan serve --host=0.0.0.0 --port=8181
EXPOSE 8181

Related

Deploying Laravel with Docker, Nginx, Let's Encrypt SSL and Supervisor

I am very new to containerizing approach of deploying applications. I am trying to deploy my Laravel app to Azure using Docker and ACI. I couldn't find any well explained articles or articles matching my requirements of deployment.
I am actually trying to setup a proper DevOps pipeline, with sequence being: I push my code to GitHub, Run GitHub Actions, Build Docker Image, Push to ACR and Pull in ACI.
I attempted to build the Laravel docker image in my local environment with Nginx and Supervisor in a single image and it works well. But now I want to use automated Let's Encrypt SSL in my Nginx server. If I rebuild the image every time requesting a new SSL certificate for my server with certbot that wouldn't be a right idea, right? So, what is the best way to do it?
Here is my current Dockerfile without SSL:
# Use the official PHP 8.1 image as the base image
FROM php:8.1-fpm
# Install necessary packages
RUN apt-get update && apt-get install -y git zip unzip supervisor libpng-dev libonig-dev libxml2-dev libzip-dev nginx
# Full update system
RUN apt-get upgrade -y
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql gd mbstring exif pcntl bcmath zip
# Set the working directory to /var/www/html
WORKDIR /var/www/html
# Copy the Laravel application files to the container
COPY . .
# Copy the Nginx configuration file
COPY nginx.conf /etc/nginx/sites-available/default
# Install Composer and run it to install the application dependencies
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer install --no-dev --no-interaction
# Copy the environment file
RUN cp .env.example .env
# Generate the application key
RUN php artisan key:generate
# Set the ownership and permissions of the application files
RUN chown -R www-data:www-data /var/www/html
# Copy the Supervisor configuration file
COPY supervisor.conf /etc/supervisor/conf.d/mysupervisor.conf
# Expose port 80 for the Nginx web server
EXPOSE 80
# Start Nginx and Supervisor
CMD ["/bin/sh", "-c" , "service nginx restart && /usr/bin/supervisord -c /etc/supervisor/supervisord.conf"]

E: Package 'mysql-client' has no installation candidate in php-fpm image build using docker compose

Im fairly new to docker and so im trying to learn more about it using a laravel project, im following this tutorial:
https://www.digitalocean.com/community/tutorials/how-to-set-up-laravel-nginx-and-mysql-with-docker-compose
Ive adjusted the Dockerfile a bit from what the tutorial has but even the tutorial file causes the same result.
FROM php:7.3-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Install dependencies
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - && \
apt-get update && apt-get install -y mysql-client \
RUN npm install -g npm
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo pdo_mysql
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# Change current user to www
USER www
# Set working directory
WORKDIR /var/www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
But i keep getting the following error when i run docker-compose up -d:
E: Package 'mysql-client' has no installation candidate
ERROR: Service 'app' failed to build: The command '/bin/sh -c curl -sL https://deb.nodesource.com/setup_10.x | bash - && apt-get update && apt-get install -y mysql-client nodejs build-essential vim git curl' returned a non-zero code: 100
Am i missing something?
I expected this to work since i am running apt-get update before installing mysql-client.
Thanks.
php:7.3-fpm now use Debian 10 (Buster) as its base image and Buster ships with MariaDB, so just replace mysql-client with mariadb-client should fix it.
If you still want to use the mysql client, it's called default-mysql-client now.
php:7.2-apache triggers the error as well, but I resolve it using php:7.2.18-apache
it worked for me: sudo apt-get update && apt-get install -y git curl libmcrypt-dev default-mysql-client
or alternatively apt-cache search mysql-server
find out your servers then sudo apt-get install default-mysql-server default-mysql-server-core mariadb-server-10.6 mariadb-server-core-10.6
in my case it was the above codes

How do I install composer in container of docker?

I am new at docker and docker-compose and I am developing a Laravel-project on docker and docker-compose with Laradock as following a tutorial(not sure whether It is a correct way or not to refer this situation though).
I want to install the composer in this environment to be able to use the composer command.
As a matter of fact, I wanted to do seeding to put data into DB that I made by php artisan make:migrate but this error appeared.
include(/var/www/laravel_practice/vendor/composer/../../database/seeds/AdminsTableSeeder.php): failed to open stream: No such file or directory
So I googled this script to find a solution that will solve the error then I found it.
It says, "Do composer dump-autoload and try seeding again", so I followed it then this error appeared.
bash: composer: command not found
Because I have not installed composer into docker-container.
My docker's condition is like this now.
・workspace
・mysql
・apache
・php-fpm
Since I have not installed the composer, I have to install it into docker-container to solve the problem, BUT I have no idea how to install it into docker-container.
So could anyone tell me how to install composer into docker-container?
Thank you.
here is the laradock/mysql/Dockerfile and laravelProject/docker-compose.yml.
ARG MYSQL_VERSION=5.7
FROM mysql:${MYSQL_VERSION}
LABEL maintainer="Mahmoud Zalt <mahmoud#zalt.me>"
#####################################
# Set Timezone
#####################################
ARG TZ=UTC
ENV TZ ${TZ}
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && chown -R mysql:root /var/lib/mysql/
COPY my.cnf /etc/mysql/conf.d/my.cnf
CMD ["mysqld"]
EXPOSE 3306
version: '2'
services:
db:
image: mysql:5.7
ports:
- "6603:3306"
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=true
- MYSQL_DATABASE=laravelProject
- LANG=C.UTF-8
volumes:
- db:/var/lib/mysql
command: mysqld --sql-mode=NO_ENGINE_SUBSTITUTION --character-set-server=utf8 --collation-server=utf8_unicode_ci
web:
image: arbiedev/php-nginx:7.1.8
ports:
- "8080:80"
volumes:
- ./www:/var/www
- ./nginx.conf:/etc/nginx/sites-enabled/default
volumes:
db:
You can build your own image and use it in your Docker compose file.
FROM php:7.2-alpine3.8
RUN apk update
RUN apk add bash
RUN apk add curl
# INSTALL COMPOSER
RUN curl -s https://getcomposer.org/installer | php
RUN alias composer='php composer.phar'
# INSTALL NGINX
RUN apk add nginx
I used the PHP alpine image as my base image because it's lightweight, so you might have to install other dependencies yourself. In your docker-compose file
web:
build: path/to/your/Dockerfile/directory
image: your-image-tag
ports:
- "8080:80"
volumes:
- ./www:/var/www
- ./nginx.conf:/etc/nginx/sites-enabled/default
You could do something like this:
FROM php:8.0.2-apache
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y mariadb-client libxml2-dev
RUN apt-get autoremove -y && apt-get autoclean
RUN docker-php-ext-install mysqli pdo pdo_mysql xml
COPY --from=composer /usr/bin/composer /usr/bin/composer
the argument COPY --from= should solve your problem.
FROM php:7.3-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
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'

Docker and laravel not clearing the cache

I have an app running with docker and laravel but there are some instructions that apparently are executed but not functioning.
These instructions are php artisan clear:cache and php artisan optimize.
Sometimes I need to get into the container and run these instructions in order to have my changes reflected in the browsers. Apart from the failures that kubernetes detects due to the unreflected changes
FROM php:7.2.0-fpm as php
RUN apt-get update && apt-get install -y openssl openssh-client \
rsync sshpass zip unzip git vim libmcrypt-dev mysql-client \
libssh2-1-dev libmagickwand-dev \n
gnupg2 --no-install-recommends zlib1g-dev sass \
&& pecl install mcrypt-1.0.1 \
&& docker-php-ext-enable mcrypt \
&& pecl install imagick \
&& docker-php-ext-enable imagick \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-install gd \
&& docker-php-ext-install zip \
&& pecl install ssh2-1.0 \
&& docker-php-ext-enable ssh2
RUN curl --silent --location https://deb.nodesource.com/setup_11.x | bash -
RUN apt-get install -y nodejs
RUN npm install --global bower
WORKDIR /var/www/html
COPY install-composer.sh install-composer.sh
RUN bash ./install-composer.sh
COPY composer.json ./
COPY composer.lock ./
RUN composer install --no-scripts --no-autoloader
COPY . ./
RUN composer update
RUN composer install
COPY zz-docker.conf /usr/local/etc/php-fpm.d/zz-docker.conf
RUN mkdir -p /var/run/php/sock/
RUN sass resources/sass/style.scss:public/assets/stylesheets/style.css
RUN bower install --allow-root
RUN composer dump-autoload
RUN php artisan vendor:publish --provider="Sentry\SentryLaravel\SentryLaravelServiceProvider"
ONBUILD chown -R www-data:www-data .
RUN php artisan optimize && php artisan config:cache \
&& php artisan view:cache && php artisan view:clear
Most of the time I need to get into the container and run the php artisan optimize, view:cache and view:clear again even though they are in the Dockerfile so it shouldn't be necessary to do it all over again
Any commands that are being executed using RUN in the Dockerfile will be executed only during the build phase. If you wish to run them while starting a container so you don't have to run them manually then you need to use a script e.g. bash script that can be used as an ENTRYPOINT for your Dockerfile and then make this script execute the command that you should run to start the application. So your entrypoint.sh can look like this:
# entrypoint.sh
#!/usr/bin/env sh
...
php artisan optimize
php artisan config:cache
php artisan view:cache
php artisan view:clear
...
php artisan serve # or use exec "$#" and pass php artisan serve through CMD in Dockerfile
This will make every container that you start execute all of these commands before start serving your application.
Other links that you might want to check:
What is the difference between CMD and ENTRYPOINT in a Dockerfile?
What does set -e and exec “$#” do for docker entrypoint scripts?
You can also use the cmd:
sudo docker-compose exec php php /var/www/html/path_to_laravel_project_dir/artisan config:cache
it will execute the artisan cmd in the container.
Just ran into a problem of cache:clear with Laravel too (no Docker involved in my case)
Fact is : if Artisan has no rights to clear caches, it just silently fails, proudly telling you cache have been successfully cleared (sigh).
See https://github.com/laravel/framework/issues/1179 for more details
*In my case, adding simple "sudo" before my command did the trick... but I lost half an hour understanding the cause (what an introduction to Laravel/Artisan :p)
*I have a Laravel v6.5

How to run laravel app inside docker container?

I want to get docker container's result from local, following is what I tried.
step1.
create php-composer image using dockerFile.
FROM php:7
RUN apt-get update
RUN apt-get install curl
RUN curl -sS https://getcomposer.org/installer -o composer-setup.php
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
RUN apt-get install -y git
step2.
create container and execute laravel app.
docker run -p 127.0.0.1:3000:8000 --name MyTest -dt php-composer to create a container
docker cp laravelApp/ d4bbb5d36312:/usr/
docker exec -it MyTest bash
cd usr/laravelApp
php artisan serve
After that, terminal shows success info:
Laravel development server started: <http://127.0.0.1:8000>
But when I access 127.0.0.1:3000 at browser, I get nothing.
Why is that?
there are some php extensions that are needed for laravel to work, so you need to install them too, this is the full dockerfile
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 mbstring
WORKDIR /app
COPY app /app # this copies all the app files to a folder called `app`
RUN composer install
CMD php artisan serve --host=0.0.0.0 --port=8000
EXPOSE 8000
then to run the container, run this command only:
docker run -p 3000:8000 --name MyTest
then go to http://localhost:3000
let me know if it didn't work

Resources